diff options
| -rw-r--r-- | challenge-158/andrezgz/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-158/andrezgz/ch-2.pl | 38 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-158/andrezgz/ch-1.pl b/challenge-158/andrezgz/ch-1.pl new file mode 100644 index 0000000000..958275ff9f --- /dev/null +++ b/challenge-158/andrezgz/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-158/ +# TASK #1 > Additive Primes +# +# 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 + +use strict; +use warnings; +use feature 'say'; + +my @additive; + +for my $n (2..100) { + next unless is_prime($n); + my $sum = 0; + $sum += $_ for split //, $n; + push @additive, $n if is_prime($sum); +} + +say join ', ', @additive; + +sub is_prime { + my $n = shift; + #every composite number has a prime factor less than or equal to its square root. + for (2 .. sqrt $n) { + return 0 if $n % $_ == 0; + } + return 1; +} + +__END__ + +$ ./ch-1.pl +2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89 diff --git a/challenge-158/andrezgz/ch-2.pl b/challenge-158/andrezgz/ch-2.pl new file mode 100644 index 0000000000..6f25f781c3 --- /dev/null +++ b/challenge-158/andrezgz/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-158/ +# Task #2 > First Series Cuban Primes +# +# Write a script to compute first series Cuban Primes <= 1000. +# +# Please refer wikipedia page for more informations. +# https://en.wikipedia.org/wiki/Cuban_prime +# +# Output +# 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919. + +use strict; +use warnings; +use feature 'say'; + +my $y = 0; +my @cuban; + +while (++$y) { + my $n = 3*$y**2+3*$y+1; + last if $n > 1000; + push @cuban, $n if is_prime($n); +} + +say join ', ', @cuban; + +sub is_prime { + my $n = shift; + #every composite number has a prime factor less than or equal to its square root. + return 1 == grep {$n % $_ == 0} (1 .. sqrt $n); +} + +__END__ + +$ ./ch-2.pl +7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919 |
