diff options
| author | boblied <boblied@gmail.com> | 2021-03-12 09:47:03 -0600 |
|---|---|---|
| committer | boblied <boblied@gmail.com> | 2021-03-12 09:47:03 -0600 |
| commit | 894e021d25024e0eb3aaf4998276031d7b6d39f6 (patch) | |
| tree | b559eab313603314f92088e15d91d9b312d8631c /challenge-102 | |
| parent | 665a693f4730d44b81323270644a1a5863171400 (diff) | |
| parent | 902d0a485ec4dbab77f9d024ff1985d381be97cc (diff) | |
| download | perlweeklychallenge-club-894e021d25024e0eb3aaf4998276031d7b6d39f6.tar.gz perlweeklychallenge-club-894e021d25024e0eb3aaf4998276031d7b6d39f6.tar.bz2 perlweeklychallenge-club-894e021d25024e0eb3aaf4998276031d7b6d39f6.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-102')
| -rw-r--r-- | challenge-102/cheok-yin-fung/perl/ch-1.pl | 14 | ||||
| -rwxr-xr-x | challenge-102/feng-chang/raku/ch-1.raku | 17 | ||||
| -rwxr-xr-x | challenge-102/feng-chang/raku/ch-2.raku | 15 | ||||
| -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, 368 insertions, 116 deletions
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/feng-chang/raku/ch-1.raku b/challenge-102/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..463aa8f413 --- /dev/null +++ b/challenge-102/feng-chang/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!/bin/env raku + +sub is-square(UInt:D \N --> Bool:D) { + my UInt $n = N.sqrt.UInt; + N == $n * $n; +} + +sub MAIN(UInt:D \N) { + my UInt $min = ('1' ~ '0' x N-1).UInt; + my UInt $max = ('9' x N).UInt; + for $min..$max -> $m { + my UInt $n = $m.flip.UInt; + next if $n > $m; + + put $m if is-square($m + $n) and is-square($m - $n); + } +} diff --git a/challenge-102/feng-chang/raku/ch-2.raku b/challenge-102/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..394fede9ae --- /dev/null +++ b/challenge-102/feng-chang/raku/ch-2.raku @@ -0,0 +1,15 @@ +#!/bin/env raku + +sub MAIN(UInt:D \N) { + my Str:D $s = '#'; + my UInt:D $pos = N; + + while $s.chars < N { + $s = $pos ~ $s; + $pos = N - $s.chars; + + $s = '#' ~ $s if $s.chars < N; + } + + put $s; +} 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; + |
