diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-12-01 20:33:46 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-01 20:33:46 +0000 |
| commit | e79ffb4570f35bc49bb493b98df3ca7653739f3a (patch) | |
| tree | 9a08f4350abf49525b83199e00b2db0efd0d161d | |
| parent | 4d52f1a3de425bab672261c2e4233b035d1d1e5f (diff) | |
| parent | 44bcd3ae0c35b887bd579e60c17198360f3497c3 (diff) | |
| download | perlweeklychallenge-club-e79ffb4570f35bc49bb493b98df3ca7653739f3a.tar.gz perlweeklychallenge-club-e79ffb4570f35bc49bb493b98df3ca7653739f3a.tar.bz2 perlweeklychallenge-club-e79ffb4570f35bc49bb493b98df3ca7653739f3a.zip | |
Merge pull request #5313 from Abigail/abigail/week-141
Solutions week 141
37 files changed, 1094 insertions, 2 deletions
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 <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <math.h> + +/* + * 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 <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * 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< |
