Subject | RE: [firebird-support] Prime numbers |
---|---|
Author | Svein Erling Tysvær |
Post date | 2013-01-16T10:09:44Z |
>The example below is from PostgreSQL. Is there an easy way to do it in Firebird 2.5 or 3.0?You could try something like:
>
>I need prime numbers to analyse bio data:
>
>with primes (num) as (
> select generate_series(2,10000)
>)
>SELECT num
>FROM primes p1
>WHERE num not in (
> SELECT p1.num
> FROM primes p2
> WHERE p2.num < p1.num
> AND MOD(p1.num, p2.num) = 0
>);
execute block (max_prime integer = :max_prime) returns(num integer)
as
declare variable i integer;
declare variable i2 integer;
declare variable prime integer;
begin
num = 2;
i2 = sqrt(max_prime);
while (num < max_prime) do
begin
prime = 1;
i = 2;
while ((num > i) and (i < i2)) do
begin
if ((num - ((num/i)*i)) = 0) then
prime = 0;
i = i + 1;
end
if (prime = 1) then
suspend;
num = num+1;
end
end
How to define input parameters (max_prime) depends on your client software. The above gets a list of numbers in Database Workbench (though I'm no mathematician, so you ought to check whether the result is correct).
HTH,
Set