From b068e9279dccdeef9f4cbda7a8dffbdfc367518a Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Sun, 13 Sep 2020 04:16:37 +0200 Subject: #1 initial --- challenge-077/markus-holzer/raku/ch-1.raku | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-077/markus-holzer/raku/ch-1.raku diff --git a/challenge-077/markus-holzer/raku/ch-1.raku b/challenge-077/markus-holzer/raku/ch-1.raku new file mode 100644 index 0000000000..f4ef0295a9 --- /dev/null +++ b/challenge-077/markus-holzer/raku/ch-1.raku @@ -0,0 +1,41 @@ +use experimental :cached; + +unit sub MAIN( $N, $v = False ); + +my $start = now; + +with gather combine( zeckendorf( $N ) ) +{ + say "# of combinations: {.elems}"; + say "Calculated in { sprintf "%.3f", now - $start } seconds"; + if $v { .say for .sort( -*.elems ) } +} + +sub combine( @Z is copy ) is cached +{ + take @Z; + + my &insert = { my @x = @Z.clone; @x[$^i] = $^z; [@x.map: |*] }; + my &valid = { $^x.elems == $^x.unique.elems && @$^x !~~ @Z }; + + sink @Z + .map( &zeckendorf ) + .kv.map( &insert ) + .grep( &valid ) + .map( &combine ); +} + +sub zeckendorf( $n is copy ) is cached +{ + state @fib = [1, 1, * + * ... * > $N]; + + $n == 1 + ?? 1.List + !! eager gather for @fib.grep( * < $n ).reverse + { + if $_ <= $n { + take $_; + $n -= $_; + } + } +} -- cgit From fb891f3b1bbcd2da4c9399809737297ae4a9b087 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Sun, 13 Sep 2020 06:04:44 +0200 Subject: #1 initial --- challenge-077/markus-holzer/raku/ch-1.raku | 37 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/challenge-077/markus-holzer/raku/ch-1.raku b/challenge-077/markus-holzer/raku/ch-1.raku index f4ef0295a9..054dd51cb9 100644 --- a/challenge-077/markus-holzer/raku/ch-1.raku +++ b/challenge-077/markus-holzer/raku/ch-1.raku @@ -1,41 +1,42 @@ use experimental :cached; -unit sub MAIN( $N, $v = False ); +unit sub MAIN( Int $N where * > 0, Bool :$v = False ); my $start = now; -with gather combine( zeckendorf( $N ) ) +with my @combinatons = gather combine zeckendorf $N { - say "# of combinations: {.elems}"; - say "Calculated in { sprintf "%.3f", now - $start } seconds"; - if $v { .say for .sort( -*.elems ) } + say @combinatons.map( *.join( "," ) ).join( "\n" ) if $v; + say "# of combinations: {+@combinatons}"; + say "Calculated in { sprintf "%.3f", now - $start } seconds" } -sub combine( @Z is copy ) is cached +sub combine( @Z ) is cached { - take @Z; + my &valid = -> $result { + $result.elems == $result.unique.elems && $result !~~ @Z }; + + my &insert = -> $where, $what { + my @x = @Z.clone; @x.splice( $where, 1, |$what ); @x }; - my &insert = { my @x = @Z.clone; @x[$^i] = $^z; [@x.map: |*] }; - my &valid = { $^x.elems == $^x.unique.elems && @$^x !~~ @Z }; + take @Z; sink @Z .map( &zeckendorf ) .kv.map( &insert ) .grep( &valid ) - .map( &combine ); + .map( &combine ) } sub zeckendorf( $n is copy ) is cached { state @fib = [1, 1, * + * ... * > $N]; - $n == 1 - ?? 1.List - !! eager gather for @fib.grep( * < $n ).reverse - { + my &do-zeckendorf = { + eager gather for @fib.grep( * < $n ).reverse { if $_ <= $n { take $_; - $n -= $_; - } - } -} + $n -= $_; }}} + + $n == 1 ?? 1.List !! do-zeckendorf +} \ No newline at end of file -- cgit From 0a43b3daca0f6b7b6cfaef12371256a84b1b9eb2 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Sun, 13 Sep 2020 06:08:47 +0200 Subject: tabs??? --- challenge-077/markus-holzer/raku/ch-1.raku | 40 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/challenge-077/markus-holzer/raku/ch-1.raku b/challenge-077/markus-holzer/raku/ch-1.raku index 054dd51cb9..44b5af60d4 100644 --- a/challenge-077/markus-holzer/raku/ch-1.raku +++ b/challenge-077/markus-holzer/raku/ch-1.raku @@ -6,37 +6,37 @@ my $start = now; with my @combinatons = gather combine zeckendorf $N { - say @combinatons.map( *.join( "," ) ).join( "\n" ) if $v; - say "# of combinations: {+@combinatons}"; - say "Calculated in { sprintf "%.3f", now - $start } seconds" + say @combinatons.map( *.join( "," ) ).join( "\n" ) if $v; + say "# of combinations: {+@combinatons}"; + say "Calculated in { sprintf "%.3f", now - $start } seconds" } sub combine( @Z ) is cached { - my &valid = -> $result { - $result.elems == $result.unique.elems && $result !~~ @Z }; + my &valid = -> $result { + $result.elems == $result.unique.elems && $result !~~ @Z }; - my &insert = -> $where, $what { - my @x = @Z.clone; @x.splice( $where, 1, |$what ); @x }; + my &insert = -> $where, $what { + my @x = @Z.clone; @x.splice( $where, 1, |$what ); @x }; - take @Z; + take @Z; - sink @Z - .map( &zeckendorf ) - .kv.map( &insert ) - .grep( &valid ) - .map( &combine ) + sink @Z + .map( &zeckendorf ) + .kv.map( &insert ) + .grep( &valid ) + .map( &combine ) } sub zeckendorf( $n is copy ) is cached { - state @fib = [1, 1, * + * ... * > $N]; + state @fib = [1, 1, * + * ... * > $N]; - my &do-zeckendorf = { - eager gather for @fib.grep( * < $n ).reverse { - if $_ <= $n { - take $_; - $n -= $_; }}} + my &do-zeckendorf = { + eager gather for @fib.grep( * < $n ).reverse { + if $_ <= $n { + take $_; + $n -= $_; }}} - $n == 1 ?? 1.List !! do-zeckendorf + $n == 1 ?? 1.List !! do-zeckendorf } \ No newline at end of file -- cgit From 1336e9d06871f76059da4f2b8f0c685d2042600f Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Sun, 13 Sep 2020 06:16:35 +0200 Subject: semicolons, comments, typos --- challenge-077/markus-holzer/raku/ch-1.raku | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/challenge-077/markus-holzer/raku/ch-1.raku b/challenge-077/markus-holzer/raku/ch-1.raku index 44b5af60d4..4f22b7501a 100644 --- a/challenge-077/markus-holzer/raku/ch-1.raku +++ b/challenge-077/markus-holzer/raku/ch-1.raku @@ -14,14 +14,12 @@ with my @combinatons = gather combine zeckendorf $N sub combine( @Z ) is cached { my &valid = -> $result { - $result.elems == $result.unique.elems && $result !~~ @Z }; + $result.elems == $result.unique.elems && $result !~~ @Z } my &insert = -> $where, $what { - my @x = @Z.clone; @x.splice( $where, 1, |$what ); @x }; + my @x = @Z.clone; @x.splice( $where, 1, |$what ); @x } - take @Z; - - sink @Z + take @Z and sink @Z .map( &zeckendorf ) .kv.map( &insert ) .grep( &valid ) -- cgit From beb7abf2eb72adb99b7d83cfc84be0e73fcb1625 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Sun, 13 Sep 2020 06:18:14 +0200 Subject: semicolons, comments, typos --- challenge-077/markus-holzer/raku/ch-1.raku | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/challenge-077/markus-holzer/raku/ch-1.raku b/challenge-077/markus-holzer/raku/ch-1.raku index 4f22b7501a..7337c6ca01 100644 --- a/challenge-077/markus-holzer/raku/ch-1.raku +++ b/challenge-077/markus-holzer/raku/ch-1.raku @@ -1,13 +1,22 @@ +# +# D:\Projekte\Perl6\perlweeklychallenge-club\challenge-077\markus-holzer\raku>raku ch-1.raku 123456789 +# Number of combinations: 3230 +# Calculated in 8.736 seconds +# +# Let me know if yours is faster +# See https://encyclopediaofmath.org/wiki/Zeckendorf_representation +# + use experimental :cached; unit sub MAIN( Int $N where * > 0, Bool :$v = False ); my $start = now; -with my @combinatons = gather combine zeckendorf $N +with my @combinations = gather combine zeckendorf $N { - say @combinatons.map( *.join( "," ) ).join( "\n" ) if $v; - say "# of combinations: {+@combinatons}"; + say @combinations.map( *.join( "," ) ).join( "\n" ) if $v; + say "Number of combinations: {+@combinations}"; say "Calculated in { sprintf "%.3f", now - $start } seconds" } @@ -34,7 +43,7 @@ sub zeckendorf( $n is copy ) is cached eager gather for @fib.grep( * < $n ).reverse { if $_ <= $n { take $_; - $n -= $_; }}} + $n -= $_ }}} $n == 1 ?? 1.List !! do-zeckendorf } \ No newline at end of file -- cgit