sieve of eratosthenes

SQL

/* Sieve of Eratosthenes in SQL (MySQL)
 * Written by Rob Hoelz
 *
 * To use:
 *  $ mysql
 *  mysql> use <database>
 *  mysql> source sieve.sql
 *  mysql> call sieve(<limit>);
 */

Delimiter //
create procedure sieve (lim int)
  begin
    declare i int;
    create temporary table if not exists primes (num int);
    truncate primes;

    set i = 2;

    while i <= lim do
        insert into primes values (i);
        set i = i + 1;
    end while;

    set i = 2;

    while (i * i) <= lim do
        delete from primes where (num <> i) and (num % i = 0);
        set i = (select num from primes where num > i limit 1);
    end while;

    select num from primes order by num asc;
  end;
//
Delimiter ;

back to Eratosthenes page

stats

It is Sunday October 12, 2008 1:38 pm
This page served 865 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: 13:38:38 up 74 days, 04:45, 1 user, 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

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