aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-136/abigail/README.md7
-rw-r--r--challenge-136/abigail/awk/ch-1.gawk19
-rw-r--r--challenge-136/abigail/bash/ch-1.sh45
-rw-r--r--challenge-136/abigail/bash/ch-2.sh32
-rw-r--r--challenge-136/abigail/bc/ch-1.bc35
-rw-r--r--challenge-136/abigail/bc/ch-2.bc21
-rw-r--r--challenge-136/abigail/c/ch-1.c28
-rw-r--r--challenge-136/abigail/go/ch-1.go19
-rw-r--r--challenge-136/abigail/java/ch-1.java55
-rw-r--r--challenge-136/abigail/java/ch-2.java29
-rw-r--r--challenge-136/abigail/lua/ch-1.lua28
-rw-r--r--challenge-136/abigail/node/ch-1.js32
-rw-r--r--challenge-136/abigail/perl/ch-1.pl26
-rw-r--r--challenge-136/abigail/python/ch-1.py25
-rw-r--r--challenge-136/abigail/r/ch-1.r53
-rw-r--r--challenge-136/abigail/r/ch-2.r29
-rw-r--r--challenge-136/abigail/ruby/ch-1.rb24
-rw-r--r--challenge-136/abigail/scheme/ch-1.scm24
-rw-r--r--challenge-136/abigail/t/ctest.ini3
-rw-r--r--challenge-136/abigail/tcl/ch-1.tcl29
20 files changed, 447 insertions, 116 deletions
diff --git a/challenge-136/abigail/README.md b/challenge-136/abigail/README.md
index c990c62f96..df95d28447 100644
--- a/challenge-136/abigail/README.md
+++ b/challenge-136/abigail/README.md
@@ -4,12 +4,15 @@
* [GNU AWK](awk/ch-1.gawk)
* [Bash](bash/ch-1.sh)
+* [Bc](bc/ch-1.bc)
* [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)
+* [R](r/ch-1.r)
* [Ruby](ruby/ch-1.rb)
* [Tcl](tcl/ch-1.tcl)
* [Scheme](scheme/ch-1.scm)
@@ -17,12 +20,16 @@
## Part 2
* [AWK](awk/ch-2.awk)
+* [Bash](bash/ch-1.sh)
+* [Bc](bc/ch-2.bc)
* [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)
+* [R](r/ch-2.r)
* [Ruby](ruby/ch-2.rb)
* [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
index c40210446f..12f563a4ea 100644
--- a/challenge-136/abigail/awk/ch-1.gawk
+++ b/challenge-136/abigail/awk/ch-1.gawk
@@ -26,16 +26,19 @@ function gcd (u, v, u_odd, v_odd) {
: 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
- }
+function is_power_of_n (number, n) {
+ return number < 1 ? 0 \
+ : number == 1 ? 1 \
+ : number % n ? 0 \
+ : is_power_of_n(number / n, n)
}
+function is_power_of_2 (number) {
+ return is_power_of_n(number, 2)
+}
+
{
- print power_of_2 [gcd($1, $2)] || 0
+ r = gcd($1, $2)
+ print (r > 1 && is_power_of_2(r) ? 1 : 0)
}
diff --git a/challenge-136/abigail/bash/ch-1.sh b/challenge-136/abigail/bash/ch-1.sh
index 5e7b17da18..857e3b7f7e 100644
--- a/challenge-136/abigail/bash/ch-1.sh
+++ b/challenge-136/abigail/bash/ch-1.sh
@@ -15,27 +15,40 @@
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))
+ if ((b > a))
+ then gcd $b $a
+ elif ((b == 0))
+ then gcd=$a
+ else gcd $b $((a % b))
+ fi
+}
+
+function is_power_of_n () {
+ local number=$1
+ local n=$2
+ if ((number < 1))
+ then is_power_of_n=0
+ elif ((number == 1))
+ then is_power_of_n=1
+ elif ((number % n > 0))
+ then is_power_of_n=0
+ else is_power_of_n $((number / 2)) $n
+ fi
+}
+
+function is_power_of_2 () {
+ is_power_of_n $1 2
+ is_power_of_2=$is_power_of_n
}
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}
+ is_power_of_2 $gcd
+ if ((gcd > 1 && is_power_of_2 == 1))
+ then echo 1
+ else echo 0
+ fi
done
diff --git a/challenge-136/abigail/bash/ch-2.sh b/challenge-136/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..1a291d7d18
--- /dev/null
+++ b/challenge-136/abigail/bash/ch-2.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh < input-file
+#
+
+set -f
+
+function count () {
+ local target=$1
+ local this_fib=${2:-1}
+ local prev_fib=${3:-1}
+
+ if ((target < this_fib))
+ then count=0
+ elif ((target == this_fib))
+ then count=1
+ else count $((target - this_fib)) $((this_fib + prev_fib)) $this_fib
+ local sum=$count
+ count $target $((this_fib + prev_fib)) $this_fib
+ count=$((count + sum))
+ fi
+}
+
+while read target
+do count $target
+ echo $count
+done
diff --git a/challenge-136/abigail/bc/ch-1.bc b/challenge-136/abigail/bc/ch-1.bc
new file mode 100644
index 0000000000..9a0c347386
--- /dev/null
+++ b/challenge-136/abigail/bc/ch-1.bc
@@ -0,0 +1,35 @@
+#
+# See ../README.md
+#
+
+#
+# Run as: bc ch-1.bc < input-file
+#
+# Terminate input with a 0
+#
+
+define g (a, b) {
+ if (b > a) {return g (b, a)}
+ if (b > 0) {return g (b, a % b)}
+ return a
+}
+
+define p (n, m) {
+ if (n < 1) {return 0}
+ if (n == 1) {return 1}
+ if (n % m > 0) {return 0}
+ return p (n / m, m)
+}
+
+define q (n) {
+ return p (n, 2)
+}
+
+while (1) {
+ n = read (); if (n == 0) {break}
+ m = read (); if (m == 0) {break}
+ r = g (n, m)
+ r > 1 && q (r)
+}
+
+quit
diff --git a/challenge-136/abigail/bc/ch-2.bc b/challenge-136/abigail/bc/ch-2.bc
new file mode 100644
index 0000000000..060a779f3c
--- /dev/null
+++ b/challenge-136/abigail/bc/ch-2.bc
@@ -0,0 +1,21 @@
+#
+# See ../README.md
+#
+
+#
+# Run as: bc ch-2.bc < input-file
+#
+
+define d (t, f, p) {
+ if (t < f) {return 0}
+ if (t == f) {return 1}
+ return d (t - f, f + p, f) + d (t, f + p, f)
+}
+define c (t) {
+ return d (t, 1, 1)
+}
+
+while (1) {
+ n = read (); if (n == 0) {break}
+ c (n)
+}
diff --git a/challenge-136/abigail/c/ch-1.c b/challenge-136/abigail/c/ch-1.c
index 57acd46d89..d1c218aee5 100644
--- a/challenge-136/abigail/c/ch-1.c
+++ b/challenge-136/abigail/c/ch-1.c
@@ -1,6 +1,7 @@
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
+# include <stdbool.h>
/*
* See ../README.md
@@ -28,25 +29,24 @@ long long gcd (long long u, long long v) {
: gcd (v - u, u);
}
+bool is_power_of_n (long long number, long long n) {
+ return number < 1 ? false
+ : number == 1 ? true
+ : number % n ? false
+ : is_power_of_n (number / n, n);
+}
+bool is_power_of_2 (long long number) {
+ return is_power_of_n (number, 2);
+}
+
+
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);
+ long long r = gcd (n, m);
+ printf ("%d\n", r > 1 && is_power_of_2 (r) ? 1 : 0);
}
return (0);
diff --git a/challenge-136/abigail/go/ch-1.go b/challenge-136/abigail/go/ch-1.go
index afbba43698..6c9e59ab1b 100644
--- a/challenge-136/abigail/go/ch-1.go
+++ b/challenge-136/abigail/go/ch-1.go
@@ -10,8 +10,6 @@ package main
import (
"fmt"
-// "bufio"
-// "os"
)
//
@@ -31,19 +29,24 @@ func gcd (u int, v int) int {
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
- }
+func is_power_of_n (number int, n int) bool {
+ if (number < 1) {return false}
+ if (number == 1) {return true}
+ if (number % n != 0) {return false}
+ return is_power_of_n (number / n, n)
+}
+func is_power_of_2 (number int) bool {
+ return is_power_of_n (number, 2)
+}
+func main () {
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 {
+ if r := gcd (m, n); r > 1 && is_power_of_2 (r) {
fmt . Print ("1\n")
} else {
fmt . Print ("0\n")
diff --git a/challenge-136/abigail/java/ch-1.java b/challenge-136/abigail/java/ch-1.java
new file mode 100644
index 0000000000..4fbc1db180
--- /dev/null
+++ b/challenge-136/abigail/java/ch-1.java
@@ -0,0 +1,55 @@
+//
+// See ../README.md
+//
+
+//
+// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file
+//
+
+import java.util.*;
+
+public class ch1 {
+ //
+ // Find the GCD, using Stein's algorithm
+ // (https://en.wikipedia.org/wiki/Binary_GCD_algorithm)
+ //
+ public static int gcd (int u, int v) {
+ boolean u_odd = u % 2 != 0;
+ boolean v_odd = v % 2 != 0;
+
+ return u == v || v == 0 ? u
+ : u == 0 ? 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);
+ }
+
+ //
+ // Return true if number is a power of n, that is, number == n ^ p
+ // for some non-negative integer p. Return false otherwise.
+ //
+ public static boolean is_power_of_n (int number, int n) {
+ return number < 1 ? false
+ : number == 1 ? true
+ : number % n != 0 ? false
+ : is_power_of_n (number / n, n);
+ }
+
+ public static boolean is_power_of_2 (int number) {
+ return is_power_of_n (number, 2);
+ }
+
+ public static void main (String [] args) {
+ Scanner scanner = new Scanner (System . in);
+ while (scanner . hasNextInt ()) {
+ int n = scanner . nextInt ();
+ int m = scanner . nextInt ();
+
+ int r = gcd (n, m);
+
+ System . out . println (r > 1 && is_power_of_2 (r) ? 1 : 0);
+ }
+ }
+}
diff --git a/challenge-136/abigail/java/ch-2.java b/challenge-136/abigail/java/ch-2.java
new file mode 100644
index 0000000000..f08384cf95
--- /dev/null
+++ b/challenge-136/abigail/java/ch-2.java
@@ -0,0 +1,29 @@
+//
+// 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 count (int target, int this_fib, int 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);
+ }
+
+ public static int count (int target) {
+ return count (target, 1, 1);
+ }
+
+ public static void main (String [] args) {
+ Scanner scanner = new Scanner (System . in);
+ while (scanner . hasNextInt ()) {
+ System . out . println (count (scanner . nextInt ()));
+ }
+ }
+}
diff --git a/challenge-136/abigail/lua/ch-1.lua b/challenge-136/abigail/lua/ch-1.lua
index 182d18fba7..44ea00f251 100644
--- a/challenge-136/abigail/lua/ch-1.lua
+++ b/challenge-136/abigail/lua/ch-1.lua
@@ -13,28 +13,26 @@
-- (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations)
--
function gcd (a, b)
- while b > 0 do
- a, b = b, a % b
- end
- return a
+ if b > a then return gcd (b, a) end
+ if b == 0 then return a end
+ return gcd (b, a % b)
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
+function is_power_of_n (number, n)
+ if number < 1 then return false end
+ if number == 1 then return true end
+ if number % n > 1 then return false end
+ return (is_power_of_n (number / n, n))
end
+function is_power_of_2 (number)
+ return is_power_of_n (number, 2)
+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
+ local r = gcd (tonumber (n), tonumber (m))
+ if r > 1 and is_power_of_2 (r) then
print (1)
else
print (0)
diff --git a/challenge-136/abigail/node/ch-1.js b/challenge-136/abigail/node/ch-1.js
index 08331ea39f..af49fa3977 100644
--- a/challenge-136/abigail/node/ch-1.js
+++ b/challenge-136/abigail/node/ch-1.js
@@ -13,25 +13,31 @@
// (https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations)
//
function gcd (a, b) {
- while (b > 0) {
- [a, b] = [b, a % b]
- }
- return (a)
+ if (b > a) {return gcd (b, a)}
+ if (b == 0) {return a}
+ return gcd (b, a % b)
}
-//
-// 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;
+function is_power_of_n (number, n) {
+ if (number < 1) {return false}
+ if (number == 1) {return true}
+ if (number % n) {return false}
+ return is_power_of_n (number / n, n)
+}
+
+function is_power_of_2 (number) {
+ return is_power_of_n (number, 2)
}
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)
+ let r = gcd (m, n)
+ if (r > 1 && is_power_of_2 (r)) {
+ console . log (1)
+ }
+ else {
+ console . log (0)
+ }
})
diff --git a/challenge-136/abigail/perl/ch-1.pl b/challenge-136/abigail/perl/ch-1.pl
index 445d00a1fd..ac0b598577 100644
--- a/challenge-136/abigail/perl/ch-1.pl
+++ b/challenge-136/abigail/perl/ch-1.pl
@@ -22,11 +22,6 @@ use experimental 'lexical_subs';
# 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
@@ -45,6 +40,25 @@ sub gcd ($u, $v) {
}
#
+# Return true of $number is a power of $n, that is
+# $number == $n ^ $p for some non-negative integer $p
+#
+sub is_power_of_n ($number, $n) {
+ use integer;
+ $number < 1 ? 0
+ : $number == 1 ? 1
+ : $number % $n ? 0
+ : is_power_of_n ($number / $n, $n)
+}
+
+sub is_power_of_2 ($number) {
+ is_power_of_n ($number, 2)
+}
+
+#
# Main program
#
-say $power_of_2 {gcd split} || 0 while <>;
+while (<>) {
+ my $r = gcd split;
+ say $r > 1 && is_power_of_2 ($r) ? 1 : 0
+}
diff --git a/challenge-136/abigail/python/ch-1.py b/challenge-136/abigail/python/ch-1.py
index 86da426994..0c27429811 100644
--- a/challenge-136/abigail/python/ch-1.py
+++ b/challenge-136/abigail/python/ch-1.py
@@ -11,13 +11,22 @@
import fileinput
import math
+def is_power_of_n (number, n):
+ if number < 1:
+ return False
+ if number == 1:
+ return True
+ if number % n:
+ return False
+ return is_power_of_n (number / n, n)
+
+def is_power_of_2 (number):
+ return is_power_of_n (number, 2)
+
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)
+ r = math . gcd (int (m), int (n))
+ if r > 1 and is_power_of_2 (r):
+ print (1)
+ else:
+ print (0)
diff --git a/challenge-136/abigail/r/ch-1.r b/challenge-136/abigail/r/ch-1.r
new file mode 100644
index 0000000000..1231a1c2a8
--- /dev/null
+++ b/challenge-136/abigail/r/ch-1.r
@@ -0,0 +1,53 @@
+#
+# See ../README.md
+#
+
+#
+# Run as: Rscript ch-1.r < input-file
+#
+
+gcd <- function (u, v) {
+ u_odd <- u %% 2 != 0
+ v_odd <- v %% 2 != 0
+
+ if (u == v | v == 0) {u}
+ else if ( u == 0) {v}
+ else if (!u_odd & !v_odd) {
+ bitwShiftL (gcd (bitwShiftR (u, 1), bitwShiftR (v, 1)), 1)}
+ else if (!u_odd & v_odd) {gcd (bitwShiftR (u, 1), v)}
+ else if ( u_odd & !v_odd) {gcd (u, bitwShiftR (v, 1))}
+ else if ( u > v) {gcd (u - v, v)}
+ else {gcd (v - u, u)}
+}
+
+is_power_of_n <- function (number, n) {
+ if (number < 1) {FALSE}
+ else if (number == 1) {TRUE}
+ else if (number %% n != 0) {FALSE}
+ else {is_power_of_n (number / n, n)}
+}
+
+is_power_of_2 <- function (number) {
+ is_power_of_n (number, 2)
+}
+
+stdin <- file ('stdin', 'r')
+repeat {
+ line <- readLines (stdin, n = 1)
+ if (length (line) == 0) {
+ break
+ }
+ parts <- strsplit (line, " ")
+
+ m <- as.numeric (parts [[1]] [[1]])
+ n <- as.numeric (parts [[1]] [[2]])
+
+ r <- gcd (n, m)
+
+ if (r > 1 & is_power_of_2 (r)) {
+ cat ("1\n")
+ }
+ else {
+ cat ("0\n")
+ }
+}
diff --git a/challenge-136/abigail/r/ch-2.r b/challenge-136/abigail/r/ch-2.r
new file mode 100644
index 0000000000..452d060524
--- /dev/null
+++ b/challenge-136/abigail/r/ch-2.r
@@ -0,0 +1,29 @@
+#
+# See ../README.md
+#
+
+#
+# Run as: Rscript ch-2.r < input-file
+#
+
+count <- function (target, this_fib = 1, prev_fib = 1) {
+ if (target < this_fib) {
+ 0
+ } else if (target == this_fib) {
+ 1
+ } else {
+ count (target - this_fib, this_fib + prev_fib, this_fib) +
+ count (target, this_fib + prev_fib, this_fib)
+ }
+}
+
+
+
+stdin <- file ('stdin', 'r')
+repeat {
+ n <- readLines (stdin, n = 1)
+ if (length (n) == 0) {
+ break
+ }
+ cat (count (as.integer (n)), "\n")
+}
diff --git a/challenge-136/abigail/ruby/ch-1.rb b/challenge-136/abigail/ruby/ch-1.rb
index 72ef8542f1..aa050a843e 100644
--- a/challenge-136/abigail/ruby/ch-1.rb
+++ b/challenge-136/abigail/ruby/ch-1.rb
@@ -8,18 +8,20 @@
# Run as: ruby ch-1.rb < input-file
#
+def is_power_of_n (number, n)
+ return number < 1 ? false
+ : number == 1 ? true
+ : number % n > 0 ? false
+ : is_power_of_n(number / n, n)
+end
+
+def is_power_of_2 (number)
+ return is_power_of_n(number, 2)
+end
+
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)
+ r = m . gcd (n)
+ puts (r > 1 && is_power_of_2(r) ? 1 : 0)
end
diff --git a/challenge-136/abigail/scheme/ch-1.scm b/challenge-136/abigail/scheme/ch-1.scm
index 3e9728abbf..d537b754e0 100644
--- a/challenge-136/abigail/scheme/ch-1.scm
+++ b/challenge-136/abigail/scheme/ch-1.scm
@@ -24,19 +24,29 @@
)
;;;
-;;; 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)))))
+;;; Return #t if number is a power of n, that is, number == n ^ p
+;;; for some non-negative integer p. Return #f otherwise.
+;;;
+(define (is-power-of-n number n)
+ (cond ((< number 1) #f)
+ ((= number 1) #t)
+ ((> (modulo number n) 0) #f)
+ (else (is-power-of-n (/ number n) n)))
+)
+
+(define (is-power-of-2 number)
+ (is-power-of-n number 2)
+)
(define (main)
(define m (read))
(define n (read))
+ (define r)
(if (not (eof-object? m))
(begin
- (display (if (is-power-of-2 (gcd m n)) 1 0))(newline)
+ (set! r (gcd m n))
+ (display (if (and (> r 1) (is-power-of-2 r)) 1 0))
+ (newline)
(main)
)
)
diff --git a/challenge-136/abigail/t/ctest.ini b/challenge-136/abigail/t/ctest.ini
index 527781acbb..faa58e9882 100644
--- a/challenge-136/abigail/t/ctest.ini
+++ b/challenge-136/abigail/t/ctest.ini
@@ -6,3 +6,6 @@
[names]
1-1 = Given Examples
2-1 = Given Examples
+
+[languages/bc]
+add_to_input = 0
diff --git a/challenge-136/abigail/tcl/ch-1.tcl b/challenge-136/abigail/tcl/ch-1.tcl
index 53a25f8956..39210c7e16 100644
--- a/challenge-136/abigail/tcl/ch-1.tcl
+++ b/challenge-136/abigail/tcl/ch-1.tcl
@@ -30,18 +30,27 @@ proc gcd {u v} {
return [gcd [expr $v - $u] $u]
}
+#
+# Return 1 if number is a power of n, that is, number == n ^ p
+# for some non-negative integer p. Return 0 otherwise.
+#
+proc is_power_of_n {number n} {
+ if {$number < 1} {return 0}
+ if {$number == 1} {return 1}
+ if {$number % $n} {return 0}
+ return [is_power_of_n [expr $number / $n] $n]
+}
+
+proc is_power_of_2 {number} {
+ return [is_power_of_n $number 2]
+}
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
- }
+ set r [gcd $m $n]
+ if {$r > 1 && [is_power_of_2 $r]} {
+ puts 1
+ } else {
+ puts 0
}
- puts $valid
}