diff options
| -rw-r--r-- | challenge-137/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-137/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-137/luca-ferrari/blog-3.txt | 1 | ||||
| -rw-r--r-- | challenge-137/luca-ferrari/blog-4.txt | 1 | ||||
| -rw-r--r-- | challenge-137/luca-ferrari/postgresql/ch-1.sql | 61 | ||||
| -rw-r--r-- | challenge-137/luca-ferrari/postgresql/ch-2.sql | 36 | ||||
| -rwxr-xr-x | challenge-137/luca-ferrari/raku/ch-1.p6 | 7 | ||||
| -rwxr-xr-x | challenge-137/luca-ferrari/raku/ch-2.p6 | 18 |
8 files changed, 126 insertions, 0 deletions
diff --git a/challenge-137/luca-ferrari/blog-1.txt b/challenge-137/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..063e6c16bd --- /dev/null +++ b/challenge-137/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/01/PerlWeeklyChallenge137.html#task1 diff --git a/challenge-137/luca-ferrari/blog-2.txt b/challenge-137/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e036e1bd2d --- /dev/null +++ b/challenge-137/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/01/PerlWeeklyChallenge137.html#task2 diff --git a/challenge-137/luca-ferrari/blog-3.txt b/challenge-137/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..4385ac2151 --- /dev/null +++ b/challenge-137/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/01/PerlWeeklyChallenge137.html#task1pg diff --git a/challenge-137/luca-ferrari/blog-4.txt b/challenge-137/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..1a5ea48a41 --- /dev/null +++ b/challenge-137/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/01/PerlWeeklyChallenge137.html#task2pg diff --git a/challenge-137/luca-ferrari/postgresql/ch-1.sql b/challenge-137/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..798e9070fb --- /dev/null +++ b/challenge-137/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,61 @@ +/* +testdb=> select * from f_long_year(); +f_long_year +------------- +1903 +1908 +1914 +1920 +1925 +1931 +1936 +1942 +1948 +1953 +1959 +1964 +1970 +1976 +1981 +1987 +1992 +1998 +2004 +2009 +2015 +2020 +2026 +2032 +2037 +2043 +2048 +2054 +2060 +2065 +2071 +2076 +2082 +2088 +2093 +2099 +(36 rows) + + +*/ +CREATE OR REPLACE FUNCTION +f_long_year() +RETURNS SETOF int +AS $CODE$ +DECLARE + current_year int; +BEGIN + FOR current_year IN 1900 .. 2100 LOOP + IF date_part( 'week', make_Date( current_year, 12, 31 ) ) = 53 THEN + RETURN NEXT current_year; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-137/luca-ferrari/postgresql/ch-2.sql b/challenge-137/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..ed96d27b8f --- /dev/null +++ b/challenge-137/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,36 @@ +/* +testdb=> select * from f_lychrel( 59, true ); +INFO: Found 1111 after 3 iterations +f_lychrel +----------- +0 +*/ +CREATE OR REPLACE FUNCTION +f_lychrel( n int, verb boolean default false ) +RETURNS smallint +AS $CODE$ +DECLARE + result bigint := n; + iteration int := 0; +BEGIN + IF n < 10 OR n > 10000 THEN + RAISE 'n is out of bounds!'; + END IF; + + WHILE result < 10000000 AND iteration < 500 LOOP + iteration = iteration + 1; + result = result + reverse( result::text )::int; + IF result = reverse( result::text )::int THEN + IF verb THEN + RAISE INFO 'Found % after % iterations', result, iteration; + END IF; + + RETURN 0; + END IF; + + END LOOP; + + RETURN 1; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-137/luca-ferrari/raku/ch-1.p6 b/challenge-137/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..1c1d8d9166 --- /dev/null +++ b/challenge-137/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,7 @@ +#!raku + +sub MAIN() { + for 1900 .. 2100 -> $year { + $year.say if Date.new( '%04d-12-31'.sprintf( $year ) ).week-number == 53; + } +} diff --git a/challenge-137/luca-ferrari/raku/ch-2.p6 b/challenge-137/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..3a67328fad --- /dev/null +++ b/challenge-137/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#!raku + +sub MAIN( Int $n where { 10 <= $n <= 10000 }, Bool :$verbose = False ) { + + my ( $result, $iteration ) = $n,0; + while ( $result < 10_000_000 && $iteration < 500 ) { + $iteration++; + $result += $result.split( '' ).reverse.join; + if $result == $result.split( '' ).reverse.join { + '0'.say; + "Found $result after $iteration iterations".say if $verbose; + exit; + } + } + + '1'.say; + "Cannot find Lychrel number for $n".say if $verbose; +} |
