diff options
Diffstat (limited to 'challenge-113/abigail')
| -rw-r--r-- | challenge-113/abigail/awk/ch-1.awk | 16 | ||||
| -rw-r--r-- | challenge-113/abigail/bash/ch-1.sh | 31 | ||||
| -rw-r--r-- | challenge-113/abigail/c/ch-1.c | 16 | ||||
| -rw-r--r-- | challenge-113/abigail/lua/ch-1.lua | 30 | ||||
| -rw-r--r-- | challenge-113/abigail/node/ch-1.js | 16 | ||||
| -rw-r--r-- | challenge-113/abigail/perl/ch-1.pl | 65 | ||||
| -rw-r--r-- | challenge-113/abigail/python/ch-1.py | 17 | ||||
| -rw-r--r-- | challenge-113/abigail/ruby/ch-1.rb | 18 |
8 files changed, 111 insertions, 98 deletions
diff --git a/challenge-113/abigail/awk/ch-1.awk b/challenge-113/abigail/awk/ch-1.awk index 91923aadb6..950a1052d0 100644 --- a/challenge-113/abigail/awk/ch-1.awk +++ b/challenge-113/abigail/awk/ch-1.awk @@ -8,19 +8,27 @@ # Run as: awk -f ch-1.awk < input-file # +# +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + BEGIN { - split ("0 1 2 1 0 2 6 3 8", tens) + split ("1 2 1 2 5 2 1 2 1", gcds) } { N = $1 D = $2 - D10 = D == 0 ? 100 : D * 10 - if (N >= D10 || N % (D == 0 ? 10 : D) == 0) { + if (D == 0) { + print (N >= 100 || N % 10 == 0 ? 1 : 0) + next + } + if (N >= 10 * D) { print 1 next } - for (i = 1; i <= tens [D]; i ++) { + for (i = 0; i < D / gcds [D]; i ++) { T = N - 10 * i - D if (T >= 0 && T % D == 0) { print 1 diff --git a/challenge-113/abigail/bash/ch-1.sh b/challenge-113/abigail/bash/ch-1.sh index 7183523485..d3e60eddf4 100644 --- a/challenge-113/abigail/bash/ch-1.sh +++ b/challenge-113/abigail/bash/ch-1.sh @@ -8,22 +8,31 @@ # Run as: bash ch-1.sh < input-file # -tens=(0 0 1 2 1 0 2 6 3 8) +# +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + +gcds=(0 1 2 1 2 5 2 1 2 1) while read N D -do ((D10 = D == 0 ? 100 : 10 * D)) - if ((N >= D10 || (N % (D == 0 ? 10 : D) == 0))) +do if ((D == 0)) + then if ((N >= 100 || N % 10 == 0)) + then echo 1 + else echo 0 + fi + continue + fi + if ((N >= D * 10)) then echo 1 continue fi - for ((i = 1; i <= ${tens[$D]}; i ++)) - do ((T = N - 10 * i - D)) - if ((T >= 0 && T % D == 0)) - then echo 1 - continue 2 - fi + for ((i = 0; i < D / gcds[D]; i ++)) + do ((T = N - 10 * i - D)) + if ((T >= 0 && T % D == 0)) + then echo 1 + continue 2 + fi done echo 0 done - - diff --git a/challenge-113/abigail/c/ch-1.c b/challenge-113/abigail/c/ch-1.c index e673fa7281..dac9106ef7 100644 --- a/challenge-113/abigail/c/ch-1.c +++ b/challenge-113/abigail/c/ch-1.c @@ -11,23 +11,31 @@ * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file */ +/* + * For a description of the algorithm, and the proof why this is correct: + * https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html + */ + typedef long long number; typedef short digit; -unsigned short tens [] = {0, 0, 1, 2, 1, 0, 2, 6, 3, 8}; +unsigned short gcds [] = {0, 1, 2, 1, 2, 5, 2, 1, 2, 1}; int main (void) { number N; digit D; while (scanf ("%lld %hd", &N, &D) == 2) { - digit D10 = D == 0 ? 100 : 10 * D; - if (N >= D10 || (N % (D ? D : 10) == 0)) { + if (D == 0) { + printf ("%d\n", N >= 100 || N % 10 == 0 ? 1 : 0); + continue; + } + if (N >= D * 10) { printf ("1\n"); continue; } bool valid = false; - for (unsigned short i = 1; i <= tens [D]; i ++) { + for (unsigned short i = 0; i < D / gcds [D]; i ++) { number T = N - 10 * i - D; if (T >= 0 && T % D == 0) { printf ("1\n"); diff --git a/challenge-113/abigail/lua/ch-1.lua b/challenge-113/abigail/lua/ch-1.lua index 36bd8e8afd..4de70d106c 100644 --- a/challenge-113/abigail/lua/ch-1.lua +++ b/challenge-113/abigail/lua/ch-1.lua @@ -8,29 +8,35 @@ -- Run as: lua ch-1.lua < input-file -- -local tens = {0, 1, 2, 1, 0, 2, 6, 3, 8} +-- +-- For a description of the algorithm, and the proof why this is correct: +-- https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +-- + +local gcds = {1, 2, 1, 2, 5, 2, 1, 2, 1} for line in io . lines () do local _, _, N, D = line : find ("([0-9]+)%s+([0-9])") N = tonumber (N) D = tonumber (D) - local D10 = 10 * D if D == 0 - then D10 = 100 + then if N >= 100 or N % 10 == 0 + then print (1) + else print (0) + end + goto end_loop end - if (N >= D10) or (D == 0 and N % 10 == 0) - or (D > 0 and N % D == 0) + + if N >= D * 10 then print (1) goto end_loop end - if D > 0 - then for i = 1, tens [D] - do local T = N - 10 * i - D - if T >= 0 and T % D == 0 - then print (1) - goto end_loop - end + for i = 0, D / gcds [D] - 1 + do local T = N - 10 * i - D + if T >= 0 and T % D == 0 + then print (1) + goto end_loop end end diff --git a/challenge-113/abigail/node/ch-1.js b/challenge-113/abigail/node/ch-1.js index 2ca4f6a0e1..e755524470 100644 --- a/challenge-113/abigail/node/ch-1.js +++ b/challenge-113/abigail/node/ch-1.js @@ -8,18 +8,26 @@ // Run as: node ch-1.js < input-file // -let tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8]; +// +// For a description of the algorithm, and the proof why this is correct: +// https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +// + +let gcds = [0, 1, 2, 1, 2, 5, 2, 1, 2, 1]; require ('readline') . createInterface ({input: process . stdin}) . on ('line', _ => { let [N, D] = _ . split (/\s+/) . map (_ => +_) - let D10 = D == 0 ? 100 : 10 * D - if (N >= D10 || (N % (D == 0 ? 10 : D) == 0)) { + if (D == 0) { + console . log (N >= 100 || N % 10 == 0 ? 1 : 0) + return + } + if (N >= D * 10) { console . log (1) return } - for (let i = 1; i <= tens [D]; i ++) { + for (let i = 0; i < D / gcds [D]; i ++) { let T = N - 10 * i - D if (T >= 0 && T % D == 0) { console . log (1) diff --git a/challenge-113/abigail/perl/ch-1.pl b/challenge-113/abigail/perl/ch-1.pl index bb45261f33..78a0eb0947 100644 --- a/challenge-113/abigail/perl/ch-1.pl +++ b/challenge-113/abigail/perl/ch-1.pl @@ -18,68 +18,23 @@ use experimental 'lexical_subs'; # # -# For any of the digits $D, more numbers $N can be written as a sum -# of positive integers, each containing the digit $D at least once. +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html # -# Let $D10 = $D == 0 ? 100 : $D * 10. -# -# It should be obvious that if $D > 0 and $N >= $D10, we can write $N as a -# sum of integers each containing at least one $D: -# -# For $D > 0: -# Let $N = $D * k + i, k >= 10 and 0 <= i < $D, -# then $N = $D10 + i + (k - 10) * $D. -# -# $D10 + i is a number containing exactly one $D, so $N can be -# written as a sum of (k - 9) integers: 10 * $D + 1 and k - 10 $D's. -# -# For $D = 0: -# Let $N = 100 + i + k * 10, for some 0 <= i < 10. -# -# 100 + i will contain a 0, so $N can be written as a sum of -# integers each containing a 0. -# -# It should also be clear that for $D > 0, if $N % $D == 0, $N can be -# written as a sum of integers containing $D, as $N is then a multiple of $D. -# -# For $N < $D10, $N can be written as a sum of positive integers -# containing at least one $D, iff: -# -# 1) $N is divisible by $D (or $D10 if $D == 0) OR -# 2) $N - 10 * l is non-negative and divisible by $D, -# for some 0 < l < $D. -# -# In case 1), $N is the sum of $N/$D $Ds. -# In case 2), $N is the sum of 10 * l + $D, and ($N - 10 * l - $D)/$D $Ds. -# -# -# We can further restrict which ls we have to check for a given $D: -# -# $D | l -# -----+----- -# 0 | -# 1 | -# 2 | 1 -# 3 | 1, 2 -# 4 | 1 -# 5 | -# 6 | 1, 2 -# 7 | 1, 2, 3, 4, 5, 6 -# 8 | 1, 2, 3 -# 9 | 1, 2, 3, 4, 5, 6, 7, 8 -# -# These are all the ls < $D so that $D does not divide 10 * l. -my @tens = (0, 0, 1, 2, 1, 0, 2, 6, 3, 8); +my @gcds = (0, 1, 2, 1, 2, 5, 2, 1, 2, 1); MAIN: while (<>) { - my ($N, $D) = /[0-9]+/g; - my $D10 = $D == 0 ? 100 : 10 * $D; - if ($N >= $D10 || ($N % ($D || 10) == 0)) { + my ($N, $D) = split; + if ($D == 0) { + say $N >= 100 || $N % 10 == 0 ? 1 : 0; + next MAIN; + } + if ($N >= $D * 10) { say 1; next MAIN; } - for (my $i = 1; $i <= $tens [$D]; $i ++) { + for (my $i = 0; $i < $D / $gcds [$D]; $i ++) { my $T = $N - 10 * $i - $D; if ($T >= 0 && $T % $D == 0) { say 1; diff --git a/challenge-113/abigail/python/ch-1.py b/challenge-113/abigail/python/ch-1.py index 7bf1b0db11..ebb8821ddc 100644 --- a/challenge-113/abigail/python/ch-1.py +++ b/challenge-113/abigail/python/ch-1.py @@ -8,20 +8,29 @@ # Run as: python ch-1.py < input-file # +# +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + import fileinput -tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8] +gcds = [0, 1, 2, 1, 2, 5, 2, 1, 2, 1] for line in fileinput . input (): (N, D) = line . split (); N = int (N) D = int (D) - D10 = 100 if D == 0 else 10 * D - if N >= D10 or (D == 0 and N % 10 == 0) or (D > 0 and N % D == 0): + if D == 0: + print (1 if N >= 100 or N % 10 == 0 else 0) + continue + + if N >= D * 10: print (1) continue + done = False - for i in range (1, tens [D] + 1): + for i in range (0, D // gcds [D]): T = N - 10 * i - D if T >= 0 and T % D == 0: print (1) diff --git a/challenge-113/abigail/ruby/ch-1.rb b/challenge-113/abigail/ruby/ch-1.rb index 4f44bf5130..dadd1f1a9c 100644 --- a/challenge-113/abigail/ruby/ch-1.rb +++ b/challenge-113/abigail/ruby/ch-1.rb @@ -8,20 +8,30 @@ # Run as: ruby ch-1.rb < input-file # -tens = [0, 0, 1, 2, 1, 0, 2, 6, 3, 8] +# +# For a description of the algorithm, and the proof why this is correct: +# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +# + +gcds = [0, 1, 2, 1, 2, 5, 2, 1, 2, 1] ARGF . each_line do | line | n, d = line . split n = n . to_i d = d . to_i - d10 = d == 0 ? 100 : d * 10 - if n >= d10 || n % (d == 0 ? 10 : d) == 0 + if d == 0 + puts (n >= 100 || n % 10 == 0 ? 1 : 0) + next + end + + if n >= d * 10 then puts (1) next end + done = false - for i in 1 .. tens [d] do + for i in 0 .. d / gcds [d] - 1 do t = n - 10 * i - d if t >= 0 && t % d == 0 then puts (1) |
