diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-26 09:52:33 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-26 09:52:33 +0100 |
| commit | 4fbb301ccb5620c8bcee20462bd1bb8145b0d13c (patch) | |
| tree | ca2cd6d56d2437c433c0913480dd769a0566911b /challenge-133 | |
| parent | de4d4e92a64a4cd61ae6db8da41662c88553772a (diff) | |
| download | perlweeklychallenge-club-4fbb301ccb5620c8bcee20462bd1bb8145b0d13c.tar.gz perlweeklychallenge-club-4fbb301ccb5620c8bcee20462bd1bb8145b0d13c.tar.bz2 perlweeklychallenge-club-4fbb301ccb5620c8bcee20462bd1bb8145b0d13c.zip | |
Whitespace
Diffstat (limited to 'challenge-133')
| -rw-r--r-- | challenge-133/paulo-custodio/perl/ch-1.pl | 84 | ||||
| -rw-r--r-- | challenge-133/paulo-custodio/perl/ch-2.pl | 72 | ||||
| -rw-r--r-- | challenge-133/paulo-custodio/python/ch-1.py | 84 | ||||
| -rw-r--r-- | challenge-133/paulo-custodio/python/ch-2.py | 14 | ||||
| -rw-r--r-- | challenge-133/paulo-custodio/t/test-1.yaml | 1 | ||||
| -rw-r--r-- | challenge-133/paulo-custodio/t/test-2.yaml | 2 |
6 files changed, 128 insertions, 129 deletions
diff --git a/challenge-133/paulo-custodio/perl/ch-1.pl b/challenge-133/paulo-custodio/perl/ch-1.pl index 84b460c143..8d2a021e3c 100644 --- a/challenge-133/paulo-custodio/perl/ch-1.pl +++ b/challenge-133/paulo-custodio/perl/ch-1.pl @@ -1,42 +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;
-}
+#!/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 index dd2cd4f7f8..a439a93d8c 100644 --- a/challenge-133/paulo-custodio/perl/ch-2.pl +++ b/challenge-133/paulo-custodio/perl/ch-2.pl @@ -1,36 +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;
-}
+#!/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 index 69d2f7e92b..1b905ee574 100644 --- a/challenge-133/paulo-custodio/python/ch-1.py +++ b/challenge-133/paulo-custodio/python/ch-1.py @@ -1,42 +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))
+#!/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 index 1cab0e4e43..adfc274f4a 100644 --- a/challenge-133/paulo-custodio/python/ch-2.py +++ b/challenge-133/paulo-custodio/python/ch-2.py @@ -3,13 +3,13 @@ # 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 +# number base, the sum of its digits is equal to the sum of the digits in its # prime factorization in the given number base. -# +# def is_prime(n): if n <= 1: @@ -35,10 +35,10 @@ def get_prime_factors(n): n //= i else: i += 1 - + if n>1: prime_factors.append(n) - + return prime_factors def is_smith(n): @@ -50,7 +50,7 @@ def is_smith(n): fact_digits = [int(x) for x in factors] sum2 = sum(fact_digits) return sum1==sum2 - + n=1 count=0 while count<10: diff --git a/challenge-133/paulo-custodio/t/test-1.yaml b/challenge-133/paulo-custodio/t/test-1.yaml index 88a52a8657..490678400b 100644 --- a/challenge-133/paulo-custodio/t/test-1.yaml +++ b/challenge-133/paulo-custodio/t/test-1.yaml @@ -18,4 +18,3 @@ 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 index 6827399570..e915b1fd0c 100644 --- a/challenge-133/paulo-custodio/t/test-2.yaml +++ b/challenge-133/paulo-custodio/t/test-2.yaml @@ -1,6 +1,6 @@ - setup: cleanup: - args: + args: input: output: | 4 |
