From fb39699b31d65405e7d104ee2b1c1c4edcdf51a8 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 23 Nov 2021 16:00:43 +0100 Subject: Solutions for week 140, part 1. --- challenge-140/abigail/README.md | 1 - challenge-140/abigail/awk/ch-1.awk | 38 +++++++++++++++++++++ challenge-140/abigail/awk/ch-2.awk | 29 ++++++++++++++++ challenge-140/abigail/bash/ch-1.sh | 42 +++++++++++++++++++++++ challenge-140/abigail/bash/ch-2.sh | 32 ++++++++++++++++++ challenge-140/abigail/bc/ch-1.bc | 18 ++++++++++ challenge-140/abigail/bc/ch-2.bc | 29 ++++++++++++++++ challenge-140/abigail/c/ch-1.c | 62 ++++++++++++++++++++++++++++++++++ challenge-140/abigail/c/ch-2.c | 39 +++++++++++++++++++++ challenge-140/abigail/go/ch-1.go | 32 ++++++++++++++++++ challenge-140/abigail/go/ch-2.go | 37 ++++++++++++++++++++ challenge-140/abigail/java/ch-1.java | 22 ++++++++++++ challenge-140/abigail/java/ch-2.java | 33 ++++++++++++++++++ challenge-140/abigail/lua/ch-1.lua | 28 +++++++++++++++ challenge-140/abigail/lua/ch-2.lua | 30 ++++++++++++++++ challenge-140/abigail/node/ch-1.js | 17 ++++++++++ challenge-140/abigail/node/ch-2.js | 28 +++++++++++++++ challenge-140/abigail/pascal/ch-1.p | 40 ++++++++++++++++++++++ challenge-140/abigail/pascal/ch-2.p | 36 ++++++++++++++++++++ challenge-140/abigail/python/ch-1.py | 15 ++++++++ challenge-140/abigail/python/ch-2.py | 25 ++++++++++++++ challenge-140/abigail/r/ch-1.r | 22 ++++++++++++ challenge-140/abigail/r/ch-2.r | 33 ++++++++++++++++++ challenge-140/abigail/ruby/ch-1.rb | 15 ++++++++ challenge-140/abigail/ruby/ch-2.rb | 27 +++++++++++++++ challenge-140/abigail/scheme/ch-1.scm | 41 ++++++++++++++++++++++ challenge-140/abigail/scheme/ch-2.scm | 62 ++++++++++++++++++++++++++++++++++ challenge-140/abigail/t/ctest.ini | 11 ++++++ challenge-140/abigail/t/input-1-1 | 3 ++ challenge-140/abigail/t/input-2-1 | 2 ++ challenge-140/abigail/t/output-1-1.exp | 3 ++ challenge-140/abigail/t/output-2-1.exp | 2 ++ challenge-140/abigail/tcl/ch-1.tcl | 29 ++++++++++++++++ challenge-140/abigail/tcl/ch-2.tcl | 24 +++++++++++++ 34 files changed, 906 insertions(+), 1 deletion(-) create mode 100644 challenge-140/abigail/awk/ch-1.awk create mode 100644 challenge-140/abigail/awk/ch-2.awk create mode 100644 challenge-140/abigail/bash/ch-1.sh create mode 100644 challenge-140/abigail/bash/ch-2.sh create mode 100644 challenge-140/abigail/bc/ch-1.bc create mode 100644 challenge-140/abigail/bc/ch-2.bc create mode 100644 challenge-140/abigail/c/ch-1.c create mode 100644 challenge-140/abigail/c/ch-2.c create mode 100644 challenge-140/abigail/go/ch-1.go create mode 100644 challenge-140/abigail/go/ch-2.go create mode 100644 challenge-140/abigail/java/ch-1.java create mode 100644 challenge-140/abigail/java/ch-2.java create mode 100644 challenge-140/abigail/lua/ch-1.lua create mode 100644 challenge-140/abigail/lua/ch-2.lua create mode 100644 challenge-140/abigail/node/ch-1.js create mode 100644 challenge-140/abigail/node/ch-2.js create mode 100644 challenge-140/abigail/pascal/ch-1.p create mode 100644 challenge-140/abigail/pascal/ch-2.p create mode 100644 challenge-140/abigail/python/ch-1.py create mode 100644 challenge-140/abigail/python/ch-2.py create mode 100644 challenge-140/abigail/r/ch-1.r create mode 100644 challenge-140/abigail/r/ch-2.r create mode 100644 challenge-140/abigail/ruby/ch-1.rb create mode 100644 challenge-140/abigail/ruby/ch-2.rb create mode 100644 challenge-140/abigail/scheme/ch-1.scm create mode 100644 challenge-140/abigail/scheme/ch-2.scm create mode 100644 challenge-140/abigail/t/ctest.ini create mode 100644 challenge-140/abigail/t/input-1-1 create mode 100644 challenge-140/abigail/t/input-2-1 create mode 100644 challenge-140/abigail/t/output-1-1.exp create mode 100644 challenge-140/abigail/t/output-2-1.exp create mode 100644 challenge-140/abigail/tcl/ch-1.tcl create mode 100644 challenge-140/abigail/tcl/ch-2.tcl diff --git a/challenge-140/abigail/README.md b/challenge-140/abigail/README.md index bb2b1dda43..0707d1fbd5 100644 --- a/challenge-140/abigail/README.md +++ b/challenge-140/abigail/README.md @@ -6,7 +6,6 @@ * [Bash](bash/ch-1.sh) * [Bc](bc/ch-1.bc) * [C](c/ch-1.c) -* [Erlang](c/ch-1.erl) * [Go](go/ch-1.go) * [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) diff --git a/challenge-140/abigail/awk/ch-1.awk b/challenge-140/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..3210e7b108 --- /dev/null +++ b/challenge-140/abigail/awk/ch-1.awk @@ -0,0 +1,38 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +# +# Take a binary representation, return its decimal equivalent +# +function bin2dec (bin, dec, digits, n) { + dec = 0 + n = split (bin, digits, "") + for (i = 1; i <= n; i ++) { + dec = 2 * dec + digits [i] + } + return (dec) +} + + +# +# Get a binary representation +# +function dec2bin (dec, bin) { + while (dec) { + bin = dec % 2 bin + dec = int (dec / 2) + } + return (bin) +} + + +{ + print dec2bin(bin2dec($1) + bin2dec($2)) +} diff --git a/challenge-140/abigail/awk/ch-2.awk b/challenge-140/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..32372950cc --- /dev/null +++ b/challenge-140/abigail/awk/ch-2.awk @@ -0,0 +1,29 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + i = $1 + j = $2 + k = $3 + + n = 0 + while (k > 0) { + n ++ + s = int (sqrt (n)) + for (d = 1; d <= s && k > 0; d ++) { + if (!(n % d)) { + k -= (d <= i && n / d <= j) + \ + (d <= j && n / d <= i) - (n == d * d) + } + } + } + print (n) +} + diff --git a/challenge-140/abigail/bash/ch-1.sh b/challenge-140/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..24b57c5079 --- /dev/null +++ b/challenge-140/abigail/bash/ch-1.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +# +# Take a binary representation, return its decimal equivalent +# +function bin2dec () { + local bin=$1 + local i + ((dec = 0)) + for ((i = 0; i < ${#bin}; i ++)) { + ((dec = 2 * dec + ${bin:$i:1})) + } +} + +# +# Given a decimal number, return its binary representation +# +function dec2bin () { + local dec=$1 + bin="" + while ((dec > 0)) + do bin=$((dec % 2))$bin + ((dec /= 2)) + done +} + +set -f + +while read a b +do bin2dec $a; a=$dec + bin2dec $b; b=$dec + dec2bin $((a + b)) + echo $bin +done diff --git a/challenge-140/abigail/bash/ch-2.sh b/challenge-140/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..16757fccb3 --- /dev/null +++ b/challenge-140/abigail/bash/ch-2.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +while read i j k +do ((n = 0)) + while ((k > 0)) + do ((n ++)) + for ((d = 1; d * d <= n && k > 0; d ++)) + do if ((n % d == 0)) + then if ((d <= i && n / d <= j)) + then ((k --)) + fi + if ((d <= j && n / d <= i)) + then ((k --)) + fi + if ((n == d * d)) + then ((k ++)) + fi + fi + done + done + echo $n +done diff --git a/challenge-140/abigail/bc/ch-1.bc b/challenge-140/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..9e1fe422ad --- /dev/null +++ b/challenge-140/abigail/bc/ch-1.bc @@ -0,0 +1,18 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# + +obase=2 +ibase=2 + +while (1) { + a = read(); if (a == 0) {break} + b = read(); if (b == 0) {break} + a + b +} + +quit diff --git a/challenge-140/abigail/bc/ch-2.bc b/challenge-140/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..bb4f56c509 --- /dev/null +++ b/challenge-140/abigail/bc/ch-2.bc @@ -0,0 +1,29 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc < input-file +# + +while (1) { + i = read (); if (i == 0) {break} + j = read (); if (j == 0) {break} + k = read (); if (k == 0) {break} + + n = 0 + while (k > 0) { + n = n + 1 + for (d = 1; d * d <= n && k > 0; d ++) { + if (n % d == 0) { + if (d <= i && n / d <= j) {k = k - 1} + if (d <= j && n / d <= i) {k = k - 1} + if (n == d * d) {k = k + 1} + } + } + } + + n +} + +quit diff --git a/challenge-140/abigail/c/ch-1.c b/challenge-140/abigail/c/ch-1.c new file mode 100644 index 0000000000..6c9024bda9 --- /dev/null +++ b/challenge-140/abigail/c/ch-1.c @@ -0,0 +1,62 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +/* + * Given a decimal number, return its binary representation + */ +char * dec2bin (long dec) { + char * bin; + if ((bin = (char *) malloc (32 * sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + if (dec == 0) { /* Special case dec == 0 */ + bin [0] = '0'; + bin [1] = '\0'; + return (bin); + } + size_t i = 0; + while (dec) { + bin [i ++] = '0' + dec % 2; + dec /= 2; + } + /* + * Reverse the string + */ + for (size_t j = 0; j < i / 2; j ++) { + char t = bin [j]; + bin [j] = bin [i - j - 1]; + bin [i - j - 1] = t; + } + + bin [i] = '\0'; + return (bin); +} + + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + char * end_ptr; + long a = strtol (line, &end_ptr, 2); + long b = strtol (end_ptr, NULL, 2); + char * bin = dec2bin (a + b); + printf ("%s\n", bin); + free (bin); + } + free (line); + + return (0); +} diff --git a/challenge-140/abigail/c/ch-2.c b/challenge-140/abigail/c/ch-2.c new file mode 100644 index 0000000000..b57fb1d955 --- /dev/null +++ b/challenge-140/abigail/c/ch-2.c @@ -0,0 +1,39 @@ +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int main (void) { + long i, j; + long long k; + + while (scanf ("%ld%ld%lld", &i, &j, &k) == 3) { + long long n = 0; + while (k > 0) { + long s = lrintf (sqrt (++ n)); + for (long d = 1; d <= s && k > 0; d ++) { + /* + * Subtract the number of divisors d, where d <= i, + * and n / d <= j, or d <= j and n / d <= i. + * A number d is a divisor if n % d == 0. + * We should count a divisor only once if n == d * d. + */ + if (!(n % d)) { + k -= (d <= i && n / d <= j) + + (d <= j && n / d <= i) - (n == d * d); + } + } + } + printf ("%lld\n", n); + } + + return (0); +} diff --git a/challenge-140/abigail/go/ch-1.go b/challenge-140/abigail/go/ch-1.go new file mode 100644 index 0000000000..c355358d16 --- /dev/null +++ b/challenge-140/abigail/go/ch-1.go @@ -0,0 +1,32 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "bufio" + "os" + "strconv" + "strings" +) + +func main () { + var reader = bufio . NewReader (os. Stdin) + for { + var text, err = reader . ReadString ('\n') + if (err != nil) { + break + } + bins := strings . Split (strings . Trim (text, "\n"), " ") + a, _ := strconv . ParseInt (bins [0], 2, 0) + b, _ := strconv . ParseInt (bins [1], 2, 0) + + fmt . Println (strconv . FormatInt (a + b, 2)) + } +} diff --git a/challenge-140/abigail/go/ch-2.go b/challenge-140/abigail/go/ch-2.go new file mode 100644 index 0000000000..0f35815237 --- /dev/null +++ b/challenge-140/abigail/go/ch-2.go @@ -0,0 +1,37 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" + "math" +) + +func main () { + for { + var i, j, k int64 + c, err := fmt . Scanf ("%d %d %d", &i, &j, &k) + if (c != 3 || err != nil) { + break; + } + n := int64 (0) + for ;k > 0; { + n ++ + s := int64 (math . Sqrt (float64 (n))) + for d := int64 (1); d <= s && k > 0; d ++ { + if (n % d == 0) { + if (d <= i && n / d <= j) {k --} + if (d <= j && n / d <= i) {k --} + if (n == d * d) {k ++} + } + } + } + fmt . Println (n) + } +} diff --git a/challenge-140/abigail/java/ch-1.java b/challenge-140/abigail/java/ch-1.java new file mode 100644 index 0000000000..7d93af1d7d --- /dev/null +++ b/challenge-140/abigail/java/ch-1.java @@ -0,0 +1,22 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextLine ()) { + String line = scanner . nextLine (); + String [] parts = line . trim () . split (" "); + int a = Integer . parseInt (parts [0], 2); + int b = Integer . parseInt (parts [1], 2); + System . out . println (Integer . toBinaryString (a + b)); + } + } +} diff --git a/challenge-140/abigail/java/ch-2.java b/challenge-140/abigail/java/ch-2.java new file mode 100644 index 0000000000..90c4bd998f --- /dev/null +++ b/challenge-140/abigail/java/ch-2.java @@ -0,0 +1,33 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file +// + +import java.util.*; + +public class ch2 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextLong ()) { + long i = scanner . nextLong (); + long j = scanner . nextLong (); + long k = scanner . nextLong (); + long n = 0; + while (k > 0) { + n ++; + long s = (long) Math . sqrt (n); + for (long d = 1; d <= s && k > 0; d ++) { + if (n % d == 0) { + if (d <= i && n / d <= j) {k --;} + if (d <= j && n / d <= i) {k --;} + if (n == d * d) {k ++;} + } + } + } + System . out . println (n); + } + } +} diff --git a/challenge-140/abigail/lua/ch-1.lua b/challenge-140/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..b0ad7ae3fd --- /dev/null +++ b/challenge-140/abigail/lua/ch-1.lua @@ -0,0 +1,28 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +function dec2bin (dec) + local bin = {} + local out = "" + while dec > 0 do + bin [#bin + 1] = dec % 2 + dec = math . floor (dec / 2) + end + for i = #bin, 1, -1 do + out = out .. bin [i] + end + return out +end + + +for line in io . lines () do + _, _, a, b = line : find ("([01]+)%s+([01]+)") + print (dec2bin (tonumber (a, 2) + tonumber (b, 2))) +end diff --git a/challenge-140/abigail/lua/ch-2.lua b/challenge-140/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..601fc35041 --- /dev/null +++ b/challenge-140/abigail/lua/ch-2.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + local _, _, i, j, k = line : find ("([0-9]+)%s+([0-9]+)%s+([0-9]+)") + i = tonumber (i) + j = tonumber (j) + k = tonumber (k) + + local n = 0 + while k > 0 do + n = n + 1 + local s = math . floor (math . sqrt (n)) + for d = 1, s do + if n % d == 0 then + if d <= i and n / d <= j then k = k - 1 end + if d <= j and n / d <= i then k = k - 1 end + if n == d * d then k = k + 1 end + end + end + end + print (n) +end diff --git a/challenge-140/abigail/node/ch-1.js b/challenge-140/abigail/node/ch-1.js new file mode 100644 index 0000000000..18dc51e939 --- /dev/null +++ b/challenge-140/abigail/node/ch-1.js @@ -0,0 +1,17 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [a, b] = line . trim () . split (" "); + console . log ((parseInt (a, 2) + parseInt (b, 2)) . toString (2)) +}) + diff --git a/challenge-140/abigail/node/ch-2.js b/challenge-140/abigail/node/ch-2.js new file mode 100644 index 0000000000..711c05f7de --- /dev/null +++ b/challenge-140/abigail/node/ch-2.js @@ -0,0 +1,28 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [i, j, k] = line . split (" ") . map (_ => +_) + let n = 0 + while (k > 0) { + n ++ + let s = Math . floor (Math . sqrt (n)) + for (let d = 1; d <= s && k > 0; d ++) { + if (n % d == 0) { + if (d <= i && n / d <= j) {k --} + if (d <= j && n / d <= i) {k --} + if (n == d * d) {k ++} + } + } + } + console . log (n) +}) diff --git a/challenge-140/abigail/pascal/ch-1.p b/challenge-140/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..57e691e9c0 --- /dev/null +++ b/challenge-140/abigail/pascal/ch-1.p @@ -0,0 +1,40 @@ +Program ch1; + +uses Strutils; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +function bin2dec (bin: string): longint; + var + c: char; + dec: integer = 0; + + begin + for c in bin do begin + dec := dec * 2; + if c = '1' then begin + dec := dec + 1; + end; + end; + bin2dec := dec; + end; + +var + n1, n2: longint; + a, b: string; + +begin + while not eof do begin + readln (n1, n2); + a := Dec2Numb (n1, 1, 10); + b := Dec2Numb (n2, 1, 10); + + writeln (Dec2Numb (bin2dec (a) + bin2dec (b), 1, 2)); + end +end. diff --git a/challenge-140/abigail/pascal/ch-2.p b/challenge-140/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..9eb1661bfc --- /dev/null +++ b/challenge-140/abigail/pascal/ch-2.p @@ -0,0 +1,36 @@ +Program ch2; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +uses + math; + +var + i, j, k, n, s, d: longint; + +begin + while not eof do begin + readln (i, j, k); + + n := 0; + + while k > 0 do begin + inc (n); + s := round (sqrt (n)); + for d := 1 to s do begin + if n mod d = 0 then begin + if (d <= i) and (n / d <= j) then begin dec (k); end; + if (d <= j) and (n / d <= i) then begin dec (k); end; + if (n = d * d) then begin inc (k); end; + end + end + end; + writeln (n); + end +end. diff --git a/challenge-140/abigail/python/ch-1.py b/challenge-140/abigail/python/ch-1.py new file mode 100644 index 0000000000..7002b13c03 --- /dev/null +++ b/challenge-140/abigail/python/ch-1.py @@ -0,0 +1,15 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + a, b = line . strip () . split (" ") + print (bin (int (a, 2) + int (b, 2)) [2:]) diff --git a/challenge-140/abigail/python/ch-2.py b/challenge-140/abigail/python/ch-2.py new file mode 100644 index 0000000000..a7bdccbb49 --- /dev/null +++ b/challenge-140/abigail/python/ch-2.py @@ -0,0 +1,25 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput +import math + +for line in fileinput . input (): + i, j, k = map (lambda x: int (x), line . strip () . split (" ")) + n = 0 + while k > 0: + n = n + 1 + s = math . floor (math . sqrt (n)) + for d in range (1, s + 1): + if n % d == 0: + if d <= i and n / d <= j: k = k - 1 + if d <= j and n / d <= i: k = k - 1 + if n == d * d: k = k + 1 + print (n) diff --git a/challenge-140/abigail/r/ch-1.r b/challenge-140/abigail/r/ch-1.r new file mode 100644 index 0000000000..10fd49abb3 --- /dev/null +++ b/challenge-140/abigail/r/ch-1.r @@ -0,0 +1,22 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file +# + +stdin <- file ('stdin', 'r') +repeat { + line <- readLines (stdin, n = 1) + if (length (line) == 0) { + break + } + + parts <- strsplit (line, " ") + a <- strtoi (parts [[1]] [[1]], 2) + b <- strtoi (parts [[1]] [[2]], 2) + + cat (as.integer (paste (as.integer (rev (intToBits (a + b))), + collapse = "")), "\n") +} diff --git a/challenge-140/abigail/r/ch-2.r b/challenge-140/abigail/r/ch-2.r new file mode 100644 index 0000000000..a83d05ccce --- /dev/null +++ b/challenge-140/abigail/r/ch-2.r @@ -0,0 +1,33 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-2.r < input-file +# + +stdin <- file ('stdin', 'r') +repeat { + line <- readLines (stdin, n = 1) + if (length (line) == 0) { + break + } + parts <- strsplit (line, " ") + list <- as.numeric (parts [[1]]) + i <- list [[1]] + j <- list [[2]] + k <- list [[3]] + n <- 0 + while (k > 0) { + n <- n + 1 + s <- floor (sqrt (n)) + for (d in 1 : s) { + if (n %% d == 0) { + if (d <= i && n / d <= j) {k = k - 1} + if (d <= j && n / d <= i) {k = k - 1} + if (n == d * d) {k = k + 1} + } + } + } + cat (sprintf ("%d\n", n)) +} diff --git a/challenge-140/abigail/ruby/ch-1.rb b/challenge-140/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..bc95be2323 --- /dev/null +++ b/challenge-140/abigail/ruby/ch-1.rb @@ -0,0 +1,15 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + | line | + a, b = line . strip() . split (" ") + puts (a . to_i(2) + b . to_i(2)) . to_s(2) +end diff --git a/challenge-140/abigail/ruby/ch-2.rb b/challenge-140/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..fe2f27a992 --- /dev/null +++ b/challenge-140/abigail/ruby/ch-2.rb @@ -0,0 +1,27 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +ARGF . each_line do + | line | + i, j, k = line . strip() . split . map {|x| x . to_i} + n = 0 + while k > 0 do + n = n + 1 + s = Math . sqrt(n) . floor() + for d in 1 .. s do + if n % d == 0 then + if d <= i && n / d <= j then k = k - 1 end + if d <= j && n / d <= i then k = k - 1 end + if n == d * d then k = k + 1 end + end + end + end + puts (n) +end diff --git a/challenge-140/abigail/scheme/ch-1.scm b/challenge-140/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..45c7e0fc71 --- /dev/null +++ b/challenge-140/abigail/scheme/ch-1.scm @@ -0,0 +1,41 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + +(use-modules (ice-9 rdelim)) +(use-modules (srfi srfi-1)) + +(define (bin2dec bin) + (define len (string-length bin)) + (cond ((= len 0) 0) + (else (+ (string->number (string-take-right bin 1)) + (* 2 (bin2dec (string-drop-right bin 1))))))) + +(define (_dec2bin dec) + (cond ((= dec 0) "") + (else (string-concatenate + (list (_dec2bin (floor-quotient dec 2)) + (number->string (modulo dec 2))))))) + +(define (dec2bin dec) + (cond ((= dec 0) "0") + (else (_dec2bin dec)))) + +(define (main) + (define line (read-line)) + (define parts) + (if (not (eof-object? line)) + (begin + (display (dec2bin (fold + 0 (map bin2dec (string-split line #\ ))))) + (newline) + (main) + ) + ) +) + + +(main) diff --git a/challenge-140/abigail/scheme/ch-2.scm b/challenge-140/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..c38d93c582 --- /dev/null +++ b/challenge-140/abigail/scheme/ch-2.scm @@ -0,0 +1,62 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm < input-file +;;; + +(use-modules (ice-9 rdelim)) +(use-modules (srfi srfi-1)) + +;; +;; Return the number of divisors of n, where the divisors don't +;; exceed neither i nor j +;; +(define (divisors n i j) + (define s (inexact->exact (round (sqrt n)))) + (define c 0) + (do ((d 1 (1+ d))) + ((> d s)) + (if (= (modulo n d) 0) + (begin + (if (and (<= d i) (<= (/ n d) j)) (set! c (+ c 1))) + (if (and (<= d j) (<= (/ n d) i)) (set! c (+ c 1))) + (if (= n (* d d)) (set! c (- c 1))) + ) + ) + ) + c +) + + + +(define (find-kth list) + (define i (list-ref list 0)) + (define j (list-ref list 1)) + (define k (list-ref list 2)) + (define n 0) + (while (> k 0) + (begin + (set! n (+ 1 n)) + (set! k (- k (divisors n i j))) + ) + ) + n +) + +(define (main) + (define line (read-line)) + (define parts) + (if (not (eof-object? line)) + (begin + (display (find-kth (map string->number (string-split line #\ )))) + (newline) + (main) + ) + ) +) + +;; (display (divisors 12 10 10))(newline) + +(main) diff --git a/challenge-140/abigail/t/ctest.ini b/challenge-140/abigail/t/ctest.ini new file mode 100644 index 0000000000..a0b58af962 --- /dev/null +++ b/challenge-140/abigail/t/ctest.ini @@ -0,0 +1,11 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +2-1 = Given Examples + +[1-1/bc] +add_to_input = 0 diff --git a/challenge-140/abigail/t/input-1-1 b/challenge-140/abigail/t/input-1-1 new file mode 100644 index 0000000000..d481afb409 --- /dev/null +++ b/challenge-140/abigail/t/input-1-1 @@ -0,0 +1,3 @@ +11 1 +101 1 +100 11 diff --git a/challenge-140/abigail/t/input-2-1 b/challenge-140/abigail/t/input-2-1 new file mode 100644 index 0000000000..c6793cdf64 --- /dev/null +++ b/challenge-140/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +2 3 4 +3 3 6 diff --git a/challenge-140/abigail/t/output-1-1.exp b/challenge-140/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..81e2d53716 --- /dev/null +++ b/challenge-140/abigail/t/output-1-1.exp @@ -0,0 +1,3 @@ +100 +110 +111 diff --git a/challenge-140/abigail/t/output-2-1.exp b/challenge-140/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..b94473479c --- /dev/null +++ b/challenge-140/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +3 +4 diff --git a/challenge-140/abigail/tcl/ch-1.tcl b/challenge-140/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..57a1b0fed3 --- /dev/null +++ b/challenge-140/abigail/tcl/ch-1.tcl @@ -0,0 +1,29 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +proc bin2dec bin { + set dec 0 + foreach c [split $bin {}] { + set dec [expr 2 * $dec + $c] + } + return $dec +} + +proc dec2bin dec { + set bin {} + while {$dec > 0} { + set bin [expr {$dec % 2}]$bin + set dec [expr {$dec / 2}] + } + return $bin +} + +while {[gets stdin line] >= 0} { + lassign [split $line " "] a b + puts [dec2bin [expr [bin2dec $a] + [bin2dec $b]]] +} diff --git a/challenge-140/abigail/tcl/ch-2.tcl b/challenge-140/abigail/tcl/ch-2.tcl new file mode 100644 index 0000000000..cc64678c3b --- /dev/null +++ b/challenge-140/abigail/tcl/ch-2.tcl @@ -0,0 +1,24 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-2.tcl < input-file +# + +while {[gets stdin line] >= 0} { + lassign [split $line " "] i j k + set n 0 + while {$k > 0} { + incr n + set s [expr isqrt ($n)] + for {set d 1} {$d <= $s} {incr d} { + if {$n % $d == 0} { + if {$d <= $i && $n / $d <= $j} {incr k -1} + if {$d <= $j && $n / $d <= $i} {incr k -1} + if {$n == $d * $d} {incr k} + } + } + } + puts $n +} -- cgit