sieve of eratosthenes
Bash
#!/bin/bash
# Sieve of Eratosthenes in Bash
# Written by Rob Hoelz
removeMultiples () {
local i=`expr $currPrime + $currPrime`
while (( $i <= $limit )); do
primes[$i]=0
i=`expr $i + $currPrime`
done
}
nextPrime () {
currPrime=`expr $currPrime + 1`
while (( $currPrime <= $limit )); do
if [ ${primes[$currPrime]} -eq 1 ]
then
break
fi
currPrime=`expr $currPrime + 1`
done
}
printList () {
local i=0
while (( $i <= $limit )); do
if [ ${primes[$i]} -eq 1 ]
then
echo -n "$i "
fi
i=`expr $i + 1`
done
echo ''
}
if [ -z "$1" ]
then
exit
fi
limit=$1
primes=(`yes 1 | head -$(expr $limit + 1)`)
primes[0]=0
primes[1]=0
currPrime=2
while (( `expr $currPrime \* $currPrime` <= "$limit" )); do
removeMultiples
nextPrime
done
printList
stats
It is
Saturday May 17, 2008 10:52 am
This page served 588 times
This page last modified: April 14, 2008 11:28 am
Your IP address is: 38.103.63.17
You are browsing using: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
You are browsing from: United States.
badcomputer.org's uptime: 10:52:43 up 23 days, 11:34, 0 users, load average: 0.00, 0.00, 0.00
local
home | unix stuff | dir2ogg | sneetchalizer | wmainfo | q&d guide to permissions | q&d guide to tar and gzip | code | MS rant | browser shootout | linux & iAudio X5 | photos | music | programming poetry | sieve of Eratosthenes | plea | rain | suffer | archive | about | recipes | compaqr3000 | sitemap
search
credits
This page, and all pages on this site were created and are maintained by Darren Kirby using valid XHTML 1.0 and CSS, and are ©copyright 2002 - 2008. The Penguin image was created by Tukka, and is used by permission. Inspiration for the look of this site was provided by Eric A. Meyer's CSS gallery. This website runs on Gentoo Linux. It is served by Apache. PHP and MySQL hold together the backend.