-- Sieve of Eratosthenes
-- The sieve of Eratosthenes is a simple algorithm to create a list of prime -- numbers up to some specified limit.
-- First, a list of all interger numbers from 2 up to the specified limit is -- created. Then, the algorithm repeats the following steps until the list -- is empty.
-- The smallest number of the remaining list is picked, it's a prime number. -- All multiples of this numbers are deleted from the list.
-- All the picked numbers are the primes numbers of the original range. DROP PROCEDURE IF EXISTS sieve; DELIMITER $$ CREATE PROCEDURE sieve (amount INT) BEGIN DECLARE cnt INT DEFAULT 2; DECLARE curprime INT; DECLARE lastprime INT DEFAULT 1; -- Temporary table speeds up operations DROP TABLE IF EXISTS mysieve; CREATE TABLE mysieve (nr MEDIUMINT UNSIGNED NOT NULL PRIMARY KEY) ENGINE=MEMORY; DROP TABLE IF EXISTS primes; CREATE TABLE primes (nr MEDIUMINT UNSIGNED NOT NULL) …
[Read more]