aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-10 19:12:54 +0000
committerGitHub <noreply@github.com>2022-01-10 19:12:54 +0000
commitf362207597bd12c424a9e651d276f28742bee361 (patch)
treee57b62f7332d23be2dc7ffbc855a8efb5512e160
parente1bd7f8ed0113c56857066bd69c613b8bfb548f2 (diff)
parentf192e61c5d3966500102b4f69cdf2cf9e6a2c268 (diff)
downloadperlweeklychallenge-club-f362207597bd12c424a9e651d276f28742bee361.tar.gz
perlweeklychallenge-club-f362207597bd12c424a9e651d276f28742bee361.tar.bz2
perlweeklychallenge-club-f362207597bd12c424a9e651d276f28742bee361.zip
Merge pull request #5500 from fluca1978/PWC147
Pwc147
-rw-r--r--challenge-147/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-147/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-147/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-147/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-147/luca-ferrari/postgresql/ch-1.sql83
-rw-r--r--challenge-147/luca-ferrari/postgresql/ch-2.sql97
-rwxr-xr-xchallenge-147/luca-ferrari/raku/ch-1.p617
-rwxr-xr-xchallenge-147/luca-ferrari/raku/ch-2.p631
8 files changed, 232 insertions, 0 deletions
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
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
+<<MAIN_LOOP>>
+ 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;
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;
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;
+}
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;
+ }
+ }
+
+ }