aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-31 01:43:51 +0000
committerGitHub <noreply@github.com>2022-01-31 01:43:51 +0000
commit782194f1c10135271acd485e001f8ebc597582fb (patch)
tree35f3164bc4e6dedf6364d2c6688fd60ec667c929
parented9928ef9d876394a02b42e30ca69e4beee55496 (diff)
parent67d5e4e5f2fc556822c252a7ebe1ed9aa65aa668 (diff)
downloadperlweeklychallenge-club-782194f1c10135271acd485e001f8ebc597582fb.tar.gz
perlweeklychallenge-club-782194f1c10135271acd485e001f8ebc597582fb.tar.bz2
perlweeklychallenge-club-782194f1c10135271acd485e001f8ebc597582fb.zip
Merge pull request #5588 from simongreen-net/master
sgreen solutions to challenge 149
-rw-r--r--challenge-148/sgreen/README.md3
-rw-r--r--challenge-149/sgreen/README.md4
-rw-r--r--challenge-149/sgreen/blog.txt1
-rwxr-xr-xchallenge-149/sgreen/perl/ch-1.pl42
-rwxr-xr-xchallenge-149/sgreen/python/ch-1.py41
-rwxr-xr-xchallenge-149/sgreen/python/ch-2.py42
6 files changed, 128 insertions, 5 deletions
diff --git a/challenge-148/sgreen/README.md b/challenge-148/sgreen/README.md
deleted file mode 100644
index 40c85b3a56..0000000000
--- a/challenge-148/sgreen/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# The Weekly Challenge 147
-
-Solution by Simon Green. [Blog - Look mum, no inputs 😛](https://dev.to/simongreennet/look-mum-no-inputs-3lon)
diff --git a/challenge-149/sgreen/README.md b/challenge-149/sgreen/README.md
index 40c85b3a56..08586987d4 100644
--- a/challenge-149/sgreen/README.md
+++ b/challenge-149/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 147
+# The Weekly Challenge 149
-Solution by Simon Green. [Blog - Look mum, no inputs 😛](https://dev.to/simongreennet/look-mum-no-inputs-3lon)
+Solution by Simon Green. [Weekly Challenge 149](https://dev.to/simongreennet/weekly-challenge-149-cln)
diff --git a/challenge-149/sgreen/blog.txt b/challenge-149/sgreen/blog.txt
new file mode 100644
index 0000000000..0b49b8af4d
--- /dev/null
+++ b/challenge-149/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-149-cln
diff --git a/challenge-149/sgreen/perl/ch-1.pl b/challenge-149/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..5569ae14dd
--- /dev/null
+++ b/challenge-149/sgreen/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw(say state);
+use List::Util qw(any sum);
+
+sub is_sum_fib {
+ my $n = shift;
+ state @fibs = ( 0, 1 );
+
+ # Make sure we have all fibonacci numbers <= n
+ while ( $fibs[-1] < $n ) {
+ push @fibs, $fibs[-2] + $fibs[-1];
+ }
+
+ return any { $n == $_ } @fibs;
+}
+
+sub get_sum {
+ return sum( split //, shift );
+}
+
+sub main {
+ my $n = shift;
+ my $value = 0;
+ my @solutions = ();
+
+ # Keep going until we have n solutions.
+ while ( @solutions < $n ) {
+ # If the sum of this number is a fibannoci number, add it
+ if ( is_sum_fib( get_sum($value) ) ) {
+ push @solutions, $value;
+ }
+
+ $value++;
+ }
+
+ say join ', ', @solutions;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-149/sgreen/python/ch-1.py b/challenge-149/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..a192f81016
--- /dev/null
+++ b/challenge-149/sgreen/python/ch-1.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+
+import sys
+
+fibs = [0, 1]
+
+
+def is_sum_fib(n):
+ global fibs
+
+ # Make sure we have all fibonacci numbers <= n
+ while (fibs[-1] < n):
+ fibs.append(fibs[-2] + fibs[-1])
+
+ return True if n in fibs else False
+
+
+def get_sum(n):
+ # Get the sum of all the digits
+ sum = 0
+ for digit in str(n):
+ sum += int(digit)
+ return sum
+
+
+def main(n):
+ value = 0
+ solutions = []
+
+ # Keep going until we have n solutions.
+ while len(solutions) < n:
+ # If the sum of this number is a fibannoci number, add it
+ if is_sum_fib(get_sum(value)):
+ solutions.append(value)
+ value += 1
+
+ print(*solutions, sep=', ')
+
+
+if __name__ == '__main__':
+ main(int(sys.argv[1]))
diff --git a/challenge-149/sgreen/python/ch-2.py b/challenge-149/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..6a2d3362b7
--- /dev/null
+++ b/challenge-149/sgreen/python/ch-2.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+import itertools
+from math import sqrt
+import sys
+
+
+def main(n):
+ v = 'ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210'
+ c = n
+
+ if n < 2 or n > 22:
+ raise ValueError('n must be between 2 and 22')
+
+ # Get all digits for this number
+ v = v[-n:]
+
+ while c > 0:
+ for x in itertools.permutations(v, c):
+ # If the first digit is zero, reset the permutations to have
+ # one less digit
+ if x[0] == '0':
+ c -= 1
+ break
+
+ # Turn the tuple into a string (s) and its integer version (i)
+ s = ''.join(x)
+ i = int(s, n)
+
+ # Calculate the square root of the integer
+ r = sqrt(i)
+ if r == int(r):
+ # If it is a perfect square, then print the number and exit
+ print(f'{s} ({int(r)}² = {i})')
+ return
+
+ # This should never happen as '1' is valid solution for all bases
+ print('No solution!')
+
+
+if __name__ == '__main__':
+ main(int(sys.argv[1]))