From a5295c7d01239392baa29a49a085d00fe165b7bd Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 11:58:30 +0100 Subject: Task 1 done --- challenge-147/luca-ferrari/raku/ch-1.p6 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 challenge-147/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-147/luca-ferrari/raku/ch-1.p6 b/challenge-147/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..53eb20c0c0 --- /dev/null +++ b/challenge-147/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#!raku + +sub MAIN( Int $limit = 20 ) { + + my @primes; + + for 10 .. Inf -> $current { + next if $current ~~ / 0 /; + next if ! $current.is-prime; + my @values.push: $current.comb[ $_ .. * - 1 ].join.Int for 0 ..^ $current.Str.chars; + @primes.push: $current if @values.grep( *.is-prime ).elems == @values.elems; + last if @primes.elems >= $limit; + + } + + @primes.join( "\n" ).say; +} -- cgit From afb6b053323475a1fe26d22c847611fac730a8b3 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 14:06:41 +0100 Subject: Task 2 done --- challenge-147/luca-ferrari/raku/ch-2.p6 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 challenge-147/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-147/luca-ferrari/raku/ch-2.p6 b/challenge-147/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..daab2b97c4 --- /dev/null +++ b/challenge-147/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,31 @@ +#!raku + + sub MAIN( Int $limit = 3000 ) { + + my ( %pentagons, %inverse-pentagons ); + %pentagons{ $_ } = ( $_ * ( 3 * $_ - 1 ) / 2 ) for 1 .. $limit; + %inverse-pentagons{ %pentagons{ $_ } } = $_ for %pentagons.keys.sort; + + + + for %pentagons.keys.sort -> $index-left { + for %pentagons.keys.sort -> $index-right { + next if $index-left == $index-right; + + + my ( $sum, $diff ) = %pentagons{ $index-left } + %pentagons{ $index-right }, + abs( %pentagons{ $index-left } - %pentagons{ $index-right } ); + + # this is too slow, therefore I use an inverse hash! + # next if ! %pentagons.values.grep( * ~~ $sum ); + # next if ! %pentagons.values.grep( * ~~ $diff ); + next if %inverse-pentagons{ $diff }:!exists; + next if %inverse-pentagons{ $sum }:!exists; + + "P( $index-left ) + P( $index-right ) = { %pentagons{ $index-left } } + { %pentagons{ $index-right } } = $sum = P( { %inverse-pentagons{ $sum } } )".say; + "P( $index-left ) - P( $index-right ) = { %pentagons{ $index-left } } + { %pentagons{ $index-right } } = $diff = P( {%inverse-pentagons{ $diff } } )".say; + exit; + } + } + + } -- cgit From c446116e9f879b3815294b15b15143a832c2268c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 14:34:18 +0100 Subject: Task 1 done in PostgreSQL --- challenge-147/luca-ferrari/postgresql/ch-1.sql | 83 ++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 challenge-147/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-147/luca-ferrari/postgresql/ch-1.sql b/challenge-147/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..7e24f9ceb0 --- /dev/null +++ b/challenge-147/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,83 @@ +CREATE OR REPLACE FUNCTION +f_is_prime( n bigint ) +RETURNS bool +AS +$CODE$ +DECLARE + i int; +BEGIN + FOR i IN 2 .. ( n - 1 ) LOOP + IF n % i = 0 THEN + RETURN false; + END IF; + END LOOP; + + RETURN true; +END +$CODE$ +LANGUAGE plpgsql; + + +/** +testdb=> select * from f_generate_truncated_primes(); +f_generate_truncated_primes +----------------------------- +11 +13 +17 +23 +31 +37 +41 +43 +47 +53 +61 +67 +71 +73 +83 +97 +113 +131 +137 +167 +(20 rows) + +*/ +CREATE OR REPLACE FUNCTION +f_generate_truncated_primes( l int = 20 ) +RETURNS SETOF int +AS +$CODE$ +DECLARE + i int; + current bigint; + fnd int := 0; +BEGIN +<> + FOR current IN SELECT * FROM generate_series( 10, 999999 ) LOOP + CONTINUE WHEN current::text LIKE '%0%'; + + IF NOT f_is_prime( current ) THEN + CONTINUE MAIN_LOOP; + END IF; + + + FOR i IN 1 .. length( current::text ) LOOP + IF NOT f_is_prime( substring( current::text FROM i )::int ) THEN + CONTINUE MAIN_LOOP; + END IF; + END LOOP; + + fnd := fnd + 1; + RETURN NEXT current; + IF fnd >= l THEN + RETURN; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 30a0e70fd189d521add0046cb10452685ed615a0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 15:32:20 +0100 Subject: Task 2 done in PostgreSQL --- challenge-147/luca-ferrari/postgresql/ch-2.sql | 97 ++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 challenge-147/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-147/luca-ferrari/postgresql/ch-2.sql b/challenge-147/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..61465a21c6 --- /dev/null +++ b/challenge-147/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,97 @@ +/* +testdb=> select * from f_pentagons_pairs(); +INFO: P(1020) + P(2167) = P(8602840) = 1560090 +INFO: P(1020) - P(2167) = P(5482660) = 7042750 +n1 | n2 | s | d | ps | pd +------+------+---------+---------+---------+--------- +1020 | 2167 | 1560090 | 7042750 | 8602840 | 5482660 +(1 row) + +Time: 7257,715 ms (00:07,258) + +*/ + +CREATE OR REPLACE FUNCTION +f_pentagon( n bigint ) +RETURNS bigint +AS +$CODE$ + SELECT ( n * ( 3 * n - 1 ) / 2 ); +$CODE$ +LANGUAGE sql +IMMUTABLE; + + +DROP TABLE IF EXISTS pentagons; +CREATE TABLE pentagons +( + n bigint + , p bigint GENERATED ALWAYS AS ( f_pentagon( n ) ) STORED +); + + + +INSERT INTO pentagons( n ) +SELECT generate_series( 1, 5000 ); + + + + +CREATE OR REPLACE FUNCTION +f_pentagons_pairs() +RETURNS TABLE ( n1 bigint, n2 bigint, s bigint, d bigint, ps bigint, pd bigint ) +AS $CODE$ +DECLARE + current_tuple pentagons%rowtype; + other_tuple pentagons%rowtype; + fnd int := 0; +BEGIN + + FOR current_tuple IN SELECT * FROM pentagons ORDER BY n LOOP + SELECT * + INTO other_tuple + FROM pentagons pp + WHERE EXISTS( + SELECT * + FROM pentagons ps + WHERE ps.p = current_tuple.p + pp.p + ) + AND EXISTS ( + SELECT * + FROM pentagons ps + WHERE ps.p = abs( current_tuple.p - pp.p ) + ); + + + IF FOUND THEN + SELECT current_tuple.n + , other_tuple.n + , current_tuple.p + , other_tuple.p + , current_tuple.p + other_tuple.p + , abs( current_tuple.p - other_tuple.p ) + , p1.n + , p2.n + INTO n1, n2, s, d, ps, pd + FROM pentagons p1, pentagons p2 + WHERE p1.p = current_tuple.p + other_tuple.p + AND p2.p = abs( current_tuple.p - other_tuple.p ); + + RAISE INFO 'P(%) + P(%) = P(%) = %', + n1, n2, ps, s; + + RAISE INFO 'P(%) - P(%) = P(%) = %', + n1, n2, pd, d; + + + fnd := fnd + 1; + RETURN NEXT; + RETURN; + END IF; + + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From f192e61c5d3966500102b4f69cdf2cf9e6a2c268 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 10 Jan 2022 17:24:47 +0100 Subject: Blog references --- challenge-147/luca-ferrari/blog-1.txt | 1 + challenge-147/luca-ferrari/blog-2.txt | 1 + challenge-147/luca-ferrari/blog-3.txt | 1 + challenge-147/luca-ferrari/blog-4.txt | 1 + 4 files changed, 4 insertions(+) create mode 100644 challenge-147/luca-ferrari/blog-1.txt create mode 100644 challenge-147/luca-ferrari/blog-2.txt create mode 100644 challenge-147/luca-ferrari/blog-3.txt create mode 100644 challenge-147/luca-ferrari/blog-4.txt diff --git a/challenge-147/luca-ferrari/blog-1.txt b/challenge-147/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..af17183edb --- /dev/null +++ b/challenge-147/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task1 diff --git a/challenge-147/luca-ferrari/blog-2.txt b/challenge-147/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..5e5d379514 --- /dev/null +++ b/challenge-147/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task2 diff --git a/challenge-147/luca-ferrari/blog-3.txt b/challenge-147/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..aa716b41b9 --- /dev/null +++ b/challenge-147/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task1pg diff --git a/challenge-147/luca-ferrari/blog-4.txt b/challenge-147/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..520b859582 --- /dev/null +++ b/challenge-147/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/01/04/PerlWeeklyChallenge146.html#task2pg -- cgit