aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-16 21:05:10 +0000
committerGitHub <noreply@github.com>2021-11-16 21:05:10 +0000
commit57143c046f69a013c65d4448f915e122d4c7bb2b (patch)
tree8916adf96bef394bc123bed7bdd8aa812d02a89c
parentbc2ae3be43b409eff63e8b8fcdb78eea0cc37d72 (diff)
parent886740c02f62fdf1c87b345767255725d24a8df5 (diff)
downloadperlweeklychallenge-club-57143c046f69a013c65d4448f915e122d4c7bb2b.tar.gz
perlweeklychallenge-club-57143c046f69a013c65d4448f915e122d4c7bb2b.tar.bz2
perlweeklychallenge-club-57143c046f69a013c65d4448f915e122d4c7bb2b.zip
Merge pull request #5227 from fluca1978/PWC139
Pwc139
-rw-r--r--challenge-139/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-139/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-139/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-139/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-139/luca-ferrari/postgresql/ch-1.sql18
-rw-r--r--challenge-139/luca-ferrari/postgresql/ch-2.sql71
-rw-r--r--challenge-139/luca-ferrari/raku/ch-1.p66
-rw-r--r--challenge-139/luca-ferrari/raku/ch-2.p616
8 files changed, 115 insertions, 0 deletions
diff --git a/challenge-139/luca-ferrari/blog-1.txt b/challenge-139/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..99bc56fc61
--- /dev/null
+++ b/challenge-139/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/11/15/PerlWeeklyChallenge139.html#task1
diff --git a/challenge-139/luca-ferrari/blog-2.txt b/challenge-139/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..44a33b22f8
--- /dev/null
+++ b/challenge-139/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/11/15/PerlWeeklyChallenge139.html#task2
diff --git a/challenge-139/luca-ferrari/blog-3.txt b/challenge-139/luca-ferrari/blog-3.txt
new file mode 100644
index 0000000000..c7a5e9ab51
--- /dev/null
+++ b/challenge-139/luca-ferrari/blog-3.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/11/15/PerlWeeklyChallenge139.html#task1pg
diff --git a/challenge-139/luca-ferrari/blog-4.txt b/challenge-139/luca-ferrari/blog-4.txt
new file mode 100644
index 0000000000..72dd2e6987
--- /dev/null
+++ b/challenge-139/luca-ferrari/blog-4.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/11/15/PerlWeeklyChallenge139.html#task2pg
diff --git a/challenge-139/luca-ferrari/postgresql/ch-1.sql b/challenge-139/luca-ferrari/postgresql/ch-1.sql
new file mode 100644
index 0000000000..9685ec2948
--- /dev/null
+++ b/challenge-139/luca-ferrari/postgresql/ch-1.sql
@@ -0,0 +1,18 @@
+CREATE OR REPLACE FUNCTION
+ f_jort_sort( n int[] )
+ RETURNS int
+AS $CODE$
+ DECLARE
+ n_ordered int[];
+BEGIN
+ SELECT ( ARRAY( SELECT unnest( n ) ORDER BY 1 ) )
+ INTO n_ordered;
+
+ IF array_to_string( n, '|' ) = array_to_string( n_ordered, '|' ) THEN
+ RETURN 1;
+ ELSE
+ RETURN 0;
+ END IF;
+END
+ $CODE$
+ LANGUAGE plpgsql;
diff --git a/challenge-139/luca-ferrari/postgresql/ch-2.sql b/challenge-139/luca-ferrari/postgresql/ch-2.sql
new file mode 100644
index 0000000000..0356250936
--- /dev/null
+++ b/challenge-139/luca-ferrari/postgresql/ch-2.sql
@@ -0,0 +1,71 @@
+CREATE OR REPLACE FUNCTION
+ f_long_primes( l int default 5 )
+ RETURNS SETOF bigint
+AS $CODE$
+ DECLARE
+ current_value numeric;
+ i int;
+ is_prime bool;
+ rational_part text;
+ done int := 0;
+ repeating_part_size int := 0;
+ repeating_part text;
+ result_as_text text;
+ text_array text[];
+
+BEGIN
+
+ for current_value IN 1 .. 99999 LOOP
+ is_prime := true;
+
+ -- check if this is prime
+ for i in 2 .. ( current_value - 1 ) LOOP
+ if current_value % i = 0 THEN
+ is_prime := false;
+ exit; -- terminate this loop
+ END IF;
+ END LOOP;
+
+ -- avoid inspecting this number if it is not prime
+ if is_prime = false THEN
+ continue;
+ END IF;
+
+ result_as_text := ( 1 / current_value::numeric )::text;
+ i := strpos( result_as_text, '.' );
+ rational_part := substr( result_as_text, i + 1 );
+ repeating_part_size := current_value - 1;
+ repeating_part := substr( rational_part, 1, repeating_part_size );
+
+ RAISE DEBUG 'Inspecting % -> % {%} => % ( %, % )',
+ current_value,
+ rational_part,
+ repeating_part_size,
+ repeating_part,
+ i,
+ ( 1 / current_value )::text;
+
+
+ text_array := regexp_split_to_array( rational_part, repeating_part );
+ is_prime := true;
+ FOREACH result_as_text IN ARRAY text_array LOOP
+ IF result_as_text <> '' AND substr( repeating_part, 1, length( result_as_text ) ) <> result_as_text THEN
+ is_prime := false;
+ exit;
+ END IF;
+ END LOOP;
+
+ IF is_prime THEN
+ RETURN NEXT current_value;
+ done := done + 1;
+ END IF;
+
+ IF done > l THEN
+ EXIT;
+ END IF;
+ END LOOP;
+
+ RETURN;
+END
+ $CODE$
+ LANGUAGE plpgsql;
diff --git a/challenge-139/luca-ferrari/raku/ch-1.p6 b/challenge-139/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..6a91ea2e47
--- /dev/null
+++ b/challenge-139/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,6 @@
+#!raku
+
+sub MAIN( *@n where { @n.elems > 0 && @n.grep( * ~~ Int ).elems == @n.elems } ) {
+ '1'.say and exit if @n ~~ @n.sort;
+ '0'.say;
+}
diff --git a/challenge-139/luca-ferrari/raku/ch-2.p6 b/challenge-139/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..a7f35ef47e
--- /dev/null
+++ b/challenge-139/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,16 @@
+#!raku
+
+sub MAIN( Int $limit where { $limit >= 1 } = 5 ) {
+ my @solutions;
+ for 1 .. Inf {
+ next if ! $_.is-prime;
+ my $repeating-size = $_ - 1;
+ my $repeating-part = ( 1 / $_ ).Rat.base-repeating( 10 )[ 1 ];
+ next if ! $repeating-part;
+
+ @solutions.push: $_ if $repeating-part.Str.chars == $repeating-size;
+ last if @solutions.elems == $limit;
+ }
+
+ @solutions.join( ', ' ).say;
+}