I'm working on something that's going to need to use the GCD algorithm quite a bit, and I'd like it to be as fast as possible. I've tried the normal method, binary method, and a memoisation method I thought would work better than it did. I copied the binary method from here, with minor tweaks.
I've been using a class called TestGCD for testing, here's the whole thing:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestGCD
{
private static class Pair<A>
{
private final A a_one;
private final A a_two;
public Pair(A a_one, A a_two)
{
this.a_one = a_one;
this.a_two = a_two;
}
#Override
public boolean equals(Object object)
{
if (this == object)
return true;
if (object == null)
return false;
if (!(object instanceof Pair))
return false;
final Pair other = (Pair) object;
if (a_one == null)
if (other.a_one != null)
return false;
if (a_two == null)
if (other.a_two != null)
return false;
if (a_one.equals(other.a_one))
if (a_two.equals(other.a_two))
return true;
if (a_one.equals(other.a_two))
if (a_two.equals(other.a_one))
return true;
return false;
}
public A getFirst()
{
return a_one;
}
public A getSecond()
{
return a_two;
}
#Override
public int hashCode()
{
final int prime = 31;
int result = 1;
final int aOneHash = a_one == null ? 0 : a_one.hashCode();
final int aTwoHash = a_two == null ? 0 : a_two.hashCode();
int resultOneWay = prime * result + aOneHash;
resultOneWay += prime * result + aTwoHash;
int resultOtherWay = prime * result + aTwoHash;
resultOtherWay += prime * result + aOneHash;
result += resultOneWay + resultOtherWay;
return result;
}
#Override
public String toString()
{
return String.format("%s, %s", a_one, a_two);
}
}
private final static Map<Pair<Integer>, Integer> STORAGE = new HashMap<>();
private static void addNewPairs(List<Pair<Integer>> newPairs, int result)
{
for (final Pair<Integer> pair : newPairs)
STORAGE.put(pair, result);
}
private static int gcd(int x, int y)
{
if (x == 0)
return y;
if (y == 0)
return x;
int gcdX = Math.abs(x);
int gcdY = Math.abs(y);
if (gcdX == 1 || gcdY == 1)
return 1;
while (gcdX != gcdY)
if (gcdX > gcdY)
gcdX -= gcdY;
else
gcdY -= gcdX;
return gcdX;
}
private static int gcdBinary(int x, int y)
{
int shift;
/* GCD(0, y) == y; GCD(x, 0) == x, GCD(0, 0) == 0 */
if (x == 0)
return y;
if (y == 0)
return x;
int gcdX = Math.abs(x);
int gcdY = Math.abs(y);
if (gcdX == 1 || gcdY == 1)
return 1;
/* Let shift := lg K, where K is the greatest power of 2 dividing both x and y. */
for (shift = 0; ((gcdX | gcdY) & 1) == 0; ++shift)
{
gcdX >>= 1;
gcdY >>= 1;
}
while ((gcdX & 1) == 0)
gcdX >>= 1;
/* From here on, gcdX is always odd. */
do
{
/* Remove all factors of 2 in gcdY -- they are not common */
/* Note: gcdY is not zero, so while will terminate */
while ((gcdY & 1) == 0)
/* Loop X */
gcdY >>= 1;
/*
* Now gcdX and gcdY are both odd. Swap if necessary so gcdX <= gcdY,
* then set gcdY = gcdY - gcdX (which is even). For bignums, the
* swapping is just pointer movement, and the subtraction
* can be done in-place.
*/
if (gcdX > gcdY)
{
final int t = gcdY;
gcdY = gcdX;
gcdX = t;
} // Swap gcdX and gcdY.
gcdY = gcdY - gcdX; // Here gcdY >= gcdX.
}while (gcdY != 0);
/* Restore common factors of 2 */
return gcdX << shift;
}
private static int gcdMemoised(int x, int y)
{
if (x == 0)
return y;
if (y == 0)
return x;
int gcdX = Math.abs(x);
int gcdY = Math.abs(y);
if (gcdX == 1 || gcdY == 1)
return 1;
final List<Pair<Integer>> newPairs = new ArrayList<>();
while (gcdX != gcdY)
{
final Pair<Integer> pair = new Pair<>(gcdX, gcdY);
final Integer result = STORAGE.get(pair);
if (result != null)
{
addNewPairs(newPairs, result);
return result;
}
else
newPairs.add(pair);
if (gcdX > gcdY)
gcdX -= gcdY;
else
gcdY -= gcdX;
}
addNewPairs(newPairs, gcdX);
return gcdX;
}
So is there a way of making this algorithm faster or is the original version the fastest I'm going to get? No suggestions of using another language please, I'm looking for an algorithm improvement. Clearly my memoisation attempt was an utter failure, but maybe someone here can see a flaw/improve on it.
You can use Euclid's algorithm. It is very simple to implement and it is more efficient. Here is a code for it:
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
The time complexity is O(log(A + B)), while the algorithms you are using are O(A + B). It scales better and is efficient for small a and b, too.
Here is what I came up with, on the same lines as #ILoveCoding
public static long gcd(long first, long second) {
long big = 0;
long small = 0;
if(first > second) {
big=first;
small=second;
}
else {
big=second;
small=first;
}
long temp = big % small;
while( (temp) > 1 ) {
big = small;
small = temp;
temp = big % small;
}
if( temp == 0 ) {
return small ;
}
else if( temp == 1) {
return 1;
}
else {
return -1; // will never occur. hack for compilation error.
}
}
Edit: Test cases !
System.out.println( gcd(10L, 5L));
System.out.println( gcd(11L, 7L));
System.out.println( gcd(15L, 21L));
System.out.println( gcd(-2L, -5L));
System.out.println( gcd(-2L, 2L));
Euclidean Algorithm used by author of the question (subtraction-based version) and accepted answer (mod-based) both seems to be quite not as efficient as Binary GCD Algorithm, so here it's code in java (taken from wikipidea)
static long gcd(long u, long v) {
int shift;
if (u == 0) return v;
if (v == 0) return u;
for (shift = 0; ((u | v) & 1) == 0; ++shift) {
u >>= 1;
v >>= 1;
}
while ((u & 1) == 0) {
u >>= 1;
}
do {
while ((v & 1) == 0) {
v >>= 1;
}
if (u > v) {
long t = v;
v = u;
u = t;
}
v = v - u;
} while (v != 0);
return u << shift;
}
However, Binary algorithm is not the fastest gcd algorithm. More here.
Use Euclidean Algorithm for GCD
The algorithm is based on below facts.
If we subtract smaller number from larger (we reduce larger number),
GCD doesn’t change. So if we keep subtracting repeatedly the larger
of two, we end up with GCD.
Now instead of subtraction, if we divide smaller number, the
algorithm stops when we find remainder 0.
Code:
import java.util.*;
import java.lang.*;
class GFG
{
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static void main(String[] args)
{
int a = 10, b = 15, g;
g = gcd(a, b);
System.out.println("GCD(" + a + " , " + b+ ") = " + g);
}
}
Time Complexity: O(Log min(a, b))
Related
I am trying to add the individual digits of a number together and determine whether the final answer is even or odd.
public static boolean isSumOfDigitsOdd(int n) {
if (n <= 0) {
return false;
} else if (n == 1) {
return true;
} else if (n == 2) {
return false;
} else if (n > 2) {
int temp1 = n % 10;
int temp2 = (n / 10) % 10;
int tempFinal = temp1 + temp2;
while (tempFinal > 2) {
tempFinal -= 2;
}
isDigitSumOdd((n / 100) + tempFinal);
}
}
The issue that I am facing is whether a return statement is always necessary for the recursion call to work. As seen from the code above, when I try runnning it gives out an error message saying that the return type must be a boolean type.
However, after adding in a boolean variable, I was able to get the code to work as shown below.
public static boolean isDigitSumOdd(int n) {
boolean x = false;
if (n <= 0) {
x = false;
} else if (n == 1) {
x = true;
} else if (n == 2) {
x = false;
} else if (n > 2) {
int temp1 = n % 10;
int temp2 = (n / 10) % 10;
int tempFinal = temp1 + temp2;
while (tempFinal > 2) {
tempFinal -= 2;
}
return isDigitSumOdd((n / 100) + tempFinal);
}
return x;
}
In this case, after adding the boolean variable, I could run the code smoothly and it would provide me with the desired outcome.
I am fairly new to recursion and am unsure about why this happens. After looking up online, I could only figure out that a return statement is not necessary only if the return type is a void type.
In any language the return statement is necessary when the return type is non-void. There are 2 errors in the first code:
no reason to put the last else if, as an else would be more appropriate
you must call return isDigitSumOdd((n / 100) + tempFinal); because otherwise your function isn't returning anything.
Instead if you actually call it the function returns the value returned by isDigitSumOdd((n / 100) + tempFinal);. That's the key point of recursion.
Now, I don't know if your code works aside from the errors I mentioned, so here's a cleaner solution. The ^ is a XOR operator.
public static boolean isDigitSumOdd(int n) {
if (n == 0) return false;
return isDigitSumOdd(n/10) ^ n%2 == 1;
}
The problem statement is kind of basic - if the input is a 32 bit signed integer output reversed integer, else output 0.
Here is the code I came up with
public class Solution {
public int reverse(int A) {
if(A>=2143483647 || A<-2143483647)
return 0;
if(A>=0)
return Integer.parseInt((new StringBuilder(String.valueOf(A))).reverse().toString());
else
return -1*Integer.parseInt((new StringBuilder(String.valueOf(-1*A))).reverse().toString());
}
}
The solution is not accepted. The problem is in my code or the test cases?
Assuming the input is int32, here is a possible approach, including checking for overflow.
public class Solution {
public int reverse(int A) {
//if(A < Integer.MIN_VALUE || A > Integer.MAX_VALUE) return 0;
boolean neg = A < 0;
A = Math.abs(A);
long ret = 0;
while(A != 0){
ret = ret*10 + A%10;
A = A/10;
}
if(ret > Integer.MAX_VALUE) return 0;
return neg ? -(int)ret : (int)ret;
}
}
Be mindful to change int to long if the input is bigger.
The accepted answer is close but misses some edge cases. Here is an answer based on the accepted answer, but hopefully correctly handles all cases.
public static int reverse(int A) {
long aLong = Math.abs((long)A);
long ret = 0;
while (aLong != 0) {
ret = ret * 10 + aLong % 10;
aLong = aLong / 10;
}
if (A < 0) {
ret = -ret;
}
if ((ret < Integer.MIN_VALUE) || (ret > Integer.MAX_VALUE)) {
return 0;
} else {
return (int) ret;
}
you can try reversing into a long parameter first (this guarantees that it'll not overflow), then do the checking afterwards.
public int reverse(int A) {
long reversed;
if(A>=0)
reversed= reverseString(A);
else
reversed -1* reverseString(-A) ;
//we do the checking only after we have done the reverse.
if(reversed > Integer.Max_VALUE || reversed < Integer.MIN_VALUE)
return 0;
else
return (int) reversed; //we do a down cast here.
}
public long reverseString(int A){
StringBuilder sb = new StringBuilder(""+A).reverse();
String s = sb.reverse().toString();
return Long.parseLong(s);
}
This code will take care of positive and negative both numbers.After reversal if result is greater than Integer.MAX_VALUE then it will return zero.
public static int reverse(int x) {
boolean positive = true;
if(x < 0) {
positive = false;
x = x * -1;
}
long result = 0;
while(x > 0) {
result = (result * 10) + (x % 10);
x = x / 10;
}
if(result > Integer.MAX_VALUE) {
return 0;
}
return positive ? (int)result : (int)(result * -1);
}
I created a bloom filter using murmur3, blake2b, and Kirsch-Mitzenmacher-optimization, as described in the second answer to this question: Which hash functions to use in a Bloom filter
However, when I was testing it, the bloom filter constantly had a much higher error rate than I was expecting.
Here is the code I used to generate the bloom filters:
public class BloomFilter {
private BitSet filter;
private int size;
private int hfNum;
private int prime;
private double fp = 232000; //One false positive every fp items
public BloomFilter(int count) {
size = (int)Math.ceil(Math.ceil(((double)-count) * Math.log(1/fp))/(Math.pow(Math.log(2),2)));
hfNum = (int)Math.ceil(((this.size / count) * Math.log(2)));
//size = (int)Math.ceil((hfNum * count) / Math.log(2.0));
filter = new BitSet(size);
System.out.println("Initialized filter with " + size + " positions and " + hfNum + " hash functions.");
}
public BloomFilter extraSecure(int count) {
return new BloomFilter(count, true);
}
private BloomFilter(int count, boolean x) {
size = (int)Math.ceil((((double)-count) * Math.log(1/fp))/(Math.pow(Math.log(2),2)));
hfNum = (int)Math.ceil(((this.size / count) * Math.log(2)));
prime = findPrime();
size = prime * hfNum;
filter = new BitSet(prime * hfNum);
System.out.println("Initialized filter with " + size + " positions and " + hfNum + " hash functions.");
}
public void add(String in) {
filter.set(getMurmur(in), true);
filter.set(getBlake(in), true);
if(this.hfNum > 2) {
for(int i = 3; i <= (hfNum); i++) {
filter.set(getHash(in, i));
}
}
}
public boolean check(String in) {
if(!filter.get(getMurmur(in)) || !filter.get(getBlake(in))) {
return false;
}
for(int i = 3; i <= hfNum; i++) {
if(!filter.get(getHash(in, i))) {
return false;
}
}
return true;
}
private int getMurmur(String in) {
int temp = murmur(in) % (size);
if(temp < 0) {
temp = temp * -1;
}
return temp;
}
private int getBlake(String in) {
int temp = new BigInteger(blake256(in), 16).intValue() % (size);
if(temp < 0) {
temp = temp * -1;
}
return temp;
}
private int getHash(String in, int i) {
int temp = ((getMurmur(in)) + (i * getBlake(in))) % size;
return temp;
}
private int findPrime() {
int temp;
int test = size;
while((test * hfNum) > size ) {
temp = test - 1;
while(!isPrime(temp)) {
temp--;
}
test = temp;
}
if((test * hfNum) < this.size) {
test++;
while(!isPrime(test)) {
test++;
}
}
return test;
}
private static boolean isPrime(int num) {
if (num < 2) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i * i <= num; i += 2)
if (num % i == 0) return false;
return true;
}
#Override
public String toString() {
final StringBuilder buffer = new StringBuilder(size);
IntStream.range(0, size).mapToObj(i -> filter.get(i) ? '1' : '0').forEach(buffer::append);
return buffer.toString();
}
}
Here is the code I'm using to test it:
public static void main(String[] args) throws Exception {
int z = 0;
int times = 10;
while(z < times) {
z++;
System.out.print("\r");
System.out.print(z);
BloomFilter test = new BloomFilter(4000);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
for(int i = 0; i < 4000; i++) {
test.add(blake256(Integer.toString(random.nextInt())));
}
int temp = 0;
int count = 1;
while(!test.check(blake512(Integer.toString(temp)))) {
temp = random.nextInt();
count++;
}
if(z == (times)) {
Files.write(Paths.get("counts.txt"), (Integer.toString(count)).getBytes(), StandardOpenOption.APPEND);
}else {
Files.write(Paths.get("counts.txt"), (Integer.toString(count) + ",").getBytes(), StandardOpenOption.APPEND);
}
if(z == 1) {
Files.write(Paths.get("counts.txt"), (Integer.toString(count) + ",").getBytes());
}
}
}
I expect to get a value relatively close to the fp variable in the bloom filter class, but instead I frequently get half that. Anyone know what I'm doing wrong, or if this is normal?
EDIT: To show what I mean by high error rates, when I run the code on a filter initialized with count 4000 and fp 232000, this was the output in terms of how many numbers the filter had to run through before it found a false positive:
158852,354114,48563,76875,156033,82506,61294,2529,82008,32624
This was generated using the extraSecure() method for initialization, and repeated 10 times to generate these 10 numbers; all but one of them took less than 232000 generated values to find a false positive. The average of the 10 is about 105540, and that's common no matter how many times I repeat this test.
Looking at the values it found, the fact that it found a false positive after only generating 2529 numbers is a huge issue for me, considering I'm adding 4000 data points.
I'm afraid I don't know where the bug is, but you can simplify a lot. You don't actually need prime size, you don't need SecureRandom, BigInteger, and modulo. All you need is a good 64 bit hash (seeded if possible, for example murmur):
long bits = (long) (entryCount * bitsPerKey);
int arraySize = (int) ((bits + 63) / 64);
long[] data = new long[arraySize];
int k = getBestK(bitsPerKey);
void add(long key) {
long hash = hash64(key, seed);
int a = (int) (hash >>> 32);
int b = (int) hash;
for (int i = 0; i < k; i++) {
data[reduce(a, arraySize)] |= 1L << index;
a += b;
}
}
boolean mayContain(long key) {
long hash = hash64(key, seed);
int a = (int) (hash >>> 32);
int b = (int) hash;
for (int i = 0; i < k; i++) {
if ((data[reduce(a, arraySize)] & 1L << a) == 0) {
return false;
}
a += b;
}
return true;
}
static int reduce(int hash, int n) {
// http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
return (int) (((hash & 0xffffffffL) * n) >>> 32);
}
static int getBestK(double bitsPerKey) {
return Math.max(1, (int) Math.round(bitsPerKey * Math.log(2)));
}
Turns out the issue was that the answer on the other page wasn't completely correct, and neither was the comment below it.
The comment said:
in the paper hash_i = hash1 + i x hash2 % p, where p is a prime, hash1 and hash2 is within range of [0, p-1], and the bitset consists k * p bits.
However, looking at the paper reveals that while all the hashes are mod p, each hash function is assigned a subset of the total bitset, which I understood to mean hash1 mod p would determine a value for indices 0 through p, hash2 mod p would determine a value for indices p through 2*p, and so on and so forth until the k value chosen for the bitset is reached.
I'm not 100% sure if adding this will fix my code, but it's worth a try. I'll update this if it works.
UPDATE: Didn't help. I'm looking into what else may be causing this problem.
Given an array, I need to find the indices of nearest non-coprime number (i.e. GCD(Ai, Aj) > 1 , for any Ai and Aj in the array, i != j ) Example, let the array be
[2 17 4 6 10]
The answer will be
[3 -1 4 3 4]
I've written this brute force code (which is O(n^2)) using Binary GCD method, which is not very efficient. I'm wondering if there's a faster way to do this. Particularly in O(NlogN)
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* #author Mayur Kulkarni
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
BladeReader in = new BladeReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
GCDPuz solver = new GCDPuz();
solver.solve(1, in, out);
out.close();
}
static class GCDPuz {
public static int gcd(int p, int q) {
if (q == 0) return p;
if (p == 0) return q;
// p and q even
if ((p & 1) == 0 && (q & 1) == 0) return gcd(p >> 1, q >> 1) << 1;
// p is even, q is odd
else if ((p & 1) == 0) return gcd(p >> 1, q);
// p is odd, q is even
else if ((q & 1) == 0) return gcd(p, q >> 1);
// p and q odd, p >= q
else if (p >= q) return gcd((p - q) >> 1, q);
// p and q odd, p < q
else return gcd(p, (q - p) >> 1);
}
public int coprime(int p, int q) {
if (p % 2 == 0 && q % 2 == 0) {
return 2;
} else if (p == q + 1 || q == p + 1) {
return 1;
} else {
return gcd(p, q);
}
}
public void solve(int testNumber, BladeReader in, PrintWriter out) {
int size = in.nextInt();
int[] arr = in.readIntArray(size);
int[] ans = new int[size];
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1) {
ans[i] = -1;
continue;
}
int left = i == 0 ? -1 : findLeft(arr, i);
int right = i == arr.length - 1 ? -1 : findRight(arr, i);
int leftDist = left == -1 ? -1 : i - left;
int rightDist = right == -1 ? -1 : right - i;
int anss = findNearestIndex(left, leftDist, right, rightDist);
ans[i] = anss == -1 ? -1 : anss + 1;
}
printa(ans, out);
}
private void printa(int[] ans, PrintWriter out) {
StringBuilder sb = new StringBuilder();
for (int an : ans) {
sb.append(an).append(" ");
}
out.println(sb.toString());
}
private int findRight(int[] arr, int i) {
if (arr[i] == -1) return -1;
for (int j = i + 1; j < arr.length; j++) {
if (coprime(arr[i], arr[j]) > 1) return j;
}
return -1;
}
private int findLeft(int[] arr, int i) {
if (arr[i] == -1) return -1;
for (int j = i - 1; j >= 0; j--) {
if (coprime(arr[i], arr[j]) > 1) return j;
}
return -1;
}
private int findNearestIndex(int one, int oneDist, int two, int twoDist) {
if (oneDist == -1 && twoDist == -1) return -1;
if (oneDist == -1) return two;
if (twoDist == -1) return one;
if (oneDist == twoDist) {
return Math.min(one, two);
}
return oneDist < twoDist ? one : two;
}
}
static class BladeReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public BladeReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
}
}
If you know your max value for your numbers and can afford to keep a list of primes, then factoring them may be a better solution for the average/random case. Otherwise, worst case complexity, it's still O(N*N) - think "all of them are primes" for the worst case.
Approach:
factor them and store a Map<prime, multiplicity>[N] + int closestNeigh[]
take a factor and O(N) determine for each of them the closest that contain that factor (prefix/sufix sums will be involved)
eliminate that factor from all the factor maps
take the next factor. Adjust the closest neighbor index only if new one is closest.
This may bring some "relief" on the line of O(N*<num_distict_factors>), but again if <num_distict_factors> == N (all primes), then it is still O(N*N)
If you are willing to get into factorization, one could traverse the list, once from the left, factoring each number, hashing the index of each new prime (with the prime as the key), updating the index of each prime already seen, and, of course, noting the nearest seen prime. Since this traversal would miss the nearest on the right, conduct another traversal from the right to update any nearer shared prime, using the factor lists already saved.
I have seen that such a function exists for BigInteger, i.e. BigInteger#gcd. Are there other functions in Java which also work for other types (int, long or Integer)? It seems this would make sense as java.lang.Math.gcd (with all kinds of overloads) but it is not there. Is it somewhere else?
(Don't confuse this question with "how do I implement this myself", please!)
As far as I know, there isn't any built-in method for primitives. But something as simple as this should do the trick:
public int gcd(int a, int b) {
if (b==0) return a;
return gcd(b,a%b);
}
You can also one-line it if you're into that sort of thing:
public int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); }
It should be noted that there is absolutely no difference between the two as they compile to the same byte code.
For int and long, as primitives, not really. For Integer, it is possible someone wrote one.
Given that BigInteger is a (mathematical/functional) superset of int, Integer, long, and Long, if you need to use these types, convert them to a BigInteger, do the GCD, and convert the result back.
private static int gcdThing(int a, int b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
Or the Euclidean algorithm for calculating the GCD...
public int egcd(int a, int b) {
if (a == 0)
return b;
while (b != 0) {
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
Unless I have Guava, I define like this:
int gcd(int a, int b) {
return a == 0 ? b : gcd(b % a, a);
}
Use Guava LongMath.gcd() and IntMath.gcd()
Jakarta Commons Math has exactly that.
ArithmeticUtils.gcd(int p, int q)
You can use this implementation of Binary GCD algorithm
public class BinaryGCD {
public static int gcd(int p, int q) {
if (q == 0) return p;
if (p == 0) return q;
// p and q even
if ((p & 1) == 0 && (q & 1) == 0) return gcd(p >> 1, q >> 1) << 1;
// p is even, q is odd
else if ((p & 1) == 0) return gcd(p >> 1, q);
// p is odd, q is even
else if ((q & 1) == 0) return gcd(p, q >> 1);
// p and q odd, p >= q
else if (p >= q) return gcd((p-q) >> 1, q);
// p and q odd, p < q
else return gcd(p, (q-p) >> 1);
}
public static void main(String[] args) {
int p = Integer.parseInt(args[0]);
int q = Integer.parseInt(args[1]);
System.out.println("gcd(" + p + ", " + q + ") = " + gcd(p, q));
}
}
From http://introcs.cs.princeton.edu/java/23recursion/BinaryGCD.java.html
Some implementations here are not working correctly if both numbers are negative. gcd(-12, -18) is 6, not -6.
So an absolute value should be returned, something like
public static int gcd(int a, int b) {
if (b == 0) {
return Math.abs(a);
}
return gcd(b, a % b);
}
we can use recursive function for find gcd
public class Test
{
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
// Driver method
public static void main(String[] args)
{
int a = 98, b = 56;
System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));
}
}
public int gcd(int num1, int num2) {
int max = Math.abs(num1);
int min = Math.abs(num2);
while (max > 0) {
if (max < min) {
int x = max;
max = min;
min = x;
}
max %= min;
}
return min;
}
This method uses the Euclid’s algorithm to get the "Greatest Common Divisor" of two integers. It receives two integers and returns the gcd of them. just that easy!
If you are using Java 1.5 or later then this is an iterative binary GCD algorithm which uses Integer.numberOfTrailingZeros() to reduce the number of checks and iterations required.
public class Utils {
public static final int gcd( int a, int b ){
// Deal with the degenerate case where values are Integer.MIN_VALUE
// since -Integer.MIN_VALUE = Integer.MAX_VALUE+1
if ( a == Integer.MIN_VALUE )
{
if ( b == Integer.MIN_VALUE )
throw new IllegalArgumentException( "gcd() is greater than Integer.MAX_VALUE" );
return 1 << Integer.numberOfTrailingZeros( Math.abs(b) );
}
if ( b == Integer.MIN_VALUE )
return 1 << Integer.numberOfTrailingZeros( Math.abs(a) );
a = Math.abs(a);
b = Math.abs(b);
if ( a == 0 ) return b;
if ( b == 0 ) return a;
int factorsOfTwoInA = Integer.numberOfTrailingZeros(a),
factorsOfTwoInB = Integer.numberOfTrailingZeros(b),
commonFactorsOfTwo = Math.min(factorsOfTwoInA,factorsOfTwoInB);
a >>= factorsOfTwoInA;
b >>= factorsOfTwoInB;
while(a != b){
if ( a > b ) {
a = (a - b);
a >>= Integer.numberOfTrailingZeros( a );
} else {
b = (b - a);
b >>= Integer.numberOfTrailingZeros( b );
}
}
return a << commonFactorsOfTwo;
}
}
Unit test:
import java.math.BigInteger;
import org.junit.Test;
import static org.junit.Assert.*;
public class UtilsTest {
#Test
public void gcdUpToOneThousand(){
for ( int x = -1000; x <= 1000; ++x )
for ( int y = -1000; y <= 1000; ++y )
{
int gcd = Utils.gcd(x, y);
int expected = BigInteger.valueOf(x).gcd(BigInteger.valueOf(y)).intValue();
assertEquals( expected, gcd );
}
}
#Test
public void gcdMinValue(){
for ( int x = 0; x < Integer.SIZE-1; x++ ){
int gcd = Utils.gcd(Integer.MIN_VALUE,1<<x);
int expected = BigInteger.valueOf(Integer.MIN_VALUE).gcd(BigInteger.valueOf(1<<x)).intValue();
assertEquals( expected, gcd );
}
}
}
Is it somewhere else?
Apache! - it has both gcd and lcm, so cool!
However, due to profoundness of their implementation, it's slower compared to simple hand-written version (if it matters).
/*
import scanner and instantiate scanner class;
declare your method with two parameters
declare a third variable;
set condition;
swap the parameter values if condition is met;
set second conditon based on result of first condition;
divide and assign remainder to the third variable;
swap the result;
in the main method, allow for user input;
Call the method;
*/
public class gcf {
public static void main (String[]args){//start of main method
Scanner input = new Scanner (System.in);//allow for user input
System.out.println("Please enter the first integer: ");//prompt
int a = input.nextInt();//initial user input
System.out.println("Please enter a second interger: ");//prompt
int b = input.nextInt();//second user input
Divide(a,b);//call method
}
public static void Divide(int a, int b) {//start of your method
int temp;
// making a greater than b
if (b > a) {
temp = a;
a = b;
b = temp;
}
while (b !=0) {
// gcd of b and a%b
temp = a%b;
// always make a greater than b
a =b;
b =temp;
}
System.out.println(a);//print to console
}
}
I used this method that I created when I was 14 years old.
public static int gcd (int a, int b) {
int s = 1;
int ia = Math.abs(a);//<-- turns to absolute value
int ib = Math.abs(b);
if (a == b) {
s = a;
}else {
while (ib != ia) {
if (ib > ia) {
s = ib - ia;
ib = s;
}else {
s = ia - ib;
ia = s;
}
}
}
return s;
}
Those GCD functions provided by Commons-Math and Guava have some differences.
Commons-Math throws an ArithematicException.class only for Integer.MIN_VALUE or Long.MIN_VALUE.
Otherwise, handles the value as an absolute value.
Guava throws an IllegalArgumentException.class for any negative values.
The % going to give us the gcd Between two numbers, it means:-
% or mod of big_number/small_number are =gcd,
and we write it on java like this big_number % small_number.
EX1: for two integers
public static int gcd(int x1,int x2)
{
if(x1>x2)
{
if(x2!=0)
{
if(x1%x2==0)
return x2;
return x1%x2;
}
return x1;
}
else if(x1!=0)
{
if(x2%x1==0)
return x1;
return x2%x1;
}
return x2;
}
EX2: for three integers
public static int gcd(int x1,int x2,int x3)
{
int m,t;
if(x1>x2)
t=x1;
t=x2;
if(t>x3)
m=t;
m=x3;
for(int i=m;i>=1;i--)
{
if(x1%i==0 && x2%i==0 && x3%i==0)
{
return i;
}
}
return 1;
}