aboutsummaryrefslogtreecommitdiff
path: root/challenge-153
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-02-27 23:32:45 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-02-27 23:32:45 +0000
commit649b9ea422797ad2410cdd0c93a4bd1dfed89a8f (patch)
tree07bb051843fdddfc90186c971eafaa75c7170f46 /challenge-153
parent50db49be648308c78e385ca64683b27f85cc9668 (diff)
downloadperlweeklychallenge-club-649b9ea422797ad2410cdd0c93a4bd1dfed89a8f.tar.gz
perlweeklychallenge-club-649b9ea422797ad2410cdd0c93a4bd1dfed89a8f.tar.bz2
perlweeklychallenge-club-649b9ea422797ad2410cdd0c93a4bd1dfed89a8f.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-153')
-rwxr-xr-xchallenge-153/colin-crain/perl/ch-1.pl89
-rwxr-xr-xchallenge-153/colin-crain/perl/ch-2.pl109
-rwxr-xr-xchallenge-153/colin-crain/raku/ch-1.raku15
-rwxr-xr-xchallenge-153/colin-crain/raku/ch-2.raku25
4 files changed, 238 insertions, 0 deletions
diff --git a/challenge-153/colin-crain/perl/ch-1.pl b/challenge-153/colin-crain/perl/ch-1.pl
new file mode 100755
index 0000000000..08166fd717
--- /dev/null
+++ b/challenge-153/colin-crain/perl/ch-1.pl
@@ -0,0 +1,89 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# facts-left-to-the-reader.pl
+#
+# Left Factorials
+# Submitted by: Mohammad S Anwar
+# Write a script to compute Left Factorials of 1 to 10. Please
+# refer OEIS A003422 for more information.
+#
+# Expected Output:
+# 1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114
+#
+# analysis:
+# I found the meaning of the left factorial unusually elusive
+# for this task for some reason. Maybe because I wasn't fully
+# awake, and was distracted, but however I had it figured in my
+# head the answer to my confusion was, surprising no-one,
+# considerably simpler than I made out.
+#
+# Call it being unable to see the forest from the technical
+# minutia.
+#
+# In any case I will make a stab at my own descriptive
+# definition:
+#
+# The sum of all values in the sequence of factorials to the
+# left of, but not including, the value in question. So L(4) =
+# 3! + 2! + 1! + 0!
+#
+# Another clver bit of nomaclature I saw was to denotate this
+# as:
+#
+# !5 = 0! + 1! + 2! + 3! + 4!
+#
+# Note the exclamation is placed tp the left for the left
+# factorial.
+#
+# 0! is a funny concept that for the purposes of disucssion
+# equals 1, and is arrived at by considering the sequence as a
+# whole.
+#
+# method:
+# To compute a list of the first ten, or whatever number, of
+# left factorials, we will need to compute factorials and
+# maintain a running sum sequence of those preceding. To keep
+# things interesting we'll input an arbitrary number request
+# for the sequence instead of fixing the request to 10.
+#
+# Things blow up quickly along the factorial number line.
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+use bigint;
+
+my $num = shift // 50;
+
+my @left = (0,1);
+my $fact = 1;
+my $count = 1;
+
+while ($count < $num) {
+ $fact *= $count;
+ push @left, $left[-1] + $fact;
+ $count++;
+}
+
+say for @left;
+
+
+
+
+
+
+
+# use Test::More;
+#
+# is
+#
+# done_testing();
diff --git a/challenge-153/colin-crain/perl/ch-2.pl b/challenge-153/colin-crain/perl/ch-2.pl
new file mode 100755
index 0000000000..d38ff1d237
--- /dev/null
+++ b/challenge-153/colin-crain/perl/ch-2.pl
@@ -0,0 +1,109 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# factory-people-in-a-factory-world.pl
+#
+# Factorions
+# Submitted by: Mohammad S Anwar
+# You are given an integer, $n.
+#
+# Write a script to figure out if the given integer is factorion.
+#
+# A factorion is a natural number that equals the sum of the
+# factorials of its digits.
+#
+# Example 1:
+# Input: $n = 145
+# Output: 1
+#
+# Since 1! + 4! + 5! => 1 + 24 + 120 = 145
+#
+# Example 2:
+# Input: $n = 123
+# Output: 0
+#
+# Since 1! + 2! + 3! => 1 + 2 + 6 <> 123
+
+
+# method:
+#
+# This is another example of a multi-part puzzle that at first
+# seems more complicated than it turns out to be in the end.
+#
+# Which is to say the factorial part may at first glance seem
+# daunting, but on further reflection we're only asking for the
+# factorial for any single digit. So there's ten of these, and
+# those are all the values we will ever need.
+#
+# In fact, if we make a 1:1 mapping of the digits to their
+# corresponding factorials, we can divide a number down into
+# its digits and substitute in the factorials before summing.
+#
+# At that point we only need to check for equality between the
+# value and its factorial digit-sum.
+#
+# Because we only need the ten factorial values, it would make
+# sense to hard-code them into an array or hash lookup, indexed
+# against the digit values 0-9. But we're not going to do that;
+# we walk our own path in this crazy, mixed up world. Instead
+# we're going to make a short factorial function and calculate
+# the sequence from 0 through 9. Because YOLO that's why. Party
+# on, Garth, party on.
+
+# observations:
+#
+# there are a grand total of 4 factorions: 1, 2, 145 and 40585
+#
+# there can be no factorials above 7 digits because the
+# smallest 8-digit number — 10,000,000 — is larger than the sum
+# of the largest 8-gigit number: 9! + 9! + 9! + 9! + 9! + 9! +
+# 9! + 9! = 2,903,040
+#
+# So no 8-digit number can be large enough to sum to its own
+# value. By this same reasoning no larger number can either. So
+# having manually sifted through values up to 10,000,000 we can
+# state these are the only instances.
+#
+# This is indeed the case, per listing in the OEIS:
+#
+# A014080 Factorions: equal to the sum of the factorials
+# of their digits in base 10
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+
+our @fact = (1);
+push @fact, $fact[-1] * $_ for 1..9;
+
+sub is_factorion($n = 145, $ds = 0) {
+ $ds += $_ for map { $fact[$_] } split //, $n;
+
+ return $ds == $n
+}
+
+
+for (1..10000000) {
+ say if is_factorion($_);
+}
+
+# say for grep { is_factorion($_) } 1..10_000_000 ;
+
+# use Test::More;
+#
+# is is_factorion(145), 1, 'ex-1';
+# is is_factorion(123), 0, 'ex-2';
+#
+#
+# done_testing();
+
+# 362,880
diff --git a/challenge-153/colin-crain/raku/ch-1.raku b/challenge-153/colin-crain/raku/ch-1.raku
new file mode 100755
index 0000000000..5680f4ca75
--- /dev/null
+++ b/challenge-153/colin-crain/raku/ch-1.raku
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl6
+#
+#
+# facts-left-to-the-reader.raku
+#
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN ( $num = 50 ) ;
+
+say [+] map { [*] 1..$_ }, ^$_ for 0..$num;
diff --git a/challenge-153/colin-crain/raku/ch-2.raku b/challenge-153/colin-crain/raku/ch-2.raku
new file mode 100755
index 0000000000..9f6f450ab0
--- /dev/null
+++ b/challenge-153/colin-crain/raku/ch-2.raku
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl6
+#
+#
+# factory-people-in-a-factory-world.raku
+#
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN ( $n = 145 ) ;
+
+
+my @f = map { [*] 1..$_ }, 0..9;
+
+sub is_factorion($n, $ds = 0) {
+ $n == $n.comb
+ .map({@f[$_]})
+ .sum
+}
+
+.say for (^10000000).grep: {is_factorion $_}
+