sieve of eratosthenes
C#
/* Sieve of Erastosthenes in C#
* Written by Robert Hoelz
*
* If you're using Mono, use gmcs to compile.
*/
using System;
using System.Collections.Generic;
class Sieve {
private List<int> primes;
public Sieve(string str) {
int limit = System.Convert.ToInt32(str);
primes = new List<int>();
primes.Add(0);
primes.Add(0);
for(int i = 2; i < limit; i++) {
primes.Add(1);
}
}
private void generate() {
int currPrime = 2;
while(currPrime * currPrime < primes.Count) {
int currMult = 2;
while((currMult * currPrime) < primes.Count) {
primes[currMult * currPrime] = 0;
currMult++;
}
int i;
for(i = currPrime + 1; i < primes.Count; i++) {
if(primes[i] == 1)
break;
}
currPrime = i;
}
}
private void print() {
for(int i = 0; i < primes.Count; i++) {
if(primes[i] == 1)
Console.Write(i + " ");
}
Console.WriteLine();
}
public static void Main(string[] args) {
if(args.Length > 0) {
Sieve s = new Sieve(args[0]);
s.generate();
s.print();
}
}
}
stats
It is
Saturday May 17, 2008 10:56 am
This page served 666 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:56:55 up 23 days, 11:39, 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.