From 44bcd3ae0c35b887bd579e60c17198360f3497c3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 30 Nov 2021 20:04:35 +0100 Subject: Solutions week 141 --- challenge-141/abigail/README.md | 3 +- challenge-141/abigail/awk/ch-1.awk | 31 ++++++++++++++ challenge-141/abigail/awk/ch-2.awk | 32 ++++++++++++++ challenge-141/abigail/bash/ch-1.sh | 29 +++++++++++++ challenge-141/abigail/bash/ch-2.sh | 40 ++++++++++++++++++ challenge-141/abigail/bc/ch-1.bc | 28 +++++++++++++ challenge-141/abigail/c/ch-1.c | 35 ++++++++++++++++ challenge-141/abigail/c/ch-2.c | 42 +++++++++++++++++++ challenge-141/abigail/go/ch-1.go | 36 ++++++++++++++++ challenge-141/abigail/go/ch-2.go | 49 ++++++++++++++++++++++ challenge-141/abigail/java/ch-1.java | 32 ++++++++++++++ challenge-141/abigail/java/ch-2.java | 37 +++++++++++++++++ challenge-141/abigail/lua/ch-1.lua | 36 ++++++++++++++++ challenge-141/abigail/lua/ch-2.lua | 37 +++++++++++++++++ challenge-141/abigail/node/ch-1.js | 30 ++++++++++++++ challenge-141/abigail/node/ch-2.js | 32 ++++++++++++++ challenge-141/abigail/pascal/ch-1.p | 39 +++++++++++++++++ challenge-141/abigail/pascal/ch-2.p | 54 ++++++++++++++++++++++++ challenge-141/abigail/perl/ch-1.pl | 26 ++++++++++++ challenge-141/abigail/perl/ch-2.pl | 76 ++++++++++++++++++++++++++++++++++ challenge-141/abigail/python/ch-1.py | 30 ++++++++++++++ challenge-141/abigail/python/ch-2.py | 32 ++++++++++++++ challenge-141/abigail/r/ch-1.r | 32 ++++++++++++++ challenge-141/abigail/r/ch-2.r | 42 +++++++++++++++++++ challenge-141/abigail/ruby/ch-1.rb | 34 +++++++++++++++ challenge-141/abigail/ruby/ch-2.rb | 35 ++++++++++++++++ challenge-141/abigail/scheme/ch-1.scm | 31 ++++++++++++++ challenge-141/abigail/scheme/ch-2.scm | 40 ++++++++++++++++++ challenge-141/abigail/t/ctest.ini | 12 ++++++ challenge-141/abigail/t/input-1-1 | 0 challenge-141/abigail/t/input-2-1 | 2 + challenge-141/abigail/t/input-2-2 | 2 + challenge-141/abigail/t/output-1-1.exp | 10 +++++ challenge-141/abigail/t/output-2-1.exp | 2 + challenge-141/abigail/t/output-2-2.exp | 2 + challenge-141/abigail/tcl/ch-1.tcl | 31 ++++++++++++++ challenge-141/abigail/tcl/ch-2.tcl | 35 ++++++++++++++++ 37 files changed, 1094 insertions(+), 2 deletions(-) create mode 100644 challenge-141/abigail/awk/ch-1.awk create mode 100644 challenge-141/abigail/awk/ch-2.awk create mode 100644 challenge-141/abigail/bash/ch-1.sh create mode 100644 challenge-141/abigail/bash/ch-2.sh create mode 100644 challenge-141/abigail/bc/ch-1.bc create mode 100644 challenge-141/abigail/c/ch-1.c create mode 100644 challenge-141/abigail/c/ch-2.c create mode 100644 challenge-141/abigail/go/ch-1.go create mode 100644 challenge-141/abigail/go/ch-2.go create mode 100644 challenge-141/abigail/java/ch-1.java create mode 100644 challenge-141/abigail/java/ch-2.java create mode 100644 challenge-141/abigail/lua/ch-1.lua create mode 100644 challenge-141/abigail/lua/ch-2.lua create mode 100644 challenge-141/abigail/node/ch-1.js create mode 100644 challenge-141/abigail/node/ch-2.js create mode 100644 challenge-141/abigail/pascal/ch-1.p create mode 100644 challenge-141/abigail/pascal/ch-2.p create mode 100644 challenge-141/abigail/perl/ch-1.pl create mode 100644 challenge-141/abigail/perl/ch-2.pl create mode 100644 challenge-141/abigail/python/ch-1.py create mode 100644 challenge-141/abigail/python/ch-2.py create mode 100644 challenge-141/abigail/r/ch-1.r create mode 100644 challenge-141/abigail/r/ch-2.r create mode 100644 challenge-141/abigail/ruby/ch-1.rb create mode 100644 challenge-141/abigail/ruby/ch-2.rb create mode 100644 challenge-141/abigail/scheme/ch-1.scm create mode 100644 challenge-141/abigail/scheme/ch-2.scm create mode 100644 challenge-141/abigail/t/ctest.ini create mode 100644 challenge-141/abigail/t/input-1-1 create mode 100644 challenge-141/abigail/t/input-2-1 create mode 100644 challenge-141/abigail/t/input-2-2 create mode 100644 challenge-141/abigail/t/output-1-1.exp create mode 100644 challenge-141/abigail/t/output-2-1.exp create mode 100644 challenge-141/abigail/t/output-2-2.exp create mode 100644 challenge-141/abigail/tcl/ch-1.tcl create mode 100644 challenge-141/abigail/tcl/ch-2.tcl diff --git a/challenge-141/abigail/README.md b/challenge-141/abigail/README.md index 0707d1fbd5..0ebe05c131 100644 --- a/challenge-141/abigail/README.md +++ b/challenge-141/abigail/README.md @@ -2,7 +2,7 @@ ## Part 1 -* [GNU AWK](awk/ch-1.gawk) +* [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [Bc](bc/ch-1.bc) * [C](c/ch-1.c) @@ -22,7 +22,6 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [Bc](bc/ch-2.bc) * [C](c/ch-2.c) * [Go](go/ch-2.go) * [Java](java/ch-2.java) diff --git a/challenge-141/abigail/awk/ch-1.awk b/challenge-141/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..9b9ae8dc67 --- /dev/null +++ b/challenge-141/abigail/awk/ch-1.awk @@ -0,0 +1,31 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk +# + +BEGIN { + count = 10; + nr_of_divisors = 8; + + for (n = 1; count > 0; n ++) { + s = int sqrt (n); + if (n == s * s) { + continue + } + c = 0; + for (m = 1; m <= s && c <= nr_of_divisors; m ++) { + if (n % m == 0) { + c += 2 + } + } + if (c == nr_of_divisors) { + print (n) + count -- + } + } +} diff --git a/challenge-141/abigail/awk/ch-2.awk b/challenge-141/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..ac15121558 --- /dev/null +++ b/challenge-141/abigail/awk/ch-2.awk @@ -0,0 +1,32 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +function substrings (n, m, prefix, max, fc, tail, n_prefix) { + if (length (n) == 0) { + # + # End of recursion + # + return prefix > -1 && \ + prefix < max && \ + prefix % m == 0 ? 1 : 0 + } + + fc = substr (n, 1, 1) + tail = substr (n, 2) + n_prefix = 10 * (prefix == -1 ? 0 : prefix) + fc + + return substrings(tail, m, n_prefix, max) + \ + substrings(tail, m, prefix, max) +} + +{ + print (substrings($1, $2, -1, $1)) +} + diff --git a/challenge-141/abigail/bash/ch-1.sh b/challenge-141/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..c3d8fb5299 --- /dev/null +++ b/challenge-141/abigail/bash/ch-1.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh +# + + +((count = 10)) +((nr_of_divisors = 8)) + +for ((n = 1; count > 0; n ++)) +do ((c = 0)) + for ((d = 1; c <= nr_of_divisors && d * d <= n; d ++)) + do if ((n % d == 0)) + then ((c = c + 2)) + if ((d * d == n)) + then ((c = c - 1)) + fi + fi + done + if ((c == nr_of_divisors)) + then echo $n + ((count = count - 1)) + fi +done diff --git a/challenge-141/abigail/bash/ch-2.sh b/challenge-141/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..fdcf74406b --- /dev/null +++ b/challenge-141/abigail/bash/ch-2.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +function substrings () { + local n=$1 + local m=$2 + local prefix=$3 + local max=$4 + local r + if [ -z "$n" ] + then if (( prefix > -1 && prefix < max && prefix % m == 0)) + then ((r = 1)) + else ((r = 0)) + fi + else local tail=${n:1} + local fc=${n:0:1} + local n_prefix=$fc + if ((prefix > -1)) + then ((n_prefix = 10 * prefix + fc)) + fi + substrings "$tail" $m $n_prefix $max; local r1=$substrings + substrings "$tail" $m $prefix $max; local r2=$substrings + ((r = r1 + r2)) + fi + substrings=$r +} + +while read n m +do substrings $n $m -1 $n + echo $substrings +done diff --git a/challenge-141/abigail/bc/ch-1.bc b/challenge-141/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..d9f6ee0645 --- /dev/null +++ b/challenge-141/abigail/bc/ch-1.bc @@ -0,0 +1,28 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc +# + +count = 10 +nr_of_divisors = 8 + +for (n = 1; count > 0; n ++) { + c = 0 + for (m = 1; m * m <= n && c <= nr_of_divisors; m ++) { + if (n % m == 0) { + c = c + 2 + if (n == m * m) { + c = c - 1 + } + } + } + if (c == nr_of_divisors) { + n + count = count - 1 + } +} + +halt diff --git a/challenge-141/abigail/c/ch-1.c b/challenge-141/abigail/c/ch-1.c new file mode 100644 index 0000000000..ae9f916829 --- /dev/null +++ b/challenge-141/abigail/c/ch-1.c @@ -0,0 +1,35 @@ +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +# define COUNT 10 +# define NR_OF_DIVISORS 8 + +int main (void) { + int count = COUNT; + for (int n = 1; count > 0; n ++) { + long s = lrintf (sqrt (n)); + int m, c; + if (n == s * s) { + continue; + } + for (m = 1, c = 0; m <= s && c <= NR_OF_DIVISORS; m ++) { + if (n % m == 0) { + c += 2; + } + } + if (c == NR_OF_DIVISORS) { + printf ("%d\n", n); + count --; + } + } +} diff --git a/challenge-141/abigail/c/ch-2.c b/challenge-141/abigail/c/ch-2.c new file mode 100644 index 0000000000..10e729d8a3 --- /dev/null +++ b/challenge-141/abigail/c/ch-2.c @@ -0,0 +1,42 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + + + +int substrings (char * n, int m, int prefix, int max) { + int fc, n_prefix; + char * tail; + if (!* n) { + return prefix > -1 && prefix < max && prefix % m == 0; + } + + fc = * n - '0'; + tail = n + 1; + n_prefix = 10 * (prefix == -1 ? 0 : prefix) + * n - '0'; + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max); +} + +int main (void) { + int n, m; + char * num; + if ((num = (char *) malloc (32 * sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + while (scanf ("%d %d", &n, &m) == 2) { + sprintf (num, "%d", n); + printf ("%d\n", substrings (num, m, -1, n)); + } + return (0); +} diff --git a/challenge-141/abigail/go/ch-1.go b/challenge-141/abigail/go/ch-1.go new file mode 100644 index 0000000000..9da5e9763b --- /dev/null +++ b/challenge-141/abigail/go/ch-1.go @@ -0,0 +1,36 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "math" +) + +func main () { + count := 10 + nr_of_divisors := 8 + + for n := 1; count > 0; n ++ { + s := int (math . Sqrt (float64 (n))) + if n == s * s { + continue + } + c := 0 + for m := 1; m <= s && c <= nr_of_divisors; m ++ { + if n % m == 0 { + c += 2 + } + } + if c == nr_of_divisors { + fmt . Println (n) + count -- + } + } +} diff --git a/challenge-141/abigail/go/ch-2.go b/challenge-141/abigail/go/ch-2.go new file mode 100644 index 0000000000..56ed3073a2 --- /dev/null +++ b/challenge-141/abigail/go/ch-2.go @@ -0,0 +1,49 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" + "strconv" +) + +func substrings (n string, m int, prefix int, max int) int { + var n_prefix int; + if len (n) == 0 { + if prefix > -1 && + prefix < max && + prefix % m == 0 { + return 1 + } else { + return 0 + } + } + + fc, _ := strconv . Atoi (n [0:1]) + if prefix == -1 { + n_prefix = fc + } else { + n_prefix = 10 * prefix + fc + } + tail := n [1:] + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max) +} + +func main () { + for { + var n, m int + c, err := fmt . Scanf ("%d %d", &n, &m) + if c != 2 || err != nil { + break + } + fmt . Println (substrings (strconv . Itoa (n), m, -1, n)) + } +} diff --git a/challenge-141/abigail/java/ch-1.java b/challenge-141/abigail/java/ch-1.java new file mode 100644 index 0000000000..00f394d01e --- /dev/null +++ b/challenge-141/abigail/java/ch-1.java @@ -0,0 +1,32 @@ +// +// 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) { + int count = 10; + int nr_of_divisors = 8; + for (int n = 1; count > 0; n ++) { + int s = (int) Math . sqrt (n); + if (n == s * s) { + continue; + } + int c = 0; + for (int d = 1; d <= s && c <= nr_of_divisors; d ++) { + if (n % d == 0) { + c += 2; + } + } + if (c == nr_of_divisors) { + System . out . println (n); + count --; + } + } + } +} diff --git a/challenge-141/abigail/java/ch-2.java b/challenge-141/abigail/java/ch-2.java new file mode 100644 index 0000000000..bb03482e67 --- /dev/null +++ b/challenge-141/abigail/java/ch-2.java @@ -0,0 +1,37 @@ +// +// 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 int substrings (String n, int m, int prefix, int max) { + if (n . length () == 0) { + return prefix > -1 && + prefix < max && + prefix % m == 0 ? 1 : 0; + } + + int fc = Integer . parseInt (n . substring (0, 1)); + int n_prefix = 10 * (prefix == -1 ? 0 : prefix) + fc; + String tail = n . substring (1, n. length ()); + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max); + } + + + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextInt ()) { + int n = scanner . nextInt (); + int m = scanner . nextInt (); + System . out . println ( + substrings (Integer . toString (n), m, -1, n)); + } + } +} diff --git a/challenge-141/abigail/lua/ch-1.lua b/challenge-141/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..f9b626f6a9 --- /dev/null +++ b/challenge-141/abigail/lua/ch-1.lua @@ -0,0 +1,36 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua +-- + +local count = 10 +local nr_of_divisor = 8 + +local n = 0 +while count > 0 do + n = n + 1 + local s = math . floor (math . sqrt (n)) + if n == s * s then + goto end_while + end + local c = 0 + for d = 1, s do + if n % d == 0 then + c = c + 2 + if c > nr_of_divisor then + goto end_while + end + end + end + if c == nr_of_divisor then + print (n) + count = count - 1 + end + + ::end_while:: +end diff --git a/challenge-141/abigail/lua/ch-2.lua b/challenge-141/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..b7f87027f4 --- /dev/null +++ b/challenge-141/abigail/lua/ch-2.lua @@ -0,0 +1,37 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +function substrings (n, m, prefix, max) + if n : len () == 0 then + if prefix > -1 and prefix < max and prefix % m == 0 then + return 1 + else + return 0 + end + end + + local fc = tonumber (n : sub (1, 1)) + local tail = n : sub (2) + local n_prefix + if prefix == -1 then + n_prefix = fc + else + n_prefix = 10 * prefix + fc + end + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max) +end + + +for line in io . lines () do + local _, _, n, m = line : find ("([0-9]+)%s+([0-9]+)") + print (substrings (n, tonumber (m), -1, tonumber (n))) +end diff --git a/challenge-141/abigail/node/ch-1.js b/challenge-141/abigail/node/ch-1.js new file mode 100644 index 0000000000..e246fffd08 --- /dev/null +++ b/challenge-141/abigail/node/ch-1.js @@ -0,0 +1,30 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js +// + + +let count = 10 +let nr_of_divisors = 8 + +for (let n = 1; count > 0; n ++) { + let s = Math . floor (Math . sqrt (n)) + if (n == s * s) { + continue + } + let c = 0 + for (let d = 1; d <= s && c <= nr_of_divisors; d ++) { + if (n % d == 0) { + c += 2 + } + } + if (c == nr_of_divisors) { + console . log (n) + count -- + } +} diff --git a/challenge-141/abigail/node/ch-2.js b/challenge-141/abigail/node/ch-2.js new file mode 100644 index 0000000000..53b7b7ac44 --- /dev/null +++ b/challenge-141/abigail/node/ch-2.js @@ -0,0 +1,32 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +function substrings (n, m, prefix, max) { + if (n . length == 0) { + return prefix > -1 && + prefix < max && + prefix % m == 0 ? 1 : 0 + } + + let fc = n . substr (0, 1) + let tail = n . substr (1) + let n_prefix = 10 * (prefix == -1 ? 0 : prefix) + + n . substr (0, 1) + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max); +} + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [n, m] = line . split (" ") + console . log (substrings (n, +m, -1, +n)) +}) + diff --git a/challenge-141/abigail/pascal/ch-1.p b/challenge-141/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..7ade307f44 --- /dev/null +++ b/challenge-141/abigail/pascal/ch-1.p @@ -0,0 +1,39 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out *) +(* *) + +var + count: integer = 10; + nr_of_divisors: integer = 8; + n, s, d, c: integer; + + +begin + n := 0; + while count > 0 do begin + inc (n); + s := round (sqrt (n)); + if n = s * s then begin + continue; + end; + c := 0; + for d := 1 to s do begin + if n mod d = 0 then begin + c := c + 2; + if c > nr_of_divisors then begin + continue; + end + end + end; + if c = nr_of_divisors then begin + writeln (n); + dec (count); + end + end +end. diff --git a/challenge-141/abigail/pascal/ch-2.p b/challenge-141/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..e2919ebbf5 --- /dev/null +++ b/challenge-141/abigail/pascal/ch-2.p @@ -0,0 +1,54 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +uses + sysutils; + +function substrings (n: string; m, prefix, max: integer): integer; + var + fc, n_prefix: integer; + tail: string; + + begin + if length (n) = 0 then begin + if (prefix > -1) and + (prefix < max) and + (prefix mod m = 0) then begin + substrings := 1; + end + else begin + substrings := 0; + end; + end + else begin + fc := strtoint (copy (n, 1, 1)); + if prefix = -1 then begin + n_prefix := fc; + end + else begin + n_prefix := 10 * prefix + fc; + end; + + tail := copy (n, 2, length (n)); + + substrings := substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max); + end + end; + +var + n, m: Integer; + +begin + while not eof do begin + readln (n, m); + writeln (substrings (inttostr (n), m, -1, n)); + end; +end. diff --git a/challenge-141/abigail/perl/ch-1.pl b/challenge-141/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..382f1b8557 --- /dev/null +++ b/challenge-141/abigail/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +no warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# +# Math::Prime::Util has a method returning the number of divisors of +# a given number. So, it's just a matter of trying all numbers in order +# and reporting the first 10 with 8 divisors. +# + +use Math::Prime::Util qw [divisors]; + +8 == divisors (++ $::n) && say ($::n) && $::c ++ while $::c < 10; diff --git a/challenge-141/abigail/perl/ch-2.pl b/challenge-141/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..3417e54e1d --- /dev/null +++ b/challenge-141/abigail/perl/ch-2.pl @@ -0,0 +1,76 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +# +# The challenge isn't clear whether it wants a count of all possible +# numbers which match, or the number of unique numbers. For instance, +# an input of +# 1232 2 +# +# would yield 12 twice. Do we count them both? Or are we supposed to +# only count unique once? The examples are of no help. +# +# Furthermore, what about leading 0s? An input of +# +# 102 2 +# +# yield both 02 and 2. (And something like '100 3' will yield +# 0, 0 and 00). +# +# We've decided to be as inclusive as possible. So +# +# 1232 2 +# +# will give 12, 122, 132, 12, 2, 232, 22, 32, and 2, for a total of 9. +# +# and +# 102 2 +# +# will give 10, 12, 0, 02, and 2, for a total of 5. +# + +# +# Recursive function to count all substrings for which $m is +# a proper divisor. +# +sub substrings ($n, $m, $prefix = -1, $max = $n) { + if (!length $n) { + # + # If $n is empty, we have reached the end of recursion. + # If $prefix isn't -1, not equal to the full string, + # and if $m properly divides $prefix, we count $prefix, + # else, we don't. + # + return $prefix > -1 && + $prefix < $max && + $prefix % $m == 0 ? 1 : 0; + } + # + # Recurse, once by picking up the first character of $n, and + # once by skipping the first character. + # + my $fc = substr ($n, 0, 1); + my $next = 10 * ($prefix == -1 ? 0 : $prefix) + $fc; + substrings (substr ($n, 1), $m, $next, $max) + + substrings (substr ($n, 1), $m, $prefix, $max); +} + + + +say substrings split while <>; diff --git a/challenge-141/abigail/python/ch-1.py b/challenge-141/abigail/python/ch-1.py new file mode 100644 index 0000000000..cd4c0b5d3b --- /dev/null +++ b/challenge-141/abigail/python/ch-1.py @@ -0,0 +1,30 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py +# + +import math + +count = 10 +nr_of_divisors = 8 + +n = 0 +while count > 0: + n = n + 1 + s = math . floor (math . sqrt (n)) + if n == s * s: + continue + c = 0 + for d in range (1, s + 1): + if n % d == 0: + c = c + 2 + if c > nr_of_divisors: + break + if c == nr_of_divisors: + print (n) + count = count - 1 diff --git a/challenge-141/abigail/python/ch-2.py b/challenge-141/abigail/python/ch-2.py new file mode 100644 index 0000000000..61cc8cdd25 --- /dev/null +++ b/challenge-141/abigail/python/ch-2.py @@ -0,0 +1,32 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +def substrings (n, m, prefix, max): + if len (n) == 0: + if prefix > -1 and prefix < max and prefix % m == 0: + return 1 + else: + return 0 + + fc = int (n [0 : 1]) + tail = n [1:] + if prefix == -1: + n_prefix = fc + else: + n_prefix = 10 * prefix + fc + + return substrings (tail, m, n_prefix, max) + \ + substrings (tail, m, prefix, max) + +for line in fileinput . input (): + n, m = line . strip () . split (" ") + print (substrings (n, int (m), -1, int (n))) diff --git a/challenge-141/abigail/r/ch-1.r b/challenge-141/abigail/r/ch-1.r new file mode 100644 index 0000000000..a3e7e5f00c --- /dev/null +++ b/challenge-141/abigail/r/ch-1.r @@ -0,0 +1,32 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r +# + +count <- 10 +nr_of_divisor <- 8 + +n <- 0 +while (count > 0) { + n <- n + 1 + s <- floor (sqrt (n)) + if (n == s * s) { + next + } + c <- 0 + for (d in 1 : s) { + if (n %% d == 0) { + c <- c + 2 + if (c > nr_of_divisor) { + break + } + } + } + if (c == nr_of_divisor) { + cat (n, "\n") + count <- count - 1 + } +} diff --git a/challenge-141/abigail/r/ch-2.r b/challenge-141/abigail/r/ch-2.r new file mode 100644 index 0000000000..19bfe0a5aa --- /dev/null +++ b/challenge-141/abigail/r/ch-2.r @@ -0,0 +1,42 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-2.r < input-file +# + +substrings <- function (n, m, prefix, max) { + if (nchar (n) == 0) { + if (prefix > -1 && prefix < max && prefix %% m == 0) { + return (1) + } + else { + return (0) + } + } + + fc <- as.numeric (substr (n, 0, 1)) + tail <- substr (n, 2, nchar (n)) + if (prefix < 0) { + n_prefix <- fc + } else { + n_prefix <- 10 * prefix + fc + } + + return (substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max)) +} + +stdin <- file ('stdin', 'r') +repeat { + line <- readLines (stdin, n = 1) + if (length (line) == 0) { + break + } + parts <- strsplit (line, " ") + list <- parts [[1]] + + cat (substrings (list [[1]], as.numeric (list [[2]]), -1, + as.numeric (list [[1]])), "\n") +} diff --git a/challenge-141/abigail/ruby/ch-1.rb b/challenge-141/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..3aca9a120c --- /dev/null +++ b/challenge-141/abigail/ruby/ch-1.rb @@ -0,0 +1,34 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb +# + +count = 10 +nr_of_divisors = 8 + +n = 0 +while count > 0 do + n = n + 1 + s = Math . sqrt(n) . floor() + if n == s * s then + next + end + c = 0 + for d in 1 .. s do + if n % d == 0 then + c = c + 2 + if c > nr_of_divisors then + break + end + end + end + if c == nr_of_divisors then + puts (n) + count = count - 1 + end +end diff --git a/challenge-141/abigail/ruby/ch-2.rb b/challenge-141/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..86129c8794 --- /dev/null +++ b/challenge-141/abigail/ruby/ch-2.rb @@ -0,0 +1,35 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +def substrings (n, m, prefix, max) + if n . length == 0 then + return prefix > -1 && + prefix < max && + prefix % m == 0 ? 1 : 0 + end + + fc = n[0] . to_i + tail = n[1 .. -1] + if prefix == -1 then + n_prefix = fc + else + n_prefix = 10 * prefix + fc + end + + return substrings(tail, m, n_prefix, max) + + substrings(tail, m, prefix, max) +end + + +ARGF . each_line do + | line | + n, m = line . strip() . split + puts substrings(n, m . to_i, -1, n . to_i) +end diff --git a/challenge-141/abigail/scheme/ch-1.scm b/challenge-141/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..d45dfa4a01 --- /dev/null +++ b/challenge-141/abigail/scheme/ch-1.scm @@ -0,0 +1,31 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + + +(define (list-divisors count nr-of-divisors n) + (define c) + (define s (inexact->exact (round (sqrt n)))) + (if (> count 0) + (begin + (if (not (= n (* s s))) + (begin + (set! c 0) + (do ((d 1 (1+ d))) + ((or (> d s) (> c nr-of-divisors))) + (if (= (modulo n d) 0) + (set! c (+ c 2)))) + (if (= c nr-of-divisors) + (begin + (display n)(newline) + (list-divisors (- count 1) nr-of-divisors (+ n 1))) + (list-divisors count nr-of-divisors (+ n 1)))) + (list-divisors count nr-of-divisors (+ n 1)))))) + + +(list-divisors 10 8 1) + diff --git a/challenge-141/abigail/scheme/ch-2.scm b/challenge-141/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..42d6cde4ec --- /dev/null +++ b/challenge-141/abigail/scheme/ch-2.scm @@ -0,0 +1,40 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm < input-file +;;; + +(use-modules (ice-9 rdelim)) + +(define (fc n) (string->number (string-take n 1))) +(define (tail n) (string-drop n 1)) +(define (n_prefix prefix n) (if (= prefix -1) (fc n) (+ (* prefix 10) (fc n)))) + +(define (substrings n m prefix max) + (if (string-null? n) + (if (and (> prefix -1) + (< prefix max) + (= (modulo prefix m) 0)) 1 0) + (+ (substrings (tail n) m (n_prefix prefix n) max) + (substrings (tail n) m prefix max)))) + + +(define (main) + (define line (read-line)) + (define parts) + (if (not (eof-object? line)) + (begin + (set! parts (string-split line #\ )) + (display (substrings (list-ref parts 0) + (string->number (list-ref parts 1)) + -1 + (string->number (list-ref parts 0)))) + (newline) + (main) + ) + ) +) + +(main) diff --git a/challenge-141/abigail/t/ctest.ini b/challenge-141/abigail/t/ctest.ini new file mode 100644 index 0000000000..860beb8524 --- /dev/null +++ b/challenge-141/abigail/t/ctest.ini @@ -0,0 +1,12 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Fixed Output +2-1 = Given Examples +2-2 = Extra tests + +[1-1] +no_input = 1 diff --git a/challenge-141/abigail/t/input-1-1 b/challenge-141/abigail/t/input-1-1 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-141/abigail/t/input-2-1 b/challenge-141/abigail/t/input-2-1 new file mode 100644 index 0000000000..753cffc98f --- /dev/null +++ b/challenge-141/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +1234 2 +768 4 diff --git a/challenge-141/abigail/t/input-2-2 b/challenge-141/abigail/t/input-2-2 new file mode 100644 index 0000000000..7c7e0becbb --- /dev/null +++ b/challenge-141/abigail/t/input-2-2 @@ -0,0 +1,2 @@ +1232 2 +102 2 diff --git a/challenge-141/abigail/t/output-1-1.exp b/challenge-141/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..cc8a2994e6 --- /dev/null +++ b/challenge-141/abigail/t/output-1-1.exp @@ -0,0 +1,10 @@ +24 +30 +40 +42 +54 +56 +66 +70 +78 +88 diff --git a/challenge-141/abigail/t/output-2-1.exp b/challenge-141/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..93b2fc145a --- /dev/null +++ b/challenge-141/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +9 +3 diff --git a/challenge-141/abigail/t/output-2-2.exp b/challenge-141/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..f224d49c08 --- /dev/null +++ b/challenge-141/abigail/t/output-2-2.exp @@ -0,0 +1,2 @@ +9 +5 diff --git a/challenge-141/abigail/tcl/ch-1.tcl b/challenge-141/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..02f21a4e2f --- /dev/null +++ b/challenge-141/abigail/tcl/ch-1.tcl @@ -0,0 +1,31 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl +# + +set count 10 +set nr_of_divisors 8 + +set n 0 + +while {$count > 0} { + incr n + set s [expr isqrt ($n)] + if {$n == $s * $s} { + continue + } + set c 0 + for {set d 1} {$d <= $s && $c <= $nr_of_divisors} {incr d} { + if {$n % $d == 0} { + incr c 2 + } + } + + if {$c == $nr_of_divisors} { + puts $n + incr count -1 + } +} diff --git a/challenge-141/abigail/tcl/ch-2.tcl b/challenge-141/abigail/tcl/ch-2.tcl new file mode 100644 index 0000000000..c8caf4f7ae --- /dev/null +++ b/challenge-141/abigail/tcl/ch-2.tcl @@ -0,0 +1,35 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-2.tcl < input-file +# + +proc substrings {n m prefix max} { + if {[string length $n] == 0} { + if {$prefix > -1 && $prefix < $max && $prefix % $m == 0} { + return 1 + } else { + return 0 + } + } + + set fc [string range $n 0 0] + set tail [string range $n 1 [string length $n]] + + if {$prefix == -1} { + set n_prefix $fc + } else { + set n_prefix [expr 10 * $prefix + $fc] + } + + return [expr [substrings $tail $m $n_prefix $max] + \ + [substrings $tail $m $prefix $max]] +} + + +while {[gets stdin line] >= 0} { + lassign [split $line " "] n m + puts [substrings $n $m -1 $n] +} -- cgit