diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-31 21:21:38 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-31 21:21:38 +0000 |
| commit | 637eddf2bdf28ec2f7bb6ae27a98f2cea54cf5bb (patch) | |
| tree | a4a3e69aa7f5b11f8da411073b618d11c1eceaea /challenge-136 | |
| parent | ce469992b026a933f27f0e753632012efbe735c0 (diff) | |
| parent | 14a1b81924ec23b65d4d1da7ca6f742b8494ebed (diff) | |
| download | perlweeklychallenge-club-637eddf2bdf28ec2f7bb6ae27a98f2cea54cf5bb.tar.gz perlweeklychallenge-club-637eddf2bdf28ec2f7bb6ae27a98f2cea54cf5bb.tar.bz2 perlweeklychallenge-club-637eddf2bdf28ec2f7bb6ae27a98f2cea54cf5bb.zip | |
Merge pull request #5126 from Abigail/abigail/week-136
Check for even numbers.
Diffstat (limited to 'challenge-136')
| -rw-r--r-- | challenge-136/abigail/awk/ch-1.gawk | 4 | ||||
| -rw-r--r-- | challenge-136/abigail/bash/ch-1.sh | 6 | ||||
| -rw-r--r-- | challenge-136/abigail/bc/ch-1.bc | 8 | ||||
| -rw-r--r-- | challenge-136/abigail/c/ch-1.c | 4 | ||||
| -rw-r--r-- | challenge-136/abigail/go/ch-1.go | 4 | ||||
| -rw-r--r-- | challenge-136/abigail/java/ch-1.java | 5 | ||||
| -rw-r--r-- | challenge-136/abigail/lua/ch-1.lua | 9 | ||||
| -rw-r--r-- | challenge-136/abigail/node/ch-1.js | 4 | ||||
| -rw-r--r-- | challenge-136/abigail/pascal/ch-1.p | 20 | ||||
| -rw-r--r-- | challenge-136/abigail/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-136/abigail/python/ch-1.py | 13 | ||||
| -rw-r--r-- | challenge-136/abigail/r/ch-1.r | 15 | ||||
| -rw-r--r-- | challenge-136/abigail/ruby/ch-1.rb | 8 | ||||
| -rw-r--r-- | challenge-136/abigail/scheme/ch-1.scm | 12 | ||||
| -rw-r--r-- | challenge-136/abigail/tcl/ch-1.tcl | 12 |
15 files changed, 98 insertions, 30 deletions
diff --git a/challenge-136/abigail/awk/ch-1.gawk b/challenge-136/abigail/awk/ch-1.gawk index 12f563a4ea..0e8eb7d22a 100644 --- a/challenge-136/abigail/awk/ch-1.gawk +++ b/challenge-136/abigail/awk/ch-1.gawk @@ -37,6 +37,10 @@ function is_power_of_2 (number) { return is_power_of_n(number, 2) } +$1 % 2 || $2 % 2 { + print 0 + next +} { r = gcd($1, $2) diff --git a/challenge-136/abigail/bash/ch-1.sh b/challenge-136/abigail/bash/ch-1.sh index 857e3b7f7e..e965cbd96a 100644 --- a/challenge-136/abigail/bash/ch-1.sh +++ b/challenge-136/abigail/bash/ch-1.sh @@ -45,7 +45,11 @@ set -f while read n m -do gcd $n $m +do if ((n % 2 == 1 || m % 2 == 1)) + then echo 0 + continue + fi + gcd $n $m is_power_of_2 $gcd if ((gcd > 1 && is_power_of_2 == 1)) then echo 1 diff --git a/challenge-136/abigail/bc/ch-1.bc b/challenge-136/abigail/bc/ch-1.bc index 9a0c347386..a32d801dd8 100644 --- a/challenge-136/abigail/bc/ch-1.bc +++ b/challenge-136/abigail/bc/ch-1.bc @@ -28,8 +28,12 @@ define q (n) { while (1) { n = read (); if (n == 0) {break} m = read (); if (m == 0) {break} - r = g (n, m) - r > 1 && q (r) + if (n % 2 == 1 || m % 2 == 1) { + 0 + } else { + r = g (n, m) + r > 1 && q (r) + } } quit diff --git a/challenge-136/abigail/c/ch-1.c b/challenge-136/abigail/c/ch-1.c index d6fcc4d898..3c169dd635 100644 --- a/challenge-136/abigail/c/ch-1.c +++ b/challenge-136/abigail/c/ch-1.c @@ -45,6 +45,10 @@ int main (void) { long long n, m; while (scanf ("%lld %lld", &n, &m) == 2) { + if (n % 2 || m % 2) { + printf ("0\n"); + continue; + } long long r = gcd (n, m); printf ("%d\n", r > 1 && is_power_of_2 (r) ? 1 : 0); } diff --git a/challenge-136/abigail/go/ch-1.go b/challenge-136/abigail/go/ch-1.go index 6c9e59ab1b..18e85a5dba 100644 --- a/challenge-136/abigail/go/ch-1.go +++ b/challenge-136/abigail/go/ch-1.go @@ -46,6 +46,10 @@ func main () { if c != 2 || err != nil { break; } + if n % 2 == 1 || m % 2 == 1 { + fmt . Print ("0\n") + continue + } if r := gcd (m, n); r > 1 && is_power_of_2 (r) { fmt . Print ("1\n") } else { diff --git a/challenge-136/abigail/java/ch-1.java b/challenge-136/abigail/java/ch-1.java index 4fbc1db180..639ef643ca 100644 --- a/challenge-136/abigail/java/ch-1.java +++ b/challenge-136/abigail/java/ch-1.java @@ -46,6 +46,11 @@ public class ch1 { while (scanner . hasNextInt ()) { int n = scanner . nextInt (); int m = scanner . nextInt (); + + if (n % 2 == 1 || m % 2 == 1) { + System . out . println ("0"); + continue; + } int r = gcd (n, m); diff --git a/challenge-136/abigail/lua/ch-1.lua b/challenge-136/abigail/lua/ch-1.lua index 44ea00f251..369ee72246 100644 --- a/challenge-136/abigail/lua/ch-1.lua +++ b/challenge-136/abigail/lua/ch-1.lua @@ -31,10 +31,17 @@ end for line in io . lines () do local _, _, n, m = line : find ("([0-9]+)%s+([0-9]+)") - local r = gcd (tonumber (n), tonumber (m)) + n = tonumber (n) + m = tonumber (m) + if n % 2 == 1 or m % 2 == 1 then + print (0) + goto continue + end + local r = gcd (n, m) if r > 1 and is_power_of_2 (r) then print (1) else print (0) end + ::continue:: end diff --git a/challenge-136/abigail/node/ch-1.js b/challenge-136/abigail/node/ch-1.js index af49fa3977..4398a98d5a 100644 --- a/challenge-136/abigail/node/ch-1.js +++ b/challenge-136/abigail/node/ch-1.js @@ -33,6 +33,10 @@ function is_power_of_2 (number) { . createInterface ({input: process . stdin}) . on ('line', line => { let [m, n] = line . trim () . split (' ') . map (x => +x) + if (n % 2 == 1 || m % 2 == 1) { + console . log (0) + return + } let r = gcd (m, n) if (r > 1 && is_power_of_2 (r)) { console . log (1) diff --git a/challenge-136/abigail/pascal/ch-1.p b/challenge-136/abigail/pascal/ch-1.p index 391a4fe69f..7c3e122dee 100644 --- a/challenge-136/abigail/pascal/ch-1.p +++ b/challenge-136/abigail/pascal/ch-1.p @@ -12,7 +12,7 @@ Program XXX; (* Find the GCD, using Stein's algorithm *) (* (https://en.wikipedia.org/wiki/Binary_GCD_algorithm) *) (* *) -function gcd (u, v: integer): integer; +function gcd (u, v: longint): longint; var u_odd, v_odd: boolean; @@ -34,7 +34,7 @@ function gcd (u, v: integer): integer; (* Return true if number is a power of n, that is, number == n ^ p *) (* for some non-negative integer p. Return false otherwise. *) (* *) -function is_power_of_n (number, n: integer): boolean; +function is_power_of_n (number, n: longint): boolean; begin if number < 1 then is_power_of_n := false else if number = 1 then is_power_of_n := true @@ -42,7 +42,7 @@ function is_power_of_n (number, n: integer): boolean; else is_power_of_n := is_power_of_n (number div n, n); end; -function is_power_of_2 (number: integer): boolean; +function is_power_of_2 (number: longint): boolean; begin is_power_of_2 := is_power_of_n (number, 2); end; @@ -50,15 +50,19 @@ function is_power_of_2 (number: integer): boolean; var - m, n, r: integer; + m, n, r: longint; begin while (not eof) do begin readln (m, n); - r := gcd (m, n); - if (r > 1) and is_power_of_2 (r) then - writeln (1) - else + if (n mod 2 = 1) or (m mod 2 = 1) then writeln (0) + else begin + r := gcd (m, n); + if (r > 1) and is_power_of_2 (r) then + writeln (1) + else + writeln (0) + end end end. diff --git a/challenge-136/abigail/perl/ch-1.pl b/challenge-136/abigail/perl/ch-1.pl index ac0b598577..0c060f0299 100644 --- a/challenge-136/abigail/perl/ch-1.pl +++ b/challenge-136/abigail/perl/ch-1.pl @@ -59,6 +59,8 @@ sub is_power_of_2 ($number) { # Main program # while (<>) { - my $r = gcd split; + my ($n, $m) = split; + say (0), next if ($n % 2) || ($m % 2); + my $r = gcd $n, $m; 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 0c27429811..231e541d93 100644 --- a/challenge-136/abigail/python/ch-1.py +++ b/challenge-136/abigail/python/ch-1.py @@ -25,8 +25,13 @@ def is_power_of_2 (number): for line in fileinput . input (): m, n = line . strip () . split (' ') - r = math . gcd (int (m), int (n)) - if r > 1 and is_power_of_2 (r): - print (1) - else: + m = int (m) + n = int (n) + if m % 2 or n % 2: print (0) + else: + r = math . gcd (m, 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 index 1231a1c2a8..851c753f4e 100644 --- a/challenge-136/abigail/r/ch-1.r +++ b/challenge-136/abigail/r/ch-1.r @@ -42,12 +42,17 @@ repeat { 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") + if (n %% 2 == 1 | m %% 2 == 1) { + cat ("0\n") } else { - cat ("0\n") + 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/ruby/ch-1.rb b/challenge-136/abigail/ruby/ch-1.rb index aa050a843e..37e005400c 100644 --- a/challenge-136/abigail/ruby/ch-1.rb +++ b/challenge-136/abigail/ruby/ch-1.rb @@ -22,6 +22,10 @@ end ARGF . each_line do | line | m, n = line . split . map {|x| x . to_i} - r = m . gcd (n) - puts (r > 1 && is_power_of_2(r) ? 1 : 0) + if n % 2 == 1 || m % 2 == 1 then + puts (0) + else + r = m . gcd (n) + puts (r > 1 && is_power_of_2(r) ? 1 : 0) + end end diff --git a/challenge-136/abigail/scheme/ch-1.scm b/challenge-136/abigail/scheme/ch-1.scm index d537b754e0..aa6b75705a 100644 --- a/challenge-136/abigail/scheme/ch-1.scm +++ b/challenge-136/abigail/scheme/ch-1.scm @@ -44,8 +44,16 @@ (define r) (if (not (eof-object? m)) (begin - (set! r (gcd m n)) - (display (if (and (> r 1) (is-power-of-2 r)) 1 0)) + (display (cond ((= (modulo n 2) 1) 0) + ((= (modulo m 2) 1) 0) + (else + (begin + (set! r (gcd m n)) + (if (and (> r 1) (is-power-of-2 r)) 1 0) + ) + ) + ) + ) (newline) (main) ) diff --git a/challenge-136/abigail/tcl/ch-1.tcl b/challenge-136/abigail/tcl/ch-1.tcl index 39210c7e16..af5a72a3ea 100644 --- a/challenge-136/abigail/tcl/ch-1.tcl +++ b/challenge-136/abigail/tcl/ch-1.tcl @@ -47,10 +47,14 @@ proc is_power_of_2 {number} { while {[gets stdin line] >= 0} { lassign [split $line " "] m n - set r [gcd $m $n] - if {$r > 1 && [is_power_of_2 $r]} { - puts 1 - } else { + if {[expr $m % 2] == 1 || [expr $n % 2] == 1} { puts 0 + } else { + set r [gcd $m $n] + if {$r > 1 && [is_power_of_2 $r]} { + puts 1 + } else { + puts 0 + } } } |
