diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-02-27 23:36:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-27 23:36:52 +0000 |
| commit | a11540238bb61ad8324186d86552d30adb522879 (patch) | |
| tree | fc59635189ca99ef46056dcc2e2771ac85351f43 | |
| parent | 649b9ea422797ad2410cdd0c93a4bd1dfed89a8f (diff) | |
| parent | ba5ba7520792ff44cdff46d91da7cf5d8530e4db (diff) | |
| download | perlweeklychallenge-club-a11540238bb61ad8324186d86552d30adb522879.tar.gz perlweeklychallenge-club-a11540238bb61ad8324186d86552d30adb522879.tar.bz2 perlweeklychallenge-club-a11540238bb61ad8324186d86552d30adb522879.zip | |
Merge pull request #5715 from adamcrussell/challenge-153
initial commit
| -rw-r--r-- | challenge-153/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-153/adam-russell/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-153/adam-russell/perl/ch-2.pl | 22 |
3 files changed, 48 insertions, 0 deletions
diff --git a/challenge-153/adam-russell/blog.txt b/challenge-153/adam-russell/blog.txt new file mode 100644 index 0000000000..3e0711da3f --- /dev/null +++ b/challenge-153/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/02/27 diff --git a/challenge-153/adam-russell/perl/ch-1.pl b/challenge-153/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..3e5b1e9a27 --- /dev/null +++ b/challenge-153/adam-russell/perl/ch-1.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +## +# Write a script to determine the first ten members of the Left Factorials sequence. +## +use POSIX; +use constant UPPER_BOUND => INT_MAX/1000; + +sub left_factorials_sieve{ + my($n) = @_; + my @sieve = (0 .. UPPER_BOUND); + my @left_factorials; + my $x = 2; + { + my @sieve_indices = grep { $_ <= $x || $_ % $x == 0 } 0 .. @sieve - 1; + @sieve = map{ $sieve[$_] } @sieve_indices; + $x++; + redo if $x <= $n; + } + return @sieve[1 .. @sieve - 1]; +} + +MAIN:{ + print join(", ", left_factorials_sieve(10)) . "\n"; +}
\ No newline at end of file diff --git a/challenge-153/adam-russell/perl/ch-2.pl b/challenge-153/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..7ab9492746 --- /dev/null +++ b/challenge-153/adam-russell/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +## +# Write a script to figure out if the given integer is a factorion. +## +use boolean; + +sub factorial{ + my($n) = @_; + return 1 if $n == 1; + $n * factorial($n - 1); +} + +sub is_factorion{ + my($n) = @_; + return boolean($n == unpack("%32I*", pack("I*", map {factorial($_)} split(//, $n)))); +} + +MAIN:{ + print is_factorion(145) . "\n"; + print is_factorion(123) . "\n"; +}
\ No newline at end of file |
