diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-26 13:16:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-26 13:16:22 +0000 |
| commit | df966962c65252efa22bc8b686013c9c1b0f8680 (patch) | |
| tree | 2cf0b1edf0048ce1058d557b9d6a424e0f6fca2a | |
| parent | c58aa139f267a08aee64b3129f854b6e637022b3 (diff) | |
| parent | 5e0ab324c5a454538d112d0930d0fa15d6c8c4b7 (diff) | |
| download | perlweeklychallenge-club-df966962c65252efa22bc8b686013c9c1b0f8680.tar.gz perlweeklychallenge-club-df966962c65252efa22bc8b686013c9c1b0f8680.tar.bz2 perlweeklychallenge-club-df966962c65252efa22bc8b686013c9c1b0f8680.zip | |
Merge pull request #5283 from Abigail/abigail/week-140
Abigail/week 140
40 files changed, 1139 insertions, 1 deletions
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 <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * 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 <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <math.h> + +/* + * 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/perl/ch-1.pl b/challenge-140/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..94726166cd --- /dev/null +++ b/challenge-140/abigail/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK1 +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# This challenge is riddle with confusing directives. WTF is a +# "decimal-coded binary number"? Searching for that term on +# Google just returns results for Binary-coded decimals, which +# are well known. +# +# Second point: "simulate the addition of the given binary numbers". +# Uhm, virtually every general purpose computer made in the past +# 70 years does arithmetic using binary. Are we asked to simulate +# something which is already done? How? The examples just give answers, +# the answers are as closely related to spinach as they are to simulations. +# +# Third, it mentions operator overloading. Why? What? Without overloading +# binary "+" adds two numbers. We're asked to add two numbers. Why on earth +# would you want to overload an operator? +# + +# +# As the description of the challenge is pretty pointless, we have to +# resort to extract the purpose: +# - Give two binary numbers, add them, and print the result in binary +# +# Which means, this challenge is about translating to and from binary +# string representation. We use 'oct' for the latter, and sprintf for the +# former. +# +# This leaves us a one-liner: +# + +say sprintf "%b" => eval join " + " => map {oct "0b$_"} split while <>; diff --git a/challenge-140/abigail/perl/ch-2.pl b/challenge-140/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..91d052e4e5 --- /dev/null +++ b/challenge-140/abigail/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK2 +# + +# +# Run as: perl ch-2.pl < input-file +# + +# +# We consider all numbers, starting from 1. For each number $n, we will +# count how many times it occurs in the multiplication table. This +# is exactly the numbers of divisor $d, such that $d <= $i and $n / $d <= $j. +# Then it's just a matter of bookkeepping till we have found the right +# solution. +# +# Math::Prime::Util has a utility function, fordivisors, which iterates +# over the divisors of a given number. +# +# We also have two different solutions: +# +# A naive one, which calculates all numbers p = n * m, 1 <= n <= i, 1 <= m <= j, +# sorts the list, then selects the nth element. This is on ch-2b.pl. +# +# One which goes through the multiplication table in order, avoiding +# finding divisors of numbers. This is in ch-2a.pl. +# + +use Math::Prime::Util qw [fordivisors]; + +MAIN: while (<>) { + my ($n, $i, $j, $k) = (0, split); + fordivisors {$_ <= $i && $n / $_ <= $j && !-- $k && say $n} ++ $n + while $k >= 1; +} diff --git a/challenge-140/abigail/perl/ch-2a.pl b/challenge-140/abigail/perl/ch-2a.pl new file mode 100644 index 0000000000..bc3ef6f151 --- /dev/null +++ b/challenge-140/abigail/perl/ch-2a.pl @@ -0,0 +1,98 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK2 +# + +# |
