aboutsummaryrefslogtreecommitdiff
path: root/challenge-158
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-04 00:34:14 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-04 00:34:14 +0100
commit5827a5edfc2a6cd770f8d715d65eb43141ae58a8 (patch)
tree3876cce21af331817e408bacb835077e79a2fbae /challenge-158
parentf280ee43d6621f3547988b1ed4df64dc5a52e905 (diff)
downloadperlweeklychallenge-club-5827a5edfc2a6cd770f8d715d65eb43141ae58a8.tar.gz
perlweeklychallenge-club-5827a5edfc2a6cd770f8d715d65eb43141ae58a8.tar.bz2
perlweeklychallenge-club-5827a5edfc2a6cd770f8d715d65eb43141ae58a8.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-158')
-rwxr-xr-xchallenge-158/colin-crain/perl/ch-1.pl111
-rwxr-xr-xchallenge-158/colin-crain/perl/ch-2.pl56
-rwxr-xr-xchallenge-158/colin-crain/raku/ch-1.raku18
-rwxr-xr-xchallenge-158/colin-crain/raku/ch-2.raku19
4 files changed, 204 insertions, 0 deletions
diff --git a/challenge-158/colin-crain/perl/ch-1.pl b/challenge-158/colin-crain/perl/ch-1.pl
new file mode 100755
index 0000000000..127ce51645
--- /dev/null
+++ b/challenge-158/colin-crain/perl/ch-1.pl
@@ -0,0 +1,111 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# add-another-prime-to-the-pile.pl
+#
+# Additive Primes
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to find out all Additive Primes <= 100.
+#
+# Additive primes are prime numbers for which the sum of their
+# decimal digits are also primes.
+#
+# Output
+# 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89
+#
+# method
+#
+# You know, sometimes when I do these problems I like to
+# pretend that I'm not good at math. Which is to say choose to
+# I regard the problem as a computing problem, rather than a
+# math problem, or or even more broadly a problem in search of
+# a structure that will solve it, rather than any specific
+# answer.
+#
+# Or put another way solve a more generalized version with more
+# unknowns.
+#
+# In this problem we need a bunch of prime numbers. A quick bit
+# of mathematical reasoning tells us
+#
+# 1. that 100 is not a additive prime, as it adds to 1
+# 2. the that largest digit-sum we'll need to deal with is
+# for the value 99, where 9 + 9 = 18.
+# 3. thus, using preexisting knowledge we only need to consider
+# the primes 2, 3, 5, 7, 11, 13, and 17
+#
+# But you know what? We're going to pretend we don't know that,
+# and instead compute as many primes as required. Along the way
+# we'll make the upper limit user-configurable, which will
+# allow us to find the number 102 should we wish, which by the
+# looks of it is an addative prime.
+#
+# What we'll need to do is make a lookup of primes that
+# contains every prime less than the largest value we have
+# tried to date. The digit sum will always be less than or
+# equal to the value. If we exceed the last prime found, we'll
+# expand our bounds in the list of found primes as required.
+#
+# We can then construct a lookup hash to check for primality.
+#
+# Then we can break apart the number into its digits and sum
+# them, and do a quick lookup to see whether the result is
+# prime.
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+use List::Util qw( sum0 );
+
+my @primes;
+my %lookup;
+my @additives;
+
+## make a closed prime iterator function
+## modified to add to the prime lookup hash
+my $pg = sub {
+ if ( @primes < 2 ) {
+ push @primes, @primes == 0 ? 2 : 3;
+ $lookup{ $primes[-1] } = 1;
+ return $primes[-1];
+ }
+
+ my $candidate = $primes[-1] ;
+ CANDIDATE: while ( $candidate += 2 ) {
+ my $sqrt_candidate = sqrt( $candidate );
+ for my $test ( @primes ) {
+ next CANDIDATE if $candidate % $test == 0;
+ last if $test > $sqrt_candidate;
+ }
+ push @primes, $candidate;
+ $lookup{ $candidate } = 1;
+ return $candidate;
+ }
+} ;
+$pg->(); ## init the prime array with 2
+
+my $limit = shift @ARGV // 100;
+
+## look at the numbers 1 to $limit,
+## checking each for primality and primality in the digit sum
+for ( 1..$limit ) {
+ $pg->() if $primes[-1] <= $_ ;
+ next unless $lookup{$_} ;
+ my $sum = sum0( split //, $_ );
+ push @additives, $_ if $lookup{$sum};
+}
+
+say join ', ', @additives;
+
+
+
+
diff --git a/challenge-158/colin-crain/perl/ch-2.pl b/challenge-158/colin-crain/perl/ch-2.pl
new file mode 100755
index 0000000000..b20bfc5f96
--- /dev/null
+++ b/challenge-158/colin-crain/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# cubans-in-their-prime.pl
+#
+# First Series Cuban Primes
+# Submitted by: Mohammad S Anwar
+# Write a script to compute first series Cuban Primes <= 1000.
+# Please refer wikipedia page for more informations.
+#
+# Output:
+# 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919.
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+sub nth_series_cubic ( $dif ) {
+ my @c;
+ my $x;
+ for my $y ( 1..100 ) {
+ $x = $y + $dif;
+ push @c, ($x**3 - $y**3) / ($x - $y) ;
+ }
+ return @c;
+}
+
+sub is_prime ($num) {
+ for ( 2..sqrt $num ) {
+ return 0 unless $num % $_;
+ }
+ return 1;
+}
+
+## output section
+my $out;
+for my $diff ( 1..10 ) {
+ $out .= sprintf " %3s | %s\n",
+ $diff,
+ join ' ', grep { is_prime($_) && $_ <= 1000 } nth_series_cubic( $diff ) ;
+}
+
+say<<~"END";
+ series | sequence
+ -------+------------------------------------------
+ $out
+ END
+
diff --git a/challenge-158/colin-crain/raku/ch-1.raku b/challenge-158/colin-crain/raku/ch-1.raku
new file mode 100755
index 0000000000..f89ff21663
--- /dev/null
+++ b/challenge-158/colin-crain/raku/ch-1.raku
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl6
+#
+#
+# .raku
+#
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN () ;
+
+put (^100).grep(*.is-prime )
+ .grep(*.comb.sum.is-prime);
+
+
diff --git a/challenge-158/colin-crain/raku/ch-2.raku b/challenge-158/colin-crain/raku/ch-2.raku
new file mode 100755
index 0000000000..164b06d769
--- /dev/null
+++ b/challenge-158/colin-crain/raku/ch-2.raku
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl6
+#
+#
+# .raku
+#
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN ( $limit = 1000) ;
+
+say gather for 1..* -> $x {
+ my $n = 3 * $x² + 3 * $x + 1;
+ last if $n > $limit;
+ take $n if $n.is-prime;
+}