diff options
Diffstat (limited to 'challenge-102')
| -rw-r--r-- | challenge-102/bob-lied/c/ch-1.c | 82 | ||||
| -rwxr-xr-x | challenge-102/bob-lied/perl/ch-1.pl | 154 | ||||
| -rw-r--r-- | challenge-102/cheok-yin-fung/perl/ch-1.pl | 14 | ||||
| -rw-r--r-- | challenge-102/james-smith/perl/ch-1.pl | 6 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/ada/ch_1.adb | 24 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/ada/ch_2.adb | 22 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/basic/ch-1.bas | 60 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/basic/ch-2.bas | 42 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/cpp/ch-2.cpp | 2 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/lua/ch-1.lua | 43 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/lua/ch-2.lua | 46 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/perl/ch-2.pl | 38 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-102/paulo-custodio/python/ch-2.py | 44 | ||||
| -rw-r--r-- | challenge-102/yet-ebreo/perl/ch-2.pl | 37 |
16 files changed, 572 insertions, 116 deletions
diff --git a/challenge-102/bob-lied/c/ch-1.c b/challenge-102/bob-lied/c/ch-1.c new file mode 100644 index 0000000000..1ff39d59ec --- /dev/null +++ b/challenge-102/bob-lied/c/ch-1.c @@ -0,0 +1,82 @@ +/* vim:set ts=4 sw=4 sts=4 et ai wm=0 nu syntax=c: */ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +int verbose = 1; + +int mightBeRare[10] = { 1, 0, 1, 1, 0, 1, 0, 1, 1, 0 }; + +int mightBeSquare[10] = { 1, 1, 0, 0, 1, 1, 1, 0, 0, 1 }; + +long +reverse(long r) +{ + long r1 = 0; + while ( r ) + { + r1 = r1 * 10 + r%10; + r /= 10; + } + return r1; +} + + +int +main(int argc, char **argv) +{ + int N; + + N = atoi(argv[1]); + + int isNodd = N % 2; + + long scale = pow(10, (N-1)); + long endOfRange = pow(10, N); + + for ( long r = scale ; r < endOfRange ; r++ ) + { + // Rare numbers can never start with an odd digit. + if ( (r / scale ) % 2 ) + { + r += scale; + } + +// if ( r % 10000000 == 0 ) printf("%ld\n", r); + + if ( ! mightBeRare[ r % 10 ] ) + { + continue; + } + + long r1 = reverse(r); + + long y2 = r - r1; + if ( y2 < 0 ) continue; // Can't be a square + + long x2 = r + r1; + + if ( !( mightBeSquare[ x2%10] && mightBeSquare[ y2%10 ] ) ) continue; + + if ( isNodd ) + { + if ( y2 % 1089 ) continue; + } + else + { + if ( x2 % 121 ) continue; + } + + double x = sqrt(x2); + if ( x != (long)(x) ) continue; + + double y = sqrt(y2); + if ( y != (long)(y) ) continue; + + printf("N=%d R=%ld\n", N, r); + } + + + exit(0); +} diff --git a/challenge-102/bob-lied/perl/ch-1.pl b/challenge-102/bob-lied/perl/ch-1.pl new file mode 100755 index 0000000000..4b9007cd1d --- /dev/null +++ b/challenge-102/bob-lied/perl/ch-1.pl @@ -0,0 +1,154 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge 102, Task #1, Rare Numbers +# +# You are given a positive integer $N. +# Write a script to generate all Rare numbers of size $N if exists. +# http://www.shyamsundergupta.com/rare.htm +# The web site lists several constraints that can be used to limit the search. +# Also discussed at https://rosettacode.org/wiki/Talk:Rare_numbers +# Examples +# (a) 2 digits: 65 +# (b) 6 digits: 621770 +# (c) 9 digits: 281089082 +# +# From the reference web site: +# The numbers, which gives a perfect square on adding as well as subtracting +# its reverse are rare and hence termed as Rare Numbers. +# +# If R is a positive integer and R1 is the integer obtained from R by writing +# its decimal digits in reverse order, then if R + R1 and R - R1 both are +# perfect square then R is termed as Rare Number. +# +# So for R to be a Rare Number we must have +# R + R1 = X^2 and R - R1 = Y^2 +# +# For example: For R=65, R1=56 +# R+R1 = 65+56 = 121 = 11^2 AND R-R1 = 65 - 56 = 9 = 3^2 +# +#============================================================================= + +use strict; +use warnings; +use 5.020; + +use experimental qw/ signatures /; + +use Getopt::Long; + +my $doTest = 0; +my $verbose = 0; +GetOptions("test" => \$doTest, "verbose" => \&verbose); + +my $N = shift; + +# On my MacBook M1, perl 5.32, 8 takes about 7 seconds and 9 takes about 1:15 +# 10 is probably feasible, maybe 11 for the giftedly patient, but beyond that +# needs some kind of parallelism or an algorithm I wasn't able to think of. +die Usage() unless defined $N && $N > 1 && $N < 20; +warn "Expect this to take a long time ..." if $N > 8; + +# The last digit can never be 1,4,6,9 +my @mightBeRare = ( 1, 0, 1, 1, 0, 1, 0, 1, 1, 0 ); + +# A perfect square can never end in 2,3,7,8 +my @mightBeSquare = ( 1, 1, 0, 0, 1, 1, 1, 0, 0, 1 ); + +my $isNodd = $N % 2; # Optimization possible for even or odd digits. + +# Cache results of square root test here. +my %knownSquare; + +# For example, if N = 3, max is 1000, but we want 100 at a time. +my $scale = 10**($N-1); + +# Rare numbers can never start with an odd digit, so work on +# only groups that start with an even digit. +# Creates pairs of start and end. +my @boundary = map { [ $_ * 2 * $scale, $_ * 2 * $scale + $scale - 1 ] } 1..4; + +# Use faster integer math everywhere except where we need the square root. +use integer; + +for my $bound ( @boundary ) +{ + my $endOfRange = $bound->[1]; # Hoist array access out of loop processing. + R: for ( my $r = $bound->[0] ; $r <= $endOfRange ; $r++ ) + { + # say "$r ", scalar(time()) if $r % 10000000 == 0; # Progress mark + + # The last digit can never be 1,4,6,9 + next unless $mightBeRare[ $r%10 ]; + + my $r1; + $r1 = reverse($r); # String beats math + ##{ use integer; + ## my $n = $r; $r1 = 0; + ## while ( $n ) + ## { + ## $r1 = $r1 * 10 + $n%10; + ## $n /= 10; + ## } + ##} + + my $y2 = $r - $r1; + next if $y2 < 0; # No imaginary numbers. + next unless $mightBeSquare[ $y2 % 10]; + + my $x2 = $r + $r1; + next unless $mightBeSquare[ $x2 % 10]; + + # If R consist of odd number of digits, then R-R1 must be divisible by 11. + # Since R-R1 is always divisible by 9, So 1089 (33^2) must be a factor of Y2. + # + # If R consist of even number of digits, then R+R1 must be divisible by 11, + # So 121 must be a factor of X2. + if ( $isNodd ) + { + next if $y2 % 1089; + } + else + { + next if $x2 % 121; + } + + # Save the expensive square root computation for last. + + # Caching wasn't effective. Either the overhead of hash lookup was not + # much better than the cost of the sqrt function, or there aren't many + # cache hits. And memory could blow up for large N. + # if ( exists $knownSquare{$x2} ) + # { + # next unless $knownSquare{$x2}; + # } + # else + # { + # my $x = sqrt($x2); + # next unless ($knownSquare{$x2} = (int($x) == $x)); + # } + + # if ( exists $knownSquare{$y2} ) + # { + # next unless $knownSquare{$y2}; + # } + # else + # { + # my $y = sqrt($y2); + # next unless ($knownSquare{$y2} = (int($y) == $y)); + # } + + { no integer; + my $x = sqrt($x2); + next R unless int($x) == $x; + my $y = sqrt($y2); + next R unless int($y) == $y; + } + + say "R: $r"; + } +} diff --git a/challenge-102/cheok-yin-fung/perl/ch-1.pl b/challenge-102/cheok-yin-fung/perl/ch-1.pl index eab209da06..2a6a02127e 100644 --- a/challenge-102/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-102/cheok-yin-fung/perl/ch-1.pl @@ -55,3 +55,17 @@ for my $k ($bN..$eN) { # reasonable time for length = 7 , # be patient for length = 8 (one term, which is palindromic) # over 2 min and killed for length = 9 ... + +=pod +// add on March 8th +$ time perl ch-1.pl 9 +200040002 +204060402 +242484242 +281089082 +291080192 + +real 10m21.188s +user 10m19.621s +sys 0m0.084s +=cut diff --git a/challenge-102/james-smith/perl/ch-1.pl b/challenge-102/james-smith/perl/ch-1.pl index 187ed6b95b..dcfb31af4d 100644 --- a/challenge-102/james-smith/perl/ch-1.pl +++ b/challenge-102/james-smith/perl/ch-1.pl @@ -41,10 +41,10 @@ sub rare_numbers { my $x = shift; return () if $F[$x%9]; ## Digit sum is wrong... my $y = reverse $x; - return () if $x == $y; ## Musn't be the same back and forth - return $y if $x<$y && is_sq($x+$y) && is_sq($y-$x); + return () if $x == $y || ! is_sq($x+$y); ## Musn't be the same back and forth + return $y if $x<$y && is_sq($y-$x); + return $x if $y<$x && is_sq($x-$y); ## Check both ways round! - return $x if $y<$x && is_sq($x+$y) && is_sq($x-$y); return (); } diff --git a/challenge-102/paulo-custodio/ada/ch_1.adb b/challenge-102/paulo-custodio/ada/ch_1.adb index b680983748..a785727689 100644 --- a/challenge-102/paulo-custodio/ada/ch_1.adb +++ b/challenge-102/paulo-custodio/ada/ch_1.adb @@ -1,23 +1,23 @@ -- Challenge 102 --- +-- -- TASK #1 › Rare Numbers -- Submitted by: Mohammad S Anwar --- +-- -- You are given a positive integer $N. --- --- Write a script to generate all Rare numbers of size $N if exists. Please +-- +-- Write a script to generate all Rare numbers of size $N if exists. Please -- checkout the page for more information about it. -- Examples --- +-- -- (a) 2 digits: 65 -- (b) 6 digits: 621770 -- (c) 9 digits: 281089082 with Ada.Command_Line; -with Ada.Strings.Fixed; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; -with Ada.Text_IO; - use Ada.Text_IO; +with Ada.Text_IO; + use Ada.Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure ch_1 is @@ -29,8 +29,8 @@ procedure ch_1 is -- Sqrt package Value_Functions is new Ada.Numerics.Generic_Elementary_Functions(Float); - use Value_Functions; - + use Value_Functions; + -- convert 1234 to 4321 function invert_number(a0 : Integer) return integer is a : Integer := a0; @@ -54,7 +54,7 @@ procedure ch_1 is return False; end if; end is_perfect_square; - + -- print all rare numbers with n digits procedure print_rare_numbers(n : Integer) is r1 : Integer; @@ -71,7 +71,7 @@ procedure ch_1 is end if; end loop; end print_rare_numbers; - + begin print_rare_numbers(Integer'Value(CL.Argument(1))); end ch_1; diff --git a/challenge-102/paulo-custodio/ada/ch_2.adb b/challenge-102/paulo-custodio/ada/ch_2.adb index 7eb1656ec9..48fb10aa2d 100644 --- a/challenge-102/paulo-custodio/ada/ch_2.adb +++ b/challenge-102/paulo-custodio/ada/ch_2.adb @@ -1,23 +1,23 @@ -- Challenge 102 --- +-- -- TASK #2 › Hash-counting String -- Submitted by: Stuart Little --- +-- -- You are given a positive integer $N. --- +-- -- Write a script to produce Hash-counting string of that length. --- +-- -- The definition of a hash-counting string is as follows: -- - the string consists only of digits 0-9 and hashes, ‘#’ -- - there are no two consecutive hashes: ‘##’ does not appear in your string -- - the last character is a hash --- - the number immediately preceding each hash (if it exists) is the position +-- - the number immediately preceding each hash (if it exists) is the position -- of that hash in the string, with the position being counted up from 1 --- --- It can be shown that for every positive integer N there is exactly one such +-- +-- It can be shown that for every positive integer N there is exactly one such -- length-N string. -- Examples: --- +-- -- (a) "#" is the counting string of length 1 -- (b) "2#" is the counting string of length 2 -- (c) "#3#" is the string of length 3 @@ -26,7 +26,7 @@ with Ada.Command_Line; with Ada.Strings.Fixed; use Ada.Strings.Fixed; -with Ada.Text_IO; use Ada.Text_IO; +with Ada.Text_IO; use Ada.Text_IO; procedure ch_2 is -- command line arguments @@ -49,7 +49,7 @@ procedure ch_2 is end loop; return res; end hash_count; - -begin + +begin Put_Line(hash_count(Integer'Value(CL.Argument(1)))); end ch_2; diff --git a/challenge-102/paulo-custodio/basic/ch-1.bas b/challenge-102/paulo-custodio/basic/ch-1.bas index b33e778d9a..161682f7a8 100644 --- a/challenge-102/paulo-custodio/basic/ch-1.bas +++ b/challenge-102/paulo-custodio/basic/ch-1.bas @@ -1,52 +1,52 @@ ' Challenge 102 -' +' ' TASK #1 > Rare Numbers ' Submitted by: Mohammad S Anwar -' +' ' You are given a positive integer $N. -' -' Write a script to generate all Rare numbers of size $N if exists. Please +' +' Write a script to generate all Rare numbers of size $N if exists. Please ' checkout the page for more information about it. ' Examples -' +' ' (a) 2 digits: 65 ' (b) 6 digits: 621770 ' (c) 9 digits: 281089082 ' convert 1234 to 4321 function invert_number(a as Integer) as Integer - dim a1 as Integer - do while a<>0 - a1 = a1*10 + (a mod 10) - a = int(a / 10) - loop - invert_number = a1 + dim a1 as Integer + do while a<>0 + a1 = a1*10 + (a mod 10) + a = int(a / 10) + loop + invert_number = a1 end function ' check if number is a perfect square function is_perfect_square(n as Integer) as Boolean - dim sq as Double - sq = sqr(CDbl(n)) - if int(sq)^2=n then - is_perfect_square = True - else - is_perfect_square = False - end if -end function + dim sq as Double + sq = sqr(CDbl(n)) + if int(sq)^2=n then + is_perfect_square = True + else + is_perfect_square = False + end if +end function ' print all rare numbers with n digits sub print_rare_numbers(n as Integer) - dim r as Integer, r1 as Integer - for r=10^(n-1) to 10^(n)-1 - r1 = invert_number(r) - if is_perfect_square(r+r1) then - if r>=r1 then - if is_perfect_square(r-r1) then - print trim(str(r)) - end if - end if - end if - next + dim r as Integer, r1 as Integer + for r=10^(n-1) to 10^(n)-1 + r1 = invert_number(r) + if is_perfect_square(r+r1) then + if r>=r1 then + if is_perfect_square(r-r1) then + print trim(str(r)) + end if + end if + end if + next end sub ' main diff --git a/challenge-102/paulo-custodio/basic/ch-2.bas b/challenge-102/paulo-custodio/basic/ch-2.bas index 9987f13b4e..34380d02aa 100644 --- a/challenge-102/paulo-custodio/basic/ch-2.bas +++ b/challenge-102/paulo-custodio/basic/ch-2.bas @@ -1,23 +1,23 @@ ' Challenge 102 -' +' ' TASK #2 › Hash-counting String ' Submitted by: Stuart Little -' +' ' You are given a positive integer $N. -' +' ' Write a script to produce Hash-counting string of that length. -' +' ' The definition of a hash-counting string is as follows: ' - the string consists only of digits 0-9 and hashes, ‘#’ ' - there are no two consecutive hashes: ‘##’ does not appear in your string ' - the last character is a hash -' - the number immediately preceding each hash (if it exists) is the position +' - the number immediately preceding each hash (if it exists) is the position ' of that hash in the string, with the position being counted up from 1 -' -' It can be shown that for every positive integer N there is exactly one such +' +' It can be shown that for every positive integer N there is exactly one such ' length-N string. ' Examples: -' +' ' (a) "#" is the counting string of length 1 ' (b) "2#" is the counting string of length 2 ' (c) "#3#" is the string of length 3 @@ -25,19 +25,19 @@ ' (e) "2#4#6#8#11#14#" is the string of length 14 function hash_count(n as Integer) as String - Dim res as String, i as Integer, p as Integer - i = n - do while i>0 - p = i - res = "#" & res - i = i-1 - do while i>0 and p<>0 - res = chr(asc("0") + (p mod 10)) & res - p = int(p / 10) - i = i-1 - loop - loop - hash_count = res + Dim res as String, i as Integer, p as Integer + i = n + do while i>0 + p = i + res = "#" & res + i = i-1 + do while i>0 and p<>0 + res = chr(asc("0") + (p mod 10)) & res + p = int(p / 10) + i = i-1 + loop + loop + hash_count = res end function 'main diff --git a/challenge-102/paulo-custodio/cpp/ch-2.cpp b/challenge-102/paulo-custodio/cpp/ch-2.cpp index 24fbf2fb06..4832b1d64c 100644 --- a/challenge-102/paulo-custodio/cpp/ch-2.cpp +++ b/challenge-102/paulo-custodio/cpp/ch-2.cpp @@ -49,6 +49,6 @@ int main(int argc, char* argv[]) { std::cerr << "Usage: ch-1 N" << std::endl; return EXIT_FAILURE; } - else + else std::cout << hash_count(atoi(argv[1])) << std::endl; } diff --git a/challenge-102/paulo-custodio/lua/ch-1.lua b/challenge-102/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..a24f601c57 --- /dev/null +++ b/challenge-102/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,43 @@ +#!/usr/bin/env lua + +--[[ +Challenge 102 + +TASK #1 › Rare Numbers +Submitted by: Mohammad S Anwar + +You are given a positive integer $N. + +Write a script to generate all Rare numbers of size $N if exists. Please +checkout the page for more information about it. +Examples + +(a) 2 digits: 65 +(b) 6 digits: 621770 +(c) 9 digits: 281089082 +--]] + +function perfect_square(n) + local sq = math.sqrt(n) + if math.floor(sq) == sq then + return true + else + return false + end +end + +function print_rare(n) + for r=10^(n-1),(10^n)-1 do + r = math.floor(r) -- convert to int + r1 = tonumber(string.reverse(tostring(r))) + if perfect_square(r+r1) then + if r >= r1 then + if perfect_square(r-r1) then + io.write(r, "\n") + end + end + end + end +end + +print_rare(tonumber(arg[1])) diff --git a/challenge-102/paulo-custodio/lua/ch-2.lua b/challenge-102/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..72d378f496 --- /dev/null +++ b/challenge-102/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,46 @@ +#!/usr/bin/env lua + +--[[ +Challenge 102 + +TASK #2 > Hash-counting String +Submitted by: Stuart Little + +You are given a positive integer $N. + +Write a script to produce Hash-counting string of that length. + +The definition of a hash-counting string is as follows: +- the string consists only of digits 0-9 and hashes, '#' +- there are no two consecutive hashes: '##' does not appear in your string +- the last character is a hash +- the number immediately preceding each hash (if it exists) is the position +of that hash in the string, with the position being counted up from 1 + +It can be shown that for every positive integer N there is exactly one such +length-N string. +Examples: + +(a) "#" is the counting string of length 1 +(b) "2#" is the counting string of length 2 +(c) "#3#" is the string of length 3 +(d) "#3#5#7#10#" is the string of length 10 +(e) "2#4#6#8#11#14#" is the string of length 14 +--]] + +function hash_counting(n) + local out, i = '', n + while i > 0 do + p = i + out = '#'..out + i = i - 1 + while i > 0 and p ~= 0 do + out = tostring(p % 10)..out + i = i - 1 + p = math.floor(p / 10) + end + end + return out +end + +print(hash_counting(tonumber(arg[1]))) diff --git a/challenge-102/paulo-custodio/perl/ch-1.pl b/challenge-102/paulo-custodio/perl/ch-1.pl index 587ad59d05..644c5cf065 100644 --- a/challenge-102/paulo-custodio/perl/ch-1.pl +++ b/challenge-102/paulo-custodio/perl/ch-1.pl @@ -1,16 +1,16 @@ #!/usr/bin/perl # Challenge 102 -# +# # TASK #1 › Rare Numbers # Submitted by: Mohammad S Anwar -# +# # You are given a positive integer $N. -# -# Write a script to generate all Rare numbers of size $N if exists. Please +# +# Write a script to generate all Rare numbers of size $N if exists. Please # checkout the page for more information about it. # Examples -# +# # (a) 2 digits: 65 # (b) 6 digits: 621770 # (c) 9 digits: 281089082 @@ -23,21 +23,21 @@ my($N) = @ARGV or die "Usage: ch-1.pl N\n"; print_rare($N); sub print_rare { - my($n) = @_; - for my $r (10**($n-1) .. 10**($n)-1) { - my $r1=0+(join('', reverse split('', $r))); - if (perfect_square($r+$r1)) { - if ($r>=$r1) { - if (perfect_square($r-$r1)) { - say $r; - } - } - } - } + my($n) = @_; + for my $r (10**($n-1) .. 10**($n)-1) { + my $r1 = reverse $r; + if (perfect_square($r+$r1)) { + if ($r>=$r1) { + if (perfect_square($r-$r1)) { + say $r; + } + } + } + } } sub perfect_square { - my($n) = @_; - my $x = sqrt($n); - return ($x == int($x)); + my($n) = @_; + my $x = sqrt($n); + return ($x == int($x)); } diff --git a/challenge-102/paulo-custodio/perl/ch-2.pl b/challenge-102/paulo-custodio/perl/ch-2.pl index eac91f4044..64afdc45d6 100644 --- a/challenge-102/paulo-custodio/perl/ch-2.pl +++ b/challenge-102/paulo-custodio/perl/ch-2.pl @@ -1,25 +1,25 @@ #!/usr/bin/perl # Challenge 102 -# +# # TASK #2 › Hash-counting String # Submitted by: Stuart Little -# +# # You are given a positive integer $N. -# +# # Write a script to produce Hash-counting string of that length. -# +# # The definition of a hash-counting string is as follows: # - the string consists only of digits 0-9 and hashes, ‘#’ # - there are no two consecutive hashes: ‘##’ does not appear in your string # - the last character is a hash -# - the number immediately preceding each hash (if it exists) is the position +# - the number immediately preceding each hash (if it exists) is the position # of that hash in the string, with the position being counted up from 1 -# -# It can be shown that for every positive integer N there is exactly one such +# +# It can be shown that for every positive integer N there is exactly one such # length-N string. # Examples: -# +# # (a) "#" is the counting string of length 1 # (b) "2#" is the counting string of length 2 # (c) "#3#" is the string of length 3 @@ -34,15 +34,15 @@ my($N) = @ARGV or die "Usage: ch-1.pl N\n"; say hash_counting($N); sub hash_counting { - my($n) = @_; - my @out; - for (my $i = $n; $i>0;) { - my $pos = $i; - $out[$i--] = '#'; - while ($i>0 && $pos!=0) { - $out[$i--] = $pos%10; - $pos = int($pos/10); - } - } - return join('', @out[1..$n]); + my($n) = @_; + my @out; + for (my $i = $n; $i>0;) { + my $pos = $i; + $out[$i--] = '#'; + while ($i>0 && $pos!=0) { + $out[$i--] = $pos%10; + $pos = int($pos/10); + } + } + return join('', @out[1..$n]); } diff --git a/challenge-102/paulo-custodio/python/ch-1.py b/challenge-102/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..d513c19117 --- /dev/null +++ b/challenge-102/paulo-custodio/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +# Challenge 102 +# +# TASK #1 > Rare Numbers +# Submitted by: Mohammad S Anwar +# +# You are given a positive integer $N. +# +# Write a script to generate all Rare numbers of size $N if exists. Please +# checkout the page for more information about it. +# Examples +# +# (a) 2 digits: 65 +# (b) 6 digits: 621770 +# (c) 9 digits: 281089082 + +import sys +import math + +def perfect_square(n): + sq = math.sqrt(float(n)) + if math.floor(sq) == sq: + return True + else: + return False + +def print_rare(n): + for r in range(10**(n-1), 10**n): + r1 = int(str(r)[::-1]) + if perfect_square(r+r1): + if r>=r1: + if perfect_square(r-r1): + print(r) + +print_rare(int(sys.argv[1])) diff --git a/challenge-102/paulo-custodio/python/ch-2.py b/challenge-102/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..558367a422 --- /dev/null +++ b/challenge-102/paulo-custodio/python/ch-2.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +# Challenge 102 +# +# TASK #2 > Hash-counting String +# Submitted by: Stuart Little +# +# You are given a positive integer $N. +# +# Write a script to produce Hash-counting string of that length. +# +# The definition of a hash-counting string is as follows: +# - the string consists only of digits 0-9 and hashes, '#' +# - there are no two consecutive hashes: '##' does not appear in your string +# - the last character is a hash +# - the number immediately preceding each hash (if it exists) is the position +# of that hash in the string, with the position being counted up from 1 +# +# It can be shown that for every positive integer N there is exactly one such +# length-N string. +# Examples: +# +# (a) "#" is the counting string of length 1 +# (b) "2#" is the counting string of length 2 +# (c) "#3#" is the string of length 3 +# (d) "#3#5#7#10#" is the string of length 10 +# (e) "2#4#6#8#11#14#" is the string of length 14 + +import sys + +def hash_counting(n): + out = '' + i = n + while i>0: + p = i + out = '#'+out + i -= 1 + while i>0 and p!=0: + out = str(p % 10)+out + i -= 1 + p = int(p/10) + return out + +print(hash_counting(int(sys.argv[1]))) diff --git a/challenge-102/yet-ebreo/perl/ch-2.pl b/challenge-102/yet-ebreo/perl/ch-2.pl new file mode 100644 index 0000000000..4197f1d406 --- /dev/null +++ b/challenge-102/yet-ebreo/perl/ch-2.pl @@ -0,0 +1,37 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use feature qw(say); + + +# You are given a positive integer $N. + +# Write a script to produce Hash-counting string of that length. + +# The definition of a hash-counting string is as follows: + +# - the string consists only of digits 0-9 and hashes, ‘#’ +# - there are no two consecutive hashes: ‘##’ does not appear in your string +# - the last character is a hash +# - the number immediately preceding each hash (if it exists) is the position of that hash in the string, with the position being counted up from 1 +# It can be shown that for every positive integer N there is exactly one such length-N string. + +# Examples: +# (a) "#" is the counting string of length 1 +# (b) "2#" is the counting string of length 2 +# (c) "#3#" is the string of length 3 +# (d) "#3#5#7#10#" is the string of length 10 +# (e) "2#4#6#8#11#14#" is the string of length 14 + +my $N = $ARGV[0] || 2; +my $out = ""; + +while ($N) { + $out = ($N>1?"$N":"")."#$out"; + $N = $ARGV[0] - length $out; +} + + +say $out; + |
