diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-26 04:41:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-26 04:41:23 +0100 |
| commit | 0e2e32d501b0afda82f261b0f285edcfc07fadb1 (patch) | |
| tree | 9ee5b683ea3e1ca7ecb37c85252377ba742132ba | |
| parent | 31a03d4a577ef0fc7f91f993e586343480dedfe3 (diff) | |
| parent | 848f09825ecee5c5097a1fbc9268a77fedf34a30 (diff) | |
| download | perlweeklychallenge-club-0e2e32d501b0afda82f261b0f285edcfc07fadb1.tar.gz perlweeklychallenge-club-0e2e32d501b0afda82f261b0f285edcfc07fadb1.tar.bz2 perlweeklychallenge-club-0e2e32d501b0afda82f261b0f285edcfc07fadb1.zip | |
Merge pull request #5103 from Abigail/abigail/week-136
Abigail/week 136
27 files changed, 770 insertions, 6 deletions
diff --git a/challenge-136/abigail/README.md b/challenge-136/abigail/README.md index 6178e463fe..c990c62f96 100644 --- a/challenge-136/abigail/README.md +++ b/challenge-136/abigail/README.md @@ -2,30 +2,27 @@ ## Part 1 -* [AWK](awk/ch-1.awk) +* [GNU AWK](awk/ch-1.gawk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) * [Go](go/ch-1.go) -* [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) * [Ruby](ruby/ch-1.rb) -* [Scheme](scheme/ch-1.scm) * [Tcl](tcl/ch-1.tcl) +* [Scheme](scheme/ch-1.scm) ## Part 2 * [AWK](awk/ch-2.awk) -* [Bash](bash/ch-2.sh) * [C](c/ch-2.c) * [Go](go/ch-2.go) -* [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) * [Ruby](ruby/ch-2.rb) -* [Scheme](scheme/ch-2.scm) * [Tcl](tcl/ch-2.tcl) +* [Scheme](scheme/ch-2.scm) diff --git a/challenge-136/abigail/awk/ch-1.gawk b/challenge-136/abigail/awk/ch-1.gawk new file mode 100644 index 0000000000..c40210446f --- /dev/null +++ b/challenge-136/abigail/awk/ch-1.gawk @@ -0,0 +1,41 @@ +#!/opt/local/bin/gawk + +# +# See ../README.md +# + +# +# Run as: gawk -f ch-1.gawk < input-file +# + + +# +# Find the GCD, using Stein's algorithm +# (https://en.wikipedia.org/wiki/Binary_GCD_algorithm) +# +function gcd (u, v, u_odd, v_odd) { + u_odd = u % 2 + v_odd = v % 2 + + return u == v || !v ? u \ + : !u ? v \ + : !u_odd && !v_odd ? lshift (gcd(rshift (u, 1), rshift (v, 1)), 1) \ + : !u_odd && !v_odd ? gcd(rshift (u, 1), v) \ + : !u_odd && !v_odd ? gcd(u, rshift (v, 1)) \ + : u > v ? gcd(u - v, v) \ + : gcd(v - u, u) +} + +# +# Pre calculate powers of 2. We can do powers up to and including 2^52 +# +BEGIN { + for (i = 1; i <= 52; i ++) { + power_of_2 [lshift (1, i)] = 1 + } +} + + +{ + print power_of_2 [gcd($1, $2)] || 0 +} diff --git a/challenge-136/abigail/awk/ch-2.awk b/challenge-136/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..2df5623041 --- /dev/null +++ b/challenge-136/abigail/awk/ch-2.awk @@ -0,0 +1,22 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +function count (target, this_fib, prev_fib) { + if (!this_fib) {this_fib = 1} + if (!prev_fib) {prev_fib = 1} + return target < this_fib ? 0 \ + : target == this_fib ? 1 \ + : count(target - this_fib, this_fib + prev_fib, this_fib) + \ + count(target, this_fib + prev_fib, this_fib) +} + +{ + print count($1) +} diff --git a/challenge-136/abigail/bash/ch-1.sh b/challenge-136/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..5e7b17da18 --- /dev/null +++ b/challenge-136/abigail/bash/ch-1.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +# +# Find the GCD, using Euclids algorithm +# (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations) +# +function gcd () { + local a=$1 + local b=$2 + local t=0 + while ((b != 0)) + do ((t = b)) + ((b = a % b)) + ((a = t)) + done + ((gcd = a)) +} + +set -f + +# +# Calculate the powers of 2 (greater than 1). 2^62 is the max a bash +# integer holds +# +declare -a power_of_2 +for ((i = 1; i <= 62; i ++)) +do power_of_2[$((1 << i))]=1 +done + +while read n m +do gcd $n $m + echo ${power_of_2[$gcd]:-0} +done diff --git a/challenge-136/abigail/c/ch-1.c b/challenge-136/abigail/c/ch-1.c new file mode 100644 index 0000000000..57acd46d89 --- /dev/null +++ b/challenge-136/abigail/c/ch-1.c @@ -0,0 +1,53 @@ +# 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 + */ + + +/* + * Find the GCD, using Stein's algorithm + * (https://en.wikipedia.org/wiki/Binary_GCD_algorithm) + */ +long long gcd (long long u, long long v) { + long long u_odd = u % 2; + long long v_odd = v % 2; + + return u == v || !v ? u + : !u ? v + : !u_odd && !v_odd ? gcd (u >> 1, v >> 1) << 1 + : !u_odd && v_odd ? gcd (u >> 1, v) + : u_odd && !v_odd ? gcd (u, v >> 1) + : u > v ? gcd (u - v, v) + : gcd (v - u, u); +} + + +int main (void) { + long long n, m; + + while (scanf ("%lld %lld", &n, &m) == 2) { + long long ggcd = gcd (n, m); + short valid = 0; + /* + * Check whether ggcd is a power of 2 + */ + if (ggcd > 1) { + while (ggcd % 2 == 0) { + ggcd /= 2; + } + if (ggcd == 1) { + valid = 1; + } + } + printf ("%d\n", valid); + } + + return (0); +} diff --git a/challenge-136/abigail/c/ch-2.c b/challenge-136/abigail/c/ch-2.c new file mode 100644 index 0000000000..e3c2cb8fd8 --- /dev/null +++ b/challenge-136/abigail/c/ch-2.c @@ -0,0 +1,31 @@ +# 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 _count (long long target, long long this_fib, long long prev_fib) { + return target < this_fib ? 0 + : target == this_fib ? 1 + : _count (target - this_fib, this_fib + prev_fib, this_fib) + + _count (target, this_fib + prev_fib, this_fib); +} + +int count (long long target) { + return _count (target, 1, 1); +} + +int main (void) { + long long n; + while (scanf ("%lld", &n) == 1) { + printf ("%d\n", count (n)); + } + + return (0); +} diff --git a/challenge-136/abigail/go/ch-1.go b/challenge-136/abigail/go/ch-1.go new file mode 100644 index 0000000000..afbba43698 --- /dev/null +++ b/challenge-136/abigail/go/ch-1.go @@ -0,0 +1,52 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" +// "bufio" +// "os" +) + +// +// Find the GCD, using Stein's algorithm +// (https://en.wikipedia.org/wiki/Binary_GCD_algorithm) +// +func gcd (u int, v int) int { + var u_odd bool = u % 2 != 0 + var v_odd bool = v % 2 != 0 + + if (u == v || v == 0) {return u} + if ( u == 0) {return v} + if (!u_odd && !v_odd) {return gcd (u >> 1, v >> 1) << 1} + if (!u_odd && v_odd) {return gcd (u >> 1, v)} + if ( u_odd && !v_odd) {return gcd (u, v >> 1)} + if ( u > v) {return gcd (u - v, v)} + return gcd (v - u, u) +} + +func main () { + var power_of_2 map [int] int = make (map [int] int) + for i := 1; i < 62; i ++ { + power_of_2 [i << 1] = 1 + } + + for { + var n, m int + c, err := fmt . Scanf ("%d %d", &n, &m); + if c != 2 || err != nil { + break; + } + if _, ok := power_of_2 [gcd (m, n)]; ok { + fmt . Print ("1\n") + } else { + fmt . Print ("0\n") + } + } +} diff --git a/challenge-136/abigail/go/ch-2.go b/challenge-136/abigail/go/ch-2.go new file mode 100644 index 0000000000..adff1db0df --- /dev/null +++ b/challenge-136/abigail/go/ch-2.go @@ -0,0 +1,36 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go +// + +import ( + "fmt" +) + +func _count (target int, this_fib int, prev_fib int) int { + if target < this_fib {return 0} + if target == this_fib {return 1} + return _count (target - this_fib, this_fib + prev_fib, this_fib) + + _count (target, this_fib + prev_fib, this_fib) +} + +func count (target int) int { + return _count (target, 1, 1) +} + +func main () { + for { + var n int + c, err := fmt . Scanf ("%d", &n) + if c != 1 || err != nil { + break; + } + + fmt . Printf ("%d\n", count (n)) + } +} diff --git a/challenge-136/abigail/lua/ch-1.lua b/challenge-136/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..182d18fba7 --- /dev/null +++ b/challenge-136/abigail/lua/ch-1.lua @@ -0,0 +1,42 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +-- +-- Find the GCD, using Euclids algorithm +-- (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations) +-- +function gcd (a, b) + while b > 0 do + a, b = b, a % b + end + return a +end + +-- +-- Precalculate all the relevant powers of 2. Note that in pre 5-3 lua +-- integers are doubles, and start losing precision at 2^53, so we go +-- up to 2^52. +-- +local power_of_2 = {} +local power = 1 +for i = 1, 52 do + power = power * 2 + power_of_2 [power] = 1 +end + + +for line in io . lines () do + local _, _, n, m = line : find ("([0-9]+)%s+([0-9]+)") + if power_of_2 [gcd (tonumber (n), tonumber (m))] then + print (1) + else + print (0) + end +end diff --git a/challenge-136/abigail/lua/ch-2.lua b/challenge-136/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..06d92984f4 --- /dev/null +++ b/challenge-136/abigail/lua/ch-2.lua @@ -0,0 +1,24 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +function _count (target, this_fib, prev_fib) + if target < this_fib then return 0 end + if target == this_fib then return 1 end + return (_count (target - this_fib, this_fib + prev_fib, this_fib) + + _count (target, this_fib + prev_fib, this_fib)) +end + +function count (target) + return (_count (target, 1, 1)) +end + +for line in io . lines () do + print (count (tonumber (line))) +end diff --git a/challenge-136/abigail/node/ch-1.js b/challenge-136/abigail/node/ch-1.js new file mode 100644 index 0000000000..08331ea39f --- /dev/null +++ b/challenge-136/abigail/node/ch-1.js @@ -0,0 +1,37 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +// +// Find the GCD, using Euclids algorithm +// (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations) +// +function gcd (a, b) { + while (b > 0) { + [a, b] = [b, a % b] + } + return (a) +} + +// +// Precalculate the powers of 2. We're losing precision around 2^53 +// +let power_of_2 = {}; +let power = 1; +for (let i = 1; i < 53; i ++) { + power *= 2; + power_of_2 [power] = 1; +} + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [m, n] = line . trim () . split (' ') . map (x => +x) + console . log (power_of_2 [gcd (m, n)] || 0) +}) diff --git a/challenge-136/abigail/node/ch-2.js b/challenge-136/abigail/node/ch-2.js new file mode 100644 index 0000000000..eaf2a426a0 --- /dev/null +++ b/challenge-136/abigail/node/ch-2.js @@ -0,0 +1,23 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +function count (target, this_fib, prev_fib) { + if (!this_fib) {this_fib = 1} + if (!prev_fib) {prev_fib = 1} + return target < this_fib ? 0 + : target == this_fib ? 1 + : count (target - this_fib, this_fib + prev_fib, this_fib) + + count (target, this_fib + prev_fib, this_fib) +} + + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => console . log (count (+line))) diff --git a/challenge-136/abigail/perl/ch-1.pl b/challenge-136/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..445d00a1fd --- /dev/null +++ b/challenge-136/abigail/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/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-1.pl < input-file +# + +# +# We need the GCD of two numbers. We did this in the past a few times, +# for instance in weeks 82, 89 and 93. +# So, we copy-and-pasted some code. +# +# We could write some code to check whether a number is a power of 2. +# But there are only 63 powers of 2 which fit in a 64 bit integer, one +# of them being 1. So, we can just precalculate the ones we're interested in. +# +my %power_of_2 = map {1 << $_ => 1} 1 .. 62; + +# +# Find the GCD, using Stein's algorithm +# (https://en.wikipedia.org/wiki/Binary_GCD_algorithm) +# +sub gcd; +sub gcd ($u, $v) { + my ($u_odd, $v_odd) = ($u % 2, $v % 2); + $u == $v || !$v ? $u + : !$u ? $v + : !$u_odd && !$v_odd ? gcd ($u >> 1, $v >> 1) << 1 + : !$u_odd && $v_odd ? gcd ($u >> 1, $v) + : $u_odd && !$v_odd ? gcd ($u, $v >> 1) + : $u > $v ? gcd ($u - $v, $v) + : gcd ($v - $u, $u) +} + +# +# Main program +# +say $power_of_2 {gcd split} || 0 while <>; diff --git a/challenge-136/abigail/perl/ch-2.pl b/challenge-136/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..d9a5e5ae13 --- /dev/null +++ b/challenge-136/abigail/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/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 +# + +# +# We will use a simple recursive function. +# The function takes three arguments: +# - The target number we like to sum to ($target) +# - The smallest Fibonnaci number we have not tried yet ($this_fib) +# - The Fibonacci number proceeding the one above. ($prev_fib) +# +# If $this_fib is larger than $target, we have to way to make the target +# number, so we return 0. +# If $this_fib is equal to $target, we can only make the target in one way, +# so we return 1. +# Else, we recurse. First, we count the number of ways to make +# $target - $this_fib with Fibonnaci numbers larger than $this_fib, then +# we count the number of ways making $target with Fibonnaci numbers larger +# than $this_fib. We return the sum of these counts. +# + +sub count; +sub count ($target, $this_fib = 1, $prev_fib = 1) { + $this_fib > $target ? 0 + : $this_fib == $target ? 1 + : count ($target - $this_fib, $this_fib + $prev_fib, $this_fib) + + count ($target, $this_fib + $prev_fib, $this_fib) +} + + +say count $_ while <>; diff --git a/challenge-136/abigail/python/ch-1.py b/challenge-136/abigail/python/ch-1.py new file mode 100644 index 0000000000..86da426994 --- /dev/null +++ b/challenge-136/abigail/python/ch-1.py @@ -0,0 +1,23 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput +import math + +for line in fileinput . input (): + m, n = line . strip () . split (' ') + gcd = math . gcd (int (m), int (n)) + valid = 0 + if gcd > 1: + while gcd % 2 == 0: + gcd = gcd / 2 + if gcd == 1: + valid = 1 + print (valid) diff --git a/challenge-136/abigail/python/ch-2.py b/challenge-136/abigail/python/ch-2.py new file mode 100644 index 0000000000..88960021ef --- /dev/null +++ b/challenge-136/abigail/python/ch-2.py @@ -0,0 +1,25 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +def _count (target, this_fib, prev_fib): + if target < this_fib: + return (0) + if target == this_fib: + return (1) + return (_count (target - this_fib, this_fib + prev_fib, this_fib) + + _count (target, this_fib + prev_fib, this_fib)) + +def count (target): + return (_count (target, 1, 1)) + +import fileinput + +for line in fileinput . input (): + print (count (int (line))) diff --git a/challenge-136/abigail/ruby/ch-1.rb b/challenge-136/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..72ef8542f1 --- /dev/null +++ b/challenge-136/abigail/ruby/ch-1.rb @@ -0,0 +1,25 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + | line | + m, n = line . split . map {|x| x . to_i} + gcd = m . gcd (n) + valid = 0 + if gcd > 1 then + while gcd % 2 == 0 do + gcd /= 2 + end + if gcd == 1 then + valid = 1 + end + end + puts (valid) +end diff --git a/challenge-136/abigail/ruby/ch-2.rb b/challenge-136/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..24e129a00d --- /dev/null +++ b/challenge-136/abigail/ruby/ch-2.rb @@ -0,0 +1,25 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +def _count (target, this_fib, prev_fib) + return target < this_fib ? 0 + : target == this_fib ? 1 + : _count(target - this_fib, this_fib + prev_fib, this_fib) + + _count(target, this_fib + prev_fib, this_fib) +end + +def count (target) + return _count(target, 1, 1) +end + +ARGF . each_line do + | line | + puts (count(line . to_i)) +end diff --git a/challenge-136/abigail/scheme/ch-1.scm b/challenge-136/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..3e9728abbf --- /dev/null +++ b/challenge-136/abigail/scheme/ch-1.scm @@ -0,0 +1,45 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm < input-file +;;; + +;;; +;;; Find the GCD, using Stein's algorithm +;;; (https://en.wikipedia.org/wiki/Binary_GCD_algorithm) +;;; +(define (gcd u v) + (define u_odd (= (modulo u 2) 1)) + (define v_odd (= (modulo v 2) 1)) + (cond ((= u v) u) + ((= u 0) v) + ((= v 0) u) + ((and (not u_odd) (not v_odd)) (ash (gcd (ash u -1) (ash v -1)) 1)) + ((and (not u_odd) v_odd) (gcd (ash u -1) v)) + ((and u_odd (not v_odd)) (gcd u (ash v -1))) + ((> u v) (gcd (- u v) v)) + (else (gcd (- v u) u))) +) + +;;; +;;; Return #t if n is a power of 2, other than 1 +;;; +(define (is-power-of-2 n) + (cond ((= (modulo n 2) 1) #f) + ((= n 2) #t) + (else (is-power-of-2 (/ n 2))))) + +(define (main) + (define m (read)) + (define n (read)) + (if (not (eof-object? m)) + (begin + (display (if (is-power-of-2 (gcd m n)) 1 0))(newline) + (main) + ) + ) +) + +(main) diff --git a/challenge-136/abigail/scheme/ch-2.scm b/challenge-136/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..81709e323d --- /dev/null +++ b/challenge-136/abigail/scheme/ch-2.scm @@ -0,0 +1,31 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm < input-file +;;; + +(define (_count target this_fib prev_fib) + (cond ((< target this_fib) 0) + ((= target this_fib) 1) + (else (+ (_count (- target this_fib) (+ this_fib prev_fib) this_fib) + (_count target (+ this_fib prev_fib) this_fib))) + ) +) + +(define (count target) + (_count target 1 1) +) + +(define (main) + (define n (read)) + (if (not (eof-object? n)) + (begin + (display (count n))(newline) + (main) + ) + ) +) + +(main) diff --git a/challenge-136/abigail/t/ctest.ini b/challenge-136/abigail/t/ctest.ini new file mode 100644 index 0000000000..527781acbb --- /dev/null +++ b/challenge-136/abigail/t/ctest.ini @@ -0,0 +1,8 @@ +#
+# 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
diff --git a/challenge-136/abigail/t/input-1-1 b/challenge-136/abigail/t/input-1-1 new file mode 100644 index 0000000000..15032b5275 --- /dev/null +++ b/challenge-136/abigail/t/input-1-1 @@ -0,0 +1,3 @@ +8 24 +26 39 +4 10 diff --git a/challenge-136/abigail/t/input-2-1 b/challenge-136/abigail/t/input-2-1 new file mode 100644 index 0000000000..5a670722cb --- /dev/null +++ b/challenge-136/abigail/t/input-2-1 @@ -0,0 +1,3 @@ +16 +9 +15 diff --git a/challenge-136/abigail/t/output-1-1.exp b/challenge-136/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..16db301bb5 --- /dev/null +++ b/challenge-136/abigail/t/output-1-1.exp @@ -0,0 +1,3 @@ +1 +0 +1 diff --git a/challenge-136/abigail/t/output-2-1.exp b/challenge-136/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..1a77ef09c7 --- /dev/null +++ b/challenge-136/abigail/t/output-2-1.exp @@ -0,0 +1,3 @@ +4 +2 +2 diff --git a/challenge-136/abigail/tcl/ch-1.tcl b/challenge-136/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..53a25f8956 --- /dev/null +++ b/challenge-136/abigail/tcl/ch-1.tcl @@ -0,0 +1,47 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +# +# Find the GCD, using Stein's algorithm +# (https://en.wikipedia.org/wiki/Binary_GCD_algorithm) +# +proc gcd {u v} { + set u_odd [expr $u % 2] + set v_odd [expr $v % 2] + if {$u == $v || $v == 0} {return $u} + if { $u == 0} {return $v} + if {$u_odd == 0 && $v_odd == 0} { + return [expr [gcd [expr $u >> 1] [expr $v >> 1]] << 1] + } + if {$u_odd == 0 && $v_odd > 0} { + return [gcd [expr $u >> 1] $v] + } + if {$u_odd > 0 && $v_odd == 0} { + return [gcd $u [expr $v >> 1]] + } + if {$u > $v} { + return [gcd [expr $u - $v] $v] + } + return [gcd [expr $v - $u] $u] +} + + +while {[gets stdin line] >= 0} { + lassign [split $line " "] m n + set ggcd [gcd $m $n] + set valid 0 + if {$ggcd > 1} { + while {$ggcd % 2 == 0} { + set ggcd [expr $ggcd / 2] + } + if {$ggcd == 1} { + set valid 1 + } + } + puts $valid +} diff --git a/challenge-136/abigail/tcl/ch-2.tcl b/challenge-136/abigail/tcl/ch-2.tcl new file mode 100644 index 0000000000..6062392bce --- /dev/null +++ b/challenge-136/abigail/tcl/ch-2.tcl @@ -0,0 +1,28 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-2.tcl < input-file +# + +proc _count {target this_fib prev_fib} { + if {$target < $this_fib} { + return 0 + } + if {$target == $this_fib} { + return 1 + } + return [expr [_count [expr $target - $this_fib] \ + [expr $this_fib + $prev_fib] $this_fib] + \ + [_count $target \ + [expr $this_fib + $prev_fib] $this_fib]] +} + +proc count {target} { + return [_count $target 1 1] +} + +while {[gets stdin line] >= 0} { + puts [count $line] +} |
