diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2022-03-08 13:30:04 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2022-03-08 13:30:04 +0000 |
| commit | 7f4a0ea7e00120e1884f760f52bb3dda647531f4 (patch) | |
| tree | 7a7febd88267c28c4f14723c1e38761cf77de544 /challenge-155 | |
| parent | 533e5d6075971e41e6dd166244895e2bd25ad912 (diff) | |
| download | perlweeklychallenge-club-7f4a0ea7e00120e1884f760f52bb3dda647531f4.tar.gz perlweeklychallenge-club-7f4a0ea7e00120e1884f760f52bb3dda647531f4.tar.bz2 perlweeklychallenge-club-7f4a0ea7e00120e1884f760f52bb3dda647531f4.zip | |
Task 1 & 2
Diffstat (limited to 'challenge-155')
| -rwxr-xr-x | challenge-155/perlboy1967/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-155/perlboy1967/perl/ch-2.pl | 69 |
2 files changed, 116 insertions, 0 deletions
diff --git a/challenge-155/perlboy1967/perl/ch-1.pl b/challenge-155/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..455ee92dd2 --- /dev/null +++ b/challenge-155/perlboy1967/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 155 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-155/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +TASK #1 › Fortunate Numbers +Submitted by: Mohammad S Anwar + +Write a script to produce first 8 Fortunate Numbers (unique and sorted). + +According to Wikipedia + + || A Fortunate number, named after Reo Fortune, is the smallest integer + || m > 1 such that, for a given positive integer n, pn# + m is a prime + || number, where the primorial pn# is the product of the first n prime numbers. + +=cut + +use v5.16; +use bigint; + +use Math::Primality qw(next_prime); + +my $N = 8; + +my %fortunateNumbers; +my $fNfound = 0; +my @primes; +my @product; + +while ($N > 0) { + push(@primes, next_prime($primes[-1] // 0)); + push(@product, $primes[-1] * ($product[-1] // 1)); + + my $fN = next_prime($product[-1] + 1) - $product[-1]; + + if (!exists $fortunateNumbers{$fN}) { + $fortunateNumbers{$fN}++; + $N--; + } +} + +say join ', ', sort { $a <=> $b } keys %fortunateNumbers; diff --git a/challenge-155/perlboy1967/perl/ch-2.pl b/challenge-155/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..6b16cae6b1 --- /dev/null +++ b/challenge-155/perlboy1967/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 155 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-155/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +TASK #2 › Pisano Period +Submitted by: Mohammad S Anwar + +Write a script to find the period of the 3rd Pisano Period. + + || In number theory, the nth Pisano period, written as π(n), is the period + || with which the sequence of Fibonacci numbers taken modulo n repeats. + +The Fibonacci numbers are the numbers in the integer sequence: + +0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, .... + +For any integer n, the sequence of Fibonacci numbers F(i) taken modulo n is +periodic. The Pisano period, denoted π(n), is the value of the period of this +sequence. For example, the sequence of Fibonacci numbers modulo 3 begins: + +0, 1, 1, 2, 0, 2, 2, 1, +0, 1, 1, 2, 0, 2, 2, 1, +0, 1, 1, 2, 0, 2, 2, 1, ... + +This sequence has period 8, so π(3) = 8. + +=cut + +use v5.16; + +use strict; +use warnings; +use bigint; + +# Prototype(s) +sub fib($); + +my $N = shift // 3; + +my @fibMod; + +my $i = 0; +while(1) { + foreach (0,1) { + push(@fibMod, fib($i++) % $N); + } + # Split fibMod in two halves + my @fModLh = @fibMod[0 .. int($i/2)-1]; + my @fModUh = @fibMod[int($i/2) .. $i-1]; + + # Compare lower and upper half + if (join('',@fModLh) eq join('',@fModUh)) { + printf "%dth Pisano Period: %d (%s)\n", $N, scalar @fModLh, join(',',@fModLh); + exit; + } +} + + +sub fib($) { + my ($n) = @_; + state $f = [0,1]; + $f->[$n] //= fib($n-2) + fib($n-1); + return $f->[$n]; +} |
