The sieve of eratosthenes
Pascal
(* Sieve of Eratosthenes in Pascal
* Written by Rob Hoelz
*)
program sieve(output);
var
limit : longint;
primes : ^boolean;
(* I didn't know of a standard library atoi... *)
function atoi(s : string) : longint;
var i : longint;
var num : longint;
begin
num := 0;
i := 0;
for i := 1 to length(s) do
begin
num := num * 10;
num := num + (ord(s[i]) - ord('0'));
end;
atoi := num
end;
procedure removeMultiples(base : longint);
var i : longint;
begin
i := base + base;
while i <= limit do
begin
primes[i] := false;
i := i + base
end
end;
function nextPrime(currPrime : longint) : longint;
var i : longint;
begin
for i := currPrime + 1 to limit do
begin
if primes[i] then
break
end;
nextPrime := i
end;
procedure generatePrimes();
var currPrime : longint;
begin
primes[1] := false;
for currPrime := 2 to limit do
begin
primes[currPrime] := true;
end;
currPrime := 2;
while ((currPrime * currPrime) <= limit) do
begin
removeMultiples(currPrime);
currPrime := nextPrime(currPrime)
end
end;
procedure printPrimes();
var i : longint;
begin
for i := 1 to limit do
begin
if primes[i] then
begin
write(i);
write(' ')
end
end;
writeln()
end;
begin
if paramcount > 0 then
begin
limit := atoi(paramstr(1));
getmem(primes, limit * sizeof(boolean));
generatePrimes();
printPrimes();
freemem(primes)
end
end.
stats
It is
Friday December 05, 2008 10:49 am
This page served 978 times
This page last modified: April 14, 2008 11:28 am
Your IP address is: 38.103.63.61
You are browsing using: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
You are browsing from: United States.
badcomputer.org's uptime: 10:49:51 up 24 days, 15:00, 2 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.