diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-08 22:51:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-08 22:51:53 +0100 |
| commit | 5754a3612b331f7cfc7545962fcea928e2c3b176 (patch) | |
| tree | 761589c24e956c9fabc3038dd220fe4c06fae284 | |
| parent | fbe6534dfbb8a2899f6ff97043d11a64be7b49ba (diff) | |
| parent | e4536342eb74eb731807602c65588d0895ef9505 (diff) | |
| download | perlweeklychallenge-club-5754a3612b331f7cfc7545962fcea928e2c3b176.tar.gz perlweeklychallenge-club-5754a3612b331f7cfc7545962fcea928e2c3b176.tar.bz2 perlweeklychallenge-club-5754a3612b331f7cfc7545962fcea928e2c3b176.zip | |
Merge pull request #6228 from simbabque/challenge-168
challenge 168, in Perl
| -rw-r--r-- | challenge-168/julien-fiegehenn/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-168/julien-fiegehenn/perl/ch-2.pl | 78 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-168/julien-fiegehenn/perl/ch-1.pl b/challenge-168/julien-fiegehenn/perl/ch-1.pl new file mode 100644 index 0000000000..2e331a2415 --- /dev/null +++ b/challenge-168/julien-fiegehenn/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature 'say'; + +# Task 1: Perrin Prime +# Submitted by: Roger Bell_West +# The Perrin sequence is defined to start with [3, 0, 2]; after that, term N is the sum of terms N-2 and N-3. (So it continues 3, 2, 5, 5, 7, ….) + +# A Perrin prime is a number in the Perrin sequence which is also a prime number. + +# Calculate the first 13 Perrin Primes. + +# f(13) = [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977] + +sub is_prime { + my $n = shift; + return 0 if $n < 2; + return 1 if $n == 2; + return 0 if $n % 2 == 0; + for (my $i = 3; $i <= sqrt($n); $i += 2) { + return 0 if $n % $i == 0; + } + return 1; +} + +sub perrin_primes { + my $n = shift; + my %primes = map { $_ => 1 } 2, 3; + my @perrin = (3, 0, 2); + my $i = 3; + while (keys %primes < $n) { + $perrin[$i] = $perrin[$i - 2] + $perrin[$i - 3]; + if (is_prime($perrin[$i])) { + $primes{$perrin[$i]} = 1; + } + $i++; + } + return sort { $a <=> $b } keys %primes; +} + +my @primes = perrin_primes(13); +say "@primes"; + +__END__ +use Test::More; +is_deeply \@primes, [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977]; +done_testing;
\ No newline at end of file diff --git a/challenge-168/julien-fiegehenn/perl/ch-2.pl b/challenge-168/julien-fiegehenn/perl/ch-2.pl new file mode 100644 index 0000000000..9e9812d039 --- /dev/null +++ b/challenge-168/julien-fiegehenn/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature 'say'; + +# Task 2: Home Prime +# Submitted by: Mohammad S Anwar +# You are given an integer greater than 1. + +# Write a script to find the home prime of the given number. + +# In number theory, the home prime HP(n) of an integer n greater than 1 is the prime number obtained by repeatedly factoring the increasing concatenation of prime factors including repetitions. + +# Further information can be found on Wikipedia and OEIS. + +# Example +# As given in the Wikipedia page, + +# HP(10) = 773, as 10 factors as 2×5 yielding HP10(1) = 25, 25 factors as 5×5 yielding HP10(2) = HP25(1) = 55, 55 = 5×11 implies HP10(3) = HP25(2) = HP55(1) = 511, and 511 = 7×73 gives HP10(4) = HP25(3) = HP55(2) = HP511(1) = 773, a prime number. + +# written with github copilot + +# calculate the prime factors of a number +sub prime_factors { + my $n = shift; + my @factors = (); + my $i = 2; + while ($n > 1) { + while ($n % $i == 0) { + push @factors, $i; + $n /= $i; + } + $i++; + } + return @factors; +} + +sub is_prime { + my $n = shift; + return 0 if $n < 2; + return 1 if $n == 2; + return 0 if $n % 2 == 0; + for (my $i = 3; $i <= sqrt($n); $i += 2) { + return 0 if $n % $i == 0; + } + return 1; +} + +# calculate the home prime of a number +# In number theory, the home prime HP(n) of an integer n greater than 1 is the prime number obtained by repeatedly factoring the increasing concatenation of prime factors including repetitions. +sub home_prime { + my $n = shift; + + return $n if is_prime($n); + return home_prime(join '', prime_factors($n)); +} + +# a version of home_prime that produces debug output at every step +sub home_prime_debug { + my $n = shift; + + say "n = $n"; + return $n if is_prime($n); + my @factors = prime_factors($n); + say "factors = @factors"; + return home_prime_debug(join '', @factors); +} + +__END__ + +use Test::More; +is_deeply [prime_factors(10)], [2, 5]; +is_deeply [prime_factors(13)], [13]; +is_deeply [prime_factors(14)], [2, 7]; + +is home_prime(10), 773; +is home_prime_debug(10), 773; +done_testing;
\ No newline at end of file |
