diff options
| author | Abigail <abigail@abigail.be> | 2021-10-25 16:51:51 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-10-25 16:51:51 +0200 |
| commit | d1cdf6de69edcdc8e52b8246e607e4af28d126e3 (patch) | |
| tree | 6ad9cb73cf808dc8c148d659ea39335f73cd02bc | |
| parent | b4715406dd11296d604b36ab31b238ad17cd4fe9 (diff) | |
| download | perlweeklychallenge-club-d1cdf6de69edcdc8e52b8246e607e4af28d126e3.tar.gz perlweeklychallenge-club-d1cdf6de69edcdc8e52b8246e607e4af28d126e3.tar.bz2 perlweeklychallenge-club-d1cdf6de69edcdc8e52b8246e607e4af28d126e3.zip | |
Rearranged the gcd sub
| -rw-r--r-- | challenge-136/abigail/perl/ch-1.pl | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/challenge-136/abigail/perl/ch-1.pl b/challenge-136/abigail/perl/ch-1.pl index 878aa3a81b..445d00a1fd 100644 --- a/challenge-136/abigail/perl/ch-1.pl +++ b/challenge-136/abigail/perl/ch-1.pl @@ -19,7 +19,8 @@ use experimental 'lexical_subs'; # # We need the GCD of two numbers. We did this in the past a few times, -# for instance in week 82. So, we copy-and-pasted the code from that week. +# 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 @@ -33,15 +34,14 @@ my %power_of_2 = map {1 << $_ => 1} 1 .. 62; # sub gcd; sub gcd ($u, $v) { - return $u if $u == $v || !$v; - return $v if !$u; - my $u_odd = $u % 2; - my $v_odd = $v % 2; - 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_odd && !$v_odd; - return gcd ($u - $v, $v) if $u > $v; - return gcd ($v - $u, $u); + 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) } # |
