From f659645fdef4e20b07dc7ba8cdd97937fa0cdf1c Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 17 Oct 2021 20:10:55 +0100 Subject: Add Perl and Pythol solutions to challenge 133 --- challenge-133/paulo-custodio/perl/ch-1.pl | 42 +++++++++++++++++++++++++ challenge-133/paulo-custodio/perl/ch-2.pl | 36 +++++++++++++++++++++ challenge-133/paulo-custodio/python/ch-1.py | 42 +++++++++++++++++++++++++ challenge-133/paulo-custodio/python/ch-2.py | 49 +++++++++++++++++++++++++++++ challenge-133/paulo-custodio/t/test-1.yaml | 21 +++++++++++++ challenge-133/paulo-custodio/t/test-2.yaml | 15 +++++++++ challenge-133/paulo-custodio/test.pl | 4 +++ 7 files changed, 209 insertions(+) create mode 100644 challenge-133/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-133/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-133/paulo-custodio/python/ch-1.py create mode 100644 challenge-133/paulo-custodio/python/ch-2.py create mode 100644 challenge-133/paulo-custodio/t/test-1.yaml create mode 100644 challenge-133/paulo-custodio/t/test-2.yaml create mode 100644 challenge-133/paulo-custodio/test.pl diff --git a/challenge-133/paulo-custodio/perl/ch-1.pl b/challenge-133/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..84b460c143 --- /dev/null +++ b/challenge-133/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# TASK #1 > Integer Square Root +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to calculate the integer square root of the given number. +# +# Please avoid using built-in function. Find out more about it here. +# +# Examples +# Input: $N = 10 +# Output: 3 +# +# Input: $N = 27 +# Output: 5 +# +# Input: $N = 85 +# Output: 9 +# +# Input: $N = 101 +# Output: 10 + +# solution: https://en.wikipedia.org/wiki/Integer_square_root + +use Modern::Perl; +my $n = shift || 0; +say isqrt($n); + +sub isqrt { + my($n) = @_; + my $x0 = $n >> 1; # initial estimate + return $n if $x0 == 0; + + # loop + my $x1 = ($x0 + $n/$x0) >> 1; + while ($x1 < $x0) { + $x0 = $x1; + $x1 = ($x0 + $n/$x0) >> 1; + } + return $x0; +} diff --git a/challenge-133/paulo-custodio/perl/ch-2.pl b/challenge-133/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..dd2cd4f7f8 --- /dev/null +++ b/challenge-133/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +# TASK #2 > Smith Numbers +# Submitted by: Mohammad S Anwar +# Write a script to generate first 10 Smith Numbers in base 10. +# +# According to Wikipedia: +# +# In number theory, a Smith number is a composite number for which, in a given +# number base, the sum of its digits is equal to the sum of the digits in its +# prime factorization in the given number base. +# + +use Modern::Perl; +use ntheory 'factor', 'is_prime'; +use List::Util 'sum'; + +my $n = 1; +my $count = 0; +while ($count < 10) { + $n++; + if (is_smith($n)) { + say $n; + $count++ + } +} + +sub is_smith { + my($n) = @_; + return if is_prime($n); + my @digits = split //, $n; + my $sum1 = sum(@digits); + my @fact_digits = split //, join '', factor($n); + my $sum2 = sum(@fact_digits); + return $sum1 == $sum2; +} diff --git a/challenge-133/paulo-custodio/python/ch-1.py b/challenge-133/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..69d2f7e92b --- /dev/null +++ b/challenge-133/paulo-custodio/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# TASK #1 > Integer Square Root +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to calculate the integer square root of the given number. +# +# Please avoid using built-in function. Find out more about it here. +# +# Examples +# Input: $N = 10 +# Output: 3 +# +# Input: $N = 27 +# Output: 5 +# +# Input: $N = 85 +# Output: 9 +# +# Input: $N = 101 +# Output: 10 + +# solution: https://en.wikipedia.org/wiki/Integer_square_root + +import sys + +def isqrt(n): + x0 = n >> 1 # initial estimate + if x0 == 0: + return n + + # loop + x1 = int(x0 + n/x0) >> 1 + while x1 < x0: + x0 = x1; + x1 = int(x0 + n/x0) >> 1 + + return x0 + +n = int(sys.argv[1]) +print(isqrt(n)) diff --git a/challenge-133/paulo-custodio/python/ch-2.py b/challenge-133/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e03c74f7ab --- /dev/null +++ b/challenge-133/paulo-custodio/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +# TASK #2 > Smith Numbers +# Submitted by: Mohammad S Anwar +# Write a script to generate first 10 Smith Numbers in base 10. +# +# According to Wikipedia: +# +# In number theory, a Smith number is a composite number for which, in a given +# number base, the sum of its digits is equal to the sum of the digits in its +# prime factorization in the given number base. +# + +from sympy import isprime + +def get_prime_factors(n): + i = 2 + prime_factors = [] + while i*i <= n: + if n%i == 0: + prime_factors.append(i) + n //= i + else: + i += 1 + + if n>1: + prime_factors.append(n) + + return prime_factors + +def is_smith(n): + if isprime(n): + return False + digits = [int(x) for x in str(n)] + sum1 = sum(digits) + factors = ''.join([str(x) for x in get_prime_factors(n)]) + fact_digits = [int(x) for x in factors] + sum2 = sum(fact_digits) + return sum1==sum2 + +n=1 +count=0 +while count<10: + n+=1 + if is_smith(n): + print(n) + count+=1 + + \ No newline at end of file diff --git a/challenge-133/paulo-custodio/t/test-1.yaml b/challenge-133/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..88a52a8657 --- /dev/null +++ b/challenge-133/paulo-custodio/t/test-1.yaml @@ -0,0 +1,21 @@ +- setup: + cleanup: + args: 10 + input: + output: 3 +- setup: + cleanup: + args: 27 + input: + output: 5 +- setup: + cleanup: + args: 85 + input: + output: 9 +- setup: + cleanup: + args: 101 + input: + output: 10 + diff --git a/challenge-133/paulo-custodio/t/test-2.yaml b/challenge-133/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..6827399570 --- /dev/null +++ b/challenge-133/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: + input: + output: | + 4 + 22 + 27 + 58 + 85 + 94 + 121 + 166 + 202 + 265 diff --git a/challenge-133/paulo-custodio/test.pl b/challenge-133/paulo-custodio/test.pl new file mode 100644 index 0000000000..ba6c37260b --- /dev/null +++ b/challenge-133/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'; -- cgit