aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-08-24 02:43:37 +0100
committerGitHub <noreply@github.com>2019-08-24 02:43:37 +0100
commit6acdf73acaa0da4513a594fc35473606a155ae06 (patch)
tree3e9e4abd1717f5a74e6b378062cec5a43d0b4a99
parent59359bd9f6e92e586695a072ff7d57724f25c041 (diff)
parent431096b0d68b442180604c63842eeaa9d93edbe2 (diff)
downloadperlweeklychallenge-club-6acdf73acaa0da4513a594fc35473606a155ae06.tar.gz
perlweeklychallenge-club-6acdf73acaa0da4513a594fc35473606a155ae06.tar.bz2
perlweeklychallenge-club-6acdf73acaa0da4513a594fc35473606a155ae06.zip
Merge pull request #545 from jmaslak/joelle-22-1-1
Joelle's solutions to 22.1 in Perl6 & Perl5
-rwxr-xr-xchallenge-022/joelle-maslak/perl5/ch-1.pl71
-rwxr-xr-xchallenge-022/joelle-maslak/perl6/ch-1.p617
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-022/joelle-maslak/perl5/ch-1.pl b/challenge-022/joelle-maslak/perl5/ch-1.pl
new file mode 100755
index 0000000000..a3d187f508
--- /dev/null
+++ b/challenge-022/joelle-maslak/perl5/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+use v5.22;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+
+use Memoize;
+
+die("Usage: $0 <number of sequence elements>") unless @ARGV <= 1;
+my $length = $ARGV[0] // 10;
+
+# Reused some of solution 15 (the prime function)
+
+sub MAIN() {
+ say "Primes:";
+ say join( " ", $_->@* ) for ( sexy($length) );
+ return;
+}
+
+sub sexy($count) {
+ my @return;
+
+ my $i = 0;
+ while ( scalar(@return) < $count ) {
+ $i++;
+
+ if ( perl_isprime( prime($i) + 6 ) ) {
+ push @return, [ prime($i), prime($i) + 6 ];
+ }
+ }
+
+ return @return;
+}
+
+MAIN();
+
+sub prime($i) {
+ state @primes = ( 2, 3 );
+
+ prime( $i - 1 ) if $i > scalar(@primes);
+ return $primes[$i] if $i < scalar(@primes);
+
+ my $last = $primes[-1];
+ do { $last += 2 } until perl_isprime($last);
+
+ push @primes, $last;
+ return $last;
+}
+
+memoize('perl_isprime');
+
+sub perl_isprime($i) {
+ my $sqrt = int( sqrt($i) );
+
+ if ( $i <= 2 ) { return 0; } # negatives are wrong, at least for us
+ if ( $i % 2 == 0 ) { return 0; }
+
+ my $div = 3;
+ while ( $div <= $sqrt ) {
+ if ( $i % $div == 0 ) { return 0; }
+
+ $div += 2; # Test just evens
+ }
+
+ return 1;
+}
diff --git a/challenge-022/joelle-maslak/perl6/ch-1.p6 b/challenge-022/joelle-maslak/perl6/ch-1.p6
new file mode 100755
index 0000000000..c0b7cab189
--- /dev/null
+++ b/challenge-022/joelle-maslak/perl6/ch-1.p6
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN(UInt:D $count? where * > 0 = 10) {
+ my @primes = (^∞).grep( *.is-prime );
+
+ my @sexy = lazy gather {
+ for ^∞ -> $i {
+ take @primes[$i] => @primes[$i] + 6 if (@primes[$i]+6).is-prime;
+ };
+ }
+
+ say "Primes:";
+ say "{$_.key} {$_.value}" for @sexy[^$count];
+}
+
+