diff options
| author | Andinus <andinus@nand.sh> | 2020-09-10 11:53:30 -0400 |
|---|---|---|
| committer | Andinus <andinus@nand.sh> | 2020-09-10 11:53:30 -0400 |
| commit | 3904006cb4180f01d631bed2ed4588728219b17a (patch) | |
| tree | 381e41b75510b6ba8113f1eb20cbee97202071b7 | |
| parent | 94882719ec2d2ea35cfaf241de7d45b1b2ed1162 (diff) | |
| download | perlweeklychallenge-club-3904006cb4180f01d631bed2ed4588728219b17a.tar.gz perlweeklychallenge-club-3904006cb4180f01d631bed2ed4588728219b17a.tar.bz2 perlweeklychallenge-club-3904006cb4180f01d631bed2ed4588728219b17a.zip | |
Add challenge-077's ch-1 incomplete solution in Perl
It's incomplete because it will not print all possible sums.
I had written this on the same day the challenge was posted & thought
I'll work on it later but it's Thursday & I don't think I'll work on
it this week so I'm pushing it in this state, maybe will complete it
next week.
| -rw-r--r-- | challenge-077/andinus/README | 94 | ||||
| -rw-r--r-- | challenge-077/andinus/blog-1.txt | 1 | ||||
| -rwxr-xr-x | challenge-077/andinus/perl/ch-1.pl | 25 |
3 files changed, 48 insertions, 72 deletions
diff --git a/challenge-077/andinus/README b/challenge-077/andinus/README index 1653e8e915..9437e98f28 100644 --- a/challenge-077/andinus/README +++ b/challenge-077/andinus/README @@ -1,102 +1,52 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 076 + CHALLENGE 077 ━━━━━━━━━━━━━━━ Table of Contents ───────────────── -1 Task 1 - Prime Sum +1 Task 1 - Fibonacci Sum .. 1.1 Perl -1 Task 1 - Prime Sum -════════════════════ +1 Task 1 - Fibonacci Sum +════════════════════════ - You are given a number `$N'. Write a script to find the minimum number - of prime numbers required, whose summation gives you `$N'. + You are given a positive integer `$N'. - • For the sake of this task, please assume 1 is not a prime number. + Write a script to find the total number of Fibonacci Numbers required + to get `$N' on addition. You are NOT allowed to repeat a number. Print + 0 if none found. + + *Note*: This solution is incomplete. Others have pushed complete + solutions, look at those. 1.1 Perl ──────── • Program: [file:perl/ch-1.pl] - • Help taken from: [https://stackoverflow.com/a/35756072]. - - User input is stored in `$input'. - ┌──── - │ my $input = shift @ARGV; - │ chomp $input; - └──── - - 1 is assumed not to be a prime number so we reject numbers less than - or equal to 1. - ┌──── - │ die "Invalid input, enter numbers greater than 1.\n" if $input <= 1; - └──── - - If it's a prime number then the minimum sum is the number itself so we - just return it & exit. - ┌──── - │ say $input and exit 0 if is_prime($input) == 1; - └──── - - If `$input' is even then we loop from 2 to `$input / 2' & check if - both `$i' & `$diff' are primes. If both are primes then we have our - numbers. - Eventually we'll find 2 primes to be a sum of even numbers. From - WikiPedia, [Goldbach's conjecture] has been shown to hold for all - integers less than `4 × 10^18'. + Make a list of all possible sums of `$input'. ┌──── - │ if ($input % 2 == 0) { - │ foreach my $i (2 ... $input / 2) { - │ my $diff = $input - $i; - │ say "$i + $diff" - │ if is_prime($i) and is_prime($diff); - │ } + │ my @sums; + │ foreach my $num (0 ... $input / 2) { + │ my $diff = $input - $num; + │ push @sums, [$diff, $num]; │ } └──── - If the input is odd then we first check if `$input - 2' is a prime, if - it is then input is the sum of two primes, 2 & `$input - 2'. + Loop over `@sums' & then print those sets which have both `$sums->[0]' + & `$sums->[1]' in fibonacci series. ┌──── - │ elsif (is_prime($input - 2)) { - │ say "2 + $input"; - │ } - └──── - - If even that doesn't match then the minimum sum will have three - numbers. 3 & then we use the same function as for even numbers to find - the other two primes. - ┌──── - │ else { - │ foreach my $i (2 ... ($input - 3) / 2) { - │ my $diff = $input - 3 - $i; - │ say "3 + $i + $diff" - │ if is_prime($i) and is_prime($diff); - │ } - │ } - └──── - - If $num is divisible by any number between 2 & `sqrt($num)' then it's - not prime. - ┌──── - │ sub is_prime { - │ my $num = shift @_; + │ sub is_fib { return Math::Fibonacci::isfibonacci(@_) } │ - │ foreach my $i (2 ... sqrt($num)) { - │ return 0 if $num % $i == 0; - │ } - │ return 1; + │ foreach (@sums) { + │ next unless is_fib($_->[0]) and is_fib($_->[1]); + │ say "$_->[0] + $_->[1]"; │ } └──── - - -[Goldbach's conjecture] -https://en.wikipedia.org/wiki/Goldbach%2527s_conjecture diff --git a/challenge-077/andinus/blog-1.txt b/challenge-077/andinus/blog-1.txt new file mode 100644 index 0000000000..3758cbc1da --- /dev/null +++ b/challenge-077/andinus/blog-1.txt @@ -0,0 +1 @@ +https://andinus.tilde.institute/pwc/challenge-077/ diff --git a/challenge-077/andinus/perl/ch-1.pl b/challenge-077/andinus/perl/ch-1.pl new file mode 100755 index 0000000000..83a03c6069 --- /dev/null +++ b/challenge-077/andinus/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +use Math::Fibonacci; + +my $input = shift @ARGV; +chomp $input; + +die "Invalid input, enter numbers greater than 0.\n" if $input < 0; + +my @sums; +foreach my $num (0 ... $input / 2) { + my $diff = $input - $num; + push @sums, [$diff, $num]; +} + +sub is_fib { return Math::Fibonacci::isfibonacci(@_) } + +foreach (@sums) { + next unless is_fib($_->[0]) and is_fib($_->[1]); + say "$_->[0] + $_->[1]"; +} |
