aboutsummaryrefslogtreecommitdiff
path: root/challenge-153
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2022-02-24 18:38:10 -0500
committerrir <rirans@comcast.net>2022-02-24 21:34:08 -0500
commit0ec4adb229a019d5223b4cc8f1dc9c20b47bf530 (patch)
tree5ea8b01886620ca087711b035961729383f7a911 /challenge-153
parent3330f2f0796e04c4faf3d0e5d5745751b54799ca (diff)
downloadperlweeklychallenge-club-0ec4adb229a019d5223b4cc8f1dc9c20b47bf530.tar.gz
perlweeklychallenge-club-0ec4adb229a019d5223b4cc8f1dc9c20b47bf530.tar.bz2
perlweeklychallenge-club-0ec4adb229a019d5223b4cc8f1dc9c20b47bf530.zip
Factorions
Diffstat (limited to 'challenge-153')
-rw-r--r--challenge-153/0rir/raku/ch-2.raku53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-153/0rir/raku/ch-2.raku b/challenge-153/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..b6965b5958
--- /dev/null
+++ b/challenge-153/0rir/raku/ch-2.raku
@@ -0,0 +1,53 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab
+use v6.d;
+use MONKEY-SEE-NO-EVAL;
+
+constant \TEST=True;
+
+# Factorions
+
+$*ERR.print: "Warning: Postfix bang binds tighter than unary minus,\n"
+ ~ " Use a function for general use.\n";
+
+my sub postfix:<!> ( Int $i --> Int ) {
+ state @f = 1,1,2,6;
+ while @f.end < $i {
+ @f.push: @f.end × (@f[@f.end -1] + @f[@f.end])
+ }
+ @f[$i];
+}
+
+my $n = (1..100000).pick;
+
+factorion( $n);
+
+exit unless TEST;
+
+sub factorion ( Int $n, Bool :$QUIET --> Bool) {
+ my Str $explan = (($n.split( '', :skip-empty)).join: '! + ') ~ '!';
+ my $output = EVAL( $explan);
+ my $ation = join ' + ',
+ ($explan.split( ' + ', :skip-empty)).map: { EVAL $_ };
+
+ unless $QUIET {
+ say "Input: \$n = $n\n"
+ ~ "Output: ", ($output==$n).Int(), "\n\n"
+ ~ " Since $explan => $ation = $output";
+ }
+ $output == $n;
+}
+
+use Test;
+
+my @good = 1, 2, 145, 40585;
+
+plan 50000;
+
+for @good -> $x {
+ is factorion($x), $x == @good.any, "is a factorion";
+}
+for 0 .. 49999 {
+ next if $_ == @good.any;
+ is factorion($_, :QUIET ), False, 'is not a factorion';
+}