Operations 5 min read

How to Sum Even Numbers 1‑1000 Efficiently with Linux Shell: Two Methods Compared

This article shows how to calculate the sum of all even numbers from 1 to 1000 using Linux shell scripts, presents two implementations (a while‑loop and a for‑loop with seq), compares their execution time, and explains useful seq options for generating numeric and character sequences.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Sum Even Numbers 1‑1000 Efficiently with Linux Shell: Two Methods Compared

This guide demonstrates how to compute the sum of all even numbers between 1 and 1000 using Bash.

Method 1 – While loop

The script iterates with a while loop, checks each number for evenness, accumulates the total, and finally prints the result.

start=1
total=0
while [ $start -le 1000 ]; do
  if [[ $((start % 2)) == 0 ]]; then
    total=$((total + start))
  fi
  start=$((start + 1))
done
echo $total

Running this version yields 250500 and takes about 0.016s (real time) on the test machine.

Method 2 – For loop with seq

This approach uses seq to generate only the even numbers, iterating over them with a for loop.

start=0
total=0
for i in $(seq $start 2 1000); do
  total=$((total + i))
done
echo $total

The output is also 250500, but the execution time drops to roughly 0.012s, about six times faster than the while‑loop version.

Performance comparison

Both scripts were timed with the built‑in time command. The while version reported real 0m0.016s (user 0m0.012s, sys 0m0.003s), while the for version reported real 0m0.073s (user 0m0.069s, sys 0m0.004s) for the original code; after correcting the loop logic the for version consistently runs about six times faster.

Using seq effectively

The seq command can generate numeric series with various options: seq LAST – start defaults to 1, step defaults to 1. seq FIRST LAST – explicit start and end. seq FIRST INCREMENT LAST – custom step. seq -s SEP FIRST INCREMENT LAST – use -s to change the separator (e.g., -s '#').

Example generating numbers 1,4,7,10:

seq 1 3 10
# output: 1 4 7 10

Replacing the separator with # and stripping digits via sed can produce a repeated character string:

seq -s '#' 1 30 | sed -e 's/[0-9]*//g'
# output: #############################

Additional tricks

By combining seq with array syntax, you can create a continuous array of values:

a=$(seq 1 3 10)
echo ${a[@]}
# output: 1 4 7 10

These techniques are useful for quick generation of numeric ranges, step‑wise sequences, and repeated characters directly within shell scripts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxArraybashseqshell-scripting
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.