diff options
| -rw-r--r-- | challenge-136/paulo-custodio/perl/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-136/paulo-custodio/perl/ch-2.pl | 72 | ||||
| -rw-r--r-- | challenge-136/paulo-custodio/python/ch-1.py | 43 | ||||
| -rw-r--r-- | challenge-136/paulo-custodio/python/ch-2.py | 112 | ||||
| -rw-r--r-- | challenge-136/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-136/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-136/paulo-custodio/test.pl | 4 |
7 files changed, 306 insertions, 0 deletions
diff --git a/challenge-136/paulo-custodio/perl/ch-1.pl b/challenge-136/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..fae0d5f06a --- /dev/null +++ b/challenge-136/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# Challenge 136 +# +# TASK #1 > Two Friendly +# Submitted by: Mohammad S Anwar +# You are given 2 positive numbers, $m and $n. +# +# Write a script to find out if the given two numbers are Two Friendly. +# +# Two positive numbers, m and n are two friendly when gcd(m, n) = 2 ^ p +# where p > 0. The greatest common divisor (gcd) of a set of numbers is +# the largest positive number that divides all the numbers in the set +# without remainder. +# +# Example 1 +# Input: $m = 8, $n = 24 +# Output: 1 +# +# Reason: gcd(8,24) = 8 => 2 ^ 3 +# Example 2 +# Input: $m = 26, $n = 39 +# Output: 0 +# +# Reason: gcd(26,39) = 13 +# Example 3 +# Input: $m = 4, $n = 10 +# Output: 1 +# +# Reason: gcd(4,10) = 2 => 2 ^ 1 + +use Modern::Perl; +use ntheory 'gcd'; + +say is_power_2(gcd(@ARGV)); + +sub is_power_2 { + my($n) = @_; + my $p = 2; + while ($p <= $n) { + return 1 if $p==$n; + $p *= 2; + } + return 0; +} diff --git a/challenge-136/paulo-custodio/perl/ch-2.pl b/challenge-136/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..fcbadd1cca --- /dev/null +++ b/challenge-136/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl + +# Challenge 136 +# +# TASK #2 > Fibonacci Sequence +# Submitted by: Mohammad S Anwar +# You are given a positive number $n. +# +# Write a script to find how many different sequences you can create using +# Fibonacci numbers where the sum of unique numbers in each sequence are the +# same as the given number. +# +# Fibonacci Numbers: 1,2,3,5,8,13,21,34,55,89, ... +# +# Example 1 +# Input: $n = 16 +# Output: 4 +# +# Reason: There are 4 possible sequences that can be created using Fibonacci +# numbers +# i.e. (3 + 13), (1 + 2 + 13), (3 + 5 + 8) and (1 + 2 + 5 + 8). +# Example 2 +# Input: $n = 9 +# Output: 2 +# +# Reason: There are 2 possible sequences that can be created using Fibonacci +# numbers +# i.e. (1 + 3 + 5) and (1 + 8). +# Example 3 +# Input: $n = 15 +# Output: 2 +# +# Reason: There are 2 possible sequences that can be created using Fibonacci +# numbers +# i.e. (2 + 5 + 8) and (2 + 13). + +use Modern::Perl; +use Math::Fibonacci 'term'; +use Math::Combinatorics; +use List::Util 'sum'; + +@ARGV or die "Usage: ch-2.pl n\n"; +my $n = shift; +my @fibs = fibonacci_upto($n); +say count_combin_sum($n, @fibs); + + +sub fibonacci_upto { + my($n) = @_; + my @fibs; + my $i = 2; # skip first '1' + do { + push @fibs, term($i++); + } while ($fibs[-1] < $n); + pop @fibs if $fibs[-1] > $n; + return @fibs; +} + +sub count_combin_sum { + my($n, @terms) = @_; + my $count = 0; + for my $k (1..@terms) { + my @combin = combine($k, @terms); + for (@combin) { + my @combo = @$_; + if (sum(@combo) == $n) { + $count++; + } + } + } + return $count; +} diff --git a/challenge-136/paulo-custodio/python/ch-1.py b/challenge-136/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2a65d9aa1d --- /dev/null +++ b/challenge-136/paulo-custodio/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# Challenge 136 +# +# TASK #1 > Two Friendly +# Submitted by: Mohammad S Anwar +# You are given 2 positive numbers, $m and $n. +# +# Write a script to find out if the given two numbers are Two Friendly. +# +# Two positive numbers, m and n are two friendly when gcd(m, n) = 2 ^ p +# where p > 0. The greatest common divisor (gcd) of a set of numbers is +# the largest positive number that divides all the numbers in the set +# without remainder. +# +# Example 1 +# Input: $m = 8, $n = 24 +# Output: 1 +# +# Reason: gcd(8,24) = 8 => 2 ^ 3 +# Example 2 +# Input: $m = 26, $n = 39 +# Output: 0 +# +# Reason: gcd(26,39) = 13 +# Example 3 +# Input: $m = 4, $n = 10 +# Output: 1 +# +# Reason: gcd(4,10) = 2 => 2 ^ 1 + +import sys +from math import gcd + +def is_power_2(n): + p = 2 + while p<=n: + if p==n: + return 1 + p *= 2 + return 0 + +print(is_power_2(gcd(int(sys.argv[1]), int(sys.argv[2])))) diff --git a/challenge-136/paulo-custodio/python/ch-2.py b/challenge-136/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..092dd8df9d --- /dev/null +++ b/challenge-136/paulo-custodio/python/ch-2.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 + +# Challenge 136 +# +# TASK #2 > Fibonacci Sequence +# Submitted by: Mohammad S Anwar +# You are given a positive number $n. +# +# Write a script to find how many different sequences you can create using +# Fibonacci numbers where the sum of unique numbers in each sequence are the +# same as the given number. +# +# Fibonacci Numbers: 1,2,3,5,8,13,21,34,55,89, ... +# +# Example 1 +# Input: $n = 16 +# Output: 4 +# +# Reason: There are 4 possible sequences that can be created using Fibonacci +# numbers +# i.e. (3 + 13), (1 + 2 + 13), (3 + 5 + 8) and (1 + 2 + 5 + 8). +# Example 2 +# Input: $n = 9 +# Output: 2 +# +# Reason: There are 2 possible sequences that can be created using Fibonacci +# numbers +# i.e. (1 + 3 + 5) and (1 + 8). +# Example 3 +# Input: $n = 15 +# Output: 2 +# +# Reason: There are 2 possible sequences that can be created using Fibonacci +# numbers +# i.e. (2 + 5 + 8) and (2 + 13). + +import sys +from itertools import combinations + +def fibonacci(n): + a = 0 + b = 1 + + # Check is n is less + # than 0 + if n < 0: + print("Incorrect input") + + # Check is n is equal + # to 0 + elif n == 0: + return 0 + + # Check if n is equal to 1 + elif n == 1: + return b + else: + for i in range(1, n): + c = a + b + a = b + b = c + return b + +def fibonacci_upto(n): + i = 2 # skip first '1' + fibs = [fibonacci(i)] + i += 1 + while fibs[-1] < n: + fibs.append(fibonacci(i)) + i += 1 + if fibs[-1] > n: + fibs.pop() + return fibs + +def count_combin_sum(n, terms): + count = 0 + for k in range(1, len(terms)+1): + for combin in combinations(terms, k): + if sum(combin)==n: + count += 1 + return count + +n = int(sys.argv[1]) +fibs = fibonacci_upto(n) +print(count_combin_sum(n, fibs)) + +# use Modern::Perl; +# use Math::Fibonacci 'term'; +# use Math::Combinatorics; +# use List::Util 'sum'; +# +# @ARGV or die "Usage: ch-2.pl n\n"; +# my $n = shift; +# my @fibs = fibs_upto($n); +# say count_combin_sum($n, @fibs); +# +# +# sub count_combin_sum { +# my($n, @fibs) = @_; +# my $count = 0; +# for my $k (1..@fibs) { +# my @combin = combine($k, @fibs); +# for (@combin) { +# my @combo = @$_; +# if (sum(@combo) == $n) { +# $count++; +# } +# } +# } +# return $count; +# } +#
\ No newline at end of file diff --git a/challenge-136/paulo-custodio/t/test-1.yaml b/challenge-136/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..97ec6250bf --- /dev/null +++ b/challenge-136/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 8 24 + input: + output: 1 +- setup: + cleanup: + args: 26 39 + input: + output: 0 +- setup: + cleanup: + args: 4 10 + input: + output: 1 diff --git a/challenge-136/paulo-custodio/t/test-2.yaml b/challenge-136/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a7d9e098ae --- /dev/null +++ b/challenge-136/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 16 + input: + output: 4 +- setup: + cleanup: + args: 9 + input: + output: 2 +- setup: + cleanup: + args: 15 + input: + output: 2 diff --git a/challenge-136/paulo-custodio/test.pl b/challenge-136/paulo-custodio/test.pl new file mode 100644 index 0000000000..ba6c37260b --- /dev/null +++ b/challenge-136/paulo-custodio/test.pl @@ -0,0 +1,4 @@ +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; +require '../../challenge-001/paulo-custodio/test.pl'; |
