aboutsummaryrefslogtreecommitdiff
path: root/challenge-141/luca-ferrari/postgresql/ch-1.sql
blob: c68e881175261767fa8c261ec221f8588d9e932a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
CREATE OR REPLACE FUNCTION
f_find_divisors( divisors int default 8, count int default 10 )
RETURNS SETOF int
AS $CODE$
DECLARE
   current_number  int;
   current_divisor int;
   current_found   int;
BEGIN
    FOR current_number IN 1 .. 999999 LOOP
        IF count = 0 THEN
           EXIT;
        END IF;

        current_found := 0;

        FOR current_divisor IN 1 .. current_number LOOP
            IF current_number % current_divisor = 0 THEN
               current_found := current_found + 1;
            END IF;
        END LOOP;

        IF current_found = divisors THEN
           count := count - 1;
           RETURN NEXT current_number;
        END IF;
    END LOOP;

    RETURN;
END
$CODE$
LANGUAGE plpgsql;