File tree Expand file tree Collapse file tree
src/main/java/com/williamfiset/algorithms/math Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -245,7 +245,7 @@ $ java -cp classes com.williamfiset.algorithms.search.BinarySearch
245245- [ Totient function (phi function, relatively prime number count)] ( src/main/java/com/williamfiset/algorithms/math/EulerTotientFunction.java ) ** - O(n<sup >1/4</sup >)**
246246- [ Totient function using sieve (phi function, relatively prime number count)] ( src/main/java/com/williamfiset/algorithms/math/EulerTotientFunctionWithSieve.java ) ** - O(nlog(log(n)))**
247247- [ Extended euclidean algorithm] ( src/main/java/com/williamfiset/algorithms/math/ExtendedEuclideanAlgorithm.java ) ** - ~ O(log(a + b))**
248- - [ Greatest Common Divisor (GCD)] ( src/main/java/com/williamfiset/algorithms/math/GCD .java ) ** - ~ O(log(a + b))**
248+ - [ Greatest Common Divisor (GCD)] ( src/main/java/com/williamfiset/algorithms/math/Gcd .java ) ** - ~ O(log(a + b))**
249249- [ Fast Fourier transform (quick polynomial multiplication)] ( src/main/java/com/williamfiset/algorithms/math/FastFourierTransform.java ) ** - O(nlog(n))**
250250- [ Fast Fourier transform (quick polynomial multiplication, complex numbers)] ( src/main/java/com/williamfiset/algorithms/math/FastFourierTransformComplexNumbers.java ) ** - O(nlog(n))**
251251- [ Primality check] ( src/main/java/com/williamfiset/algorithms/math/IsPrime.java ) ** - O(√n)**
Original file line number Diff line number Diff line change @@ -35,10 +35,10 @@ java_binary(
3535 runtime_deps = [":math" ],
3636)
3737
38- # bazel run //src/main/java/com/williamfiset/algorithms/math:GCD
38+ # bazel run //src/main/java/com/williamfiset/algorithms/math:Gcd
3939java_binary (
40- name = "GCD " ,
41- main_class = "com.williamfiset.algorithms.math.GCD " ,
40+ name = "Gcd " ,
41+ main_class = "com.williamfiset.algorithms.math.Gcd " ,
4242 runtime_deps = [":math" ],
4343)
4444
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ /**
2+ * Computes the Greatest Common Divisor (GCD) of two numbers using the Euclidean algorithm.
3+ *
4+ * <p>Time: ~O(log(a + b))
5+ *
6+ * @author William Fiset, william.alexandre.fiset@gmail.com
7+ */
8+ package com .williamfiset .algorithms .math ;
9+
10+ public class Gcd {
11+
12+ /**
13+ * Computes the Greatest Common Divisor (GCD) of a and b. The returned value is always
14+ * non-negative.
15+ */
16+ public static long gcd (long a , long b ) {
17+ if (b == 0 )
18+ return Math .abs (a );
19+ return gcd (b , a % b );
20+ }
21+
22+ public static void main (String [] args ) {
23+ System .out .println (gcd (12 , 18 )); // 6
24+ System .out .println (gcd (-12 , 18 )); // 6
25+ System .out .println (gcd (12 , -18 )); // 6
26+ System .out .println (gcd (-12 , -18 )); // 6
27+
28+ System .out .println (gcd (5 , 0 )); // 5
29+ System .out .println (gcd (0 , 5 )); // 5
30+ System .out .println (gcd (-5 , 0 )); // 5
31+ System .out .println (gcd (0 , -5 )); // 5
32+ System .out .println (gcd (0 , 0 )); // 0
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments