From 705640f5611e95eb41001b853b84c6b3d1bd22bd Mon Sep 17 00:00:00 2001
From: Luca Ferrari <fluca1978@gmail.com>
Date: Mon, 29 Nov 2021 16:22:22 +0100
Subject: Task 2 in PostgreSQL

---
 challenge-141/luca-ferrari/postgresql/ch-2.sql | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 challenge-141/luca-ferrari/postgresql/ch-2.sql

(limited to 'challenge-141/luca-ferrari/postgresql/ch-2.sql')

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;
-- 
cgit 

