sieve of eratosthenes

PHP

<?php
// written by Darren Kirby


$x = $argv[1];
$y = floor(sqrt($x));
$primes = array();
$set = range(2, $x);

while ($set[0] < $y) {
    $primes[] = $set[0];
    $temp =  array_slice($set, 1);
    foreach ($temp as $i) {
        if ($i % $set[0] == 0) {
            unset($set[array_search($i, $set)]);
        }
    }
    array_shift($set); 
} 

foreach ($set as $i)
    $primes[] = $i;

foreach ($primes as $n)
    echo "$n ";

?>

back to Eratosthenes page

stats

It is Saturday May 17, 2008 11:03 am
This page served 946 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: 11:03:36 up 23 days, 11:45, 0 users, load average: 0.04, 0.09, 0.04

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

Google

credits

hacker emblem

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.

advertisement