diff options
Diffstat (limited to 'challenge-141/luca-ferrari/postgresql/ch-2.sql')
| -rw-r--r-- | challenge-141/luca-ferrari/postgresql/ch-2.sql | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/challenge-141/luca-ferrari/postgresql/ch-2.sql b/challenge-141/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..239d846b58 --- /dev/null +++ b/challenge-141/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,21 @@ +CREATE OR REPLACE FUNCTION +f_live_numbers( m int, n int ) +RETURNS SETOF int +AS $CODE$ +WITH RECURSIVE +numbers AS ( SELECT unnest( regexp_split_to_array( m::text, '' ) ) AS n ) +, combinations( i, v, c ) AS ( +SELECT 1, n, n +FROM numbers +UNION +SELECT i + 1, n, c || num.n +FROM combinations, numbers num +WHERE length( c || num.n ) < length( m::text ) - 1 +AND num.n IN ( SELECT n FROM numbers WHERE n::int > i ) +) + +SELECT c::int FROM combinations +WHERE c::int % n = 0; + +$CODE$ +LANGUAGE sql; |
