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.
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 $totalRunning 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 $totalThe 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 10Replacing 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 10These techniques are useful for quick generation of numeric ranges, step‑wise sequences, and repeated characters directly within shell scripts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
