aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-142/luca-ferrari/postgresql/ch-1.sql20
1 files changed, 20 insertions, 0 deletions
diff --git a/challenge-142/luca-ferrari/postgresql/ch-1.sql b/challenge-142/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..57de950651
--- /dev/null
+++ b/challenge-142/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,20 @@
+CREATE OR REPLACE FUNCTION
+ f_divisors_last_digit( m int, n int )
+ RETURNS SETOF int
+AS $CODE$
+ WITH RECURSIVE numbers AS (
+ SELECT 1 as num
+ UNION
+ SELECT num + 1
+ FROM numbers
+ WHERE num + 1 <= m )
+ , divisors AS (
+ SELECT num as div
+ FROM numbers
+ WHERE m % num = 0 )
+ SELECT count(*)
+ FROM divisors
+ WHERE div::text LIKE ( '%' || n::text );
+
+ $CODE$
+ LANGUAGE sql;