tags:

views:

131

answers:

4

I have seen that such a function exists for BigInteger, i.e. BigInteger#gcd. Are there other functions in Java which also works 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!)

+1  A: 

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 = new BigInteger(""+a); // there's a better way to do this. I forget.
    BigInteger b2 = new BigInteger(""+b);
    BigInteger gcd = b1.gcd(b2);
    return gcd.intValue();
}
Tony Ennis
`BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue()` is much better.
Albert
`valueOf()`... cool, thanks.
Tony Ennis
+5  A: 
public int GCD(int a, int b)
{
   if (b==0) return a;
   return GCD(b,a%b);
}
Matt
This seems to work. If so, it's awesome, lol.
Tony Ennis
As far as I can tell it works fine. I just ran 100,000 random numbers though both methods and they agreed each time.
Tony Ennis
It's Euclidean algorithm... It's very old and proven right. http://en.wikipedia.org/wiki/Euclidean_algorithm
Rekin
Yep, I can kind of see it but I need more time to work through it. I like it.
Tony Ennis
Just to clarify: This is absolutely not what I was asking for.
Albert
@Albert, well you could always try it out with a generic type and see if it works. I dunno just a thought, but the algorithm is there for you to experiment with. As far as some standard library or class, I've never seen one. You will still need to specify when you create the object that it is an int, long, etc.. though.
Matt
@Albert, well, although Matt provided an implementation, you yourself could make it work in a, as you put is, "more generic" way, no? :)
Bart Kiers
@Bart: You misunderstood my question. I was not asking about how to implement this. I was asking if there is such a function already in Java.
Albert
@Albert, yes I misunderstood your question, and it seems you misunderstood my reply as well: I know you didn't ask **how** to implement this. But, to answer your question whether this is implemented in Java: it isn't (at least, not the way you are asking for).
Bart Kiers
With "more generic", I didn't meant necessarily Java Generics. Don't you know how most other math functions are presented in Java? They also are more generic in the sense that they provide different overloads for all the different types.
Albert
@Albert could you give an example? In all languages i've dealt with you have to tell the program what type of something you are working with. It may not be as clear as day and the compiler may take care of that work for you, but I'm pretty sure there is nothing like that.
Matt
@Albert, I never mentioned '(Java's) generics'. I meant that the overloading of these methods could be done by you yourself (even more so since the algorithm has been posted, even if you were already aware of said algorithm).
Bart Kiers
@Bart: Ah ok but I am still quite confused about that answer because I of course know that (because it is obvious and also follows from my question) and because that has nothing really to do with what I asked for. But maybe we are still talking about different things. I don't really know though how I should have formulate my question differently. I just wanted to know if such a function *already exists* in Java, besides the one in `BigInteger`.
Albert
@Matt: I'm not exactly sure what you mean and it also seems very off topic to my question. But if you are speaking about automatic type inference, you should take a look at the language Haskell; it's really worth it and is very enlightening!
Albert
@Albert yea, ive gone through that. And you specify types in haskell, So i guess i still don't know what you are talking about.
Matt
@Matt: I asked if Java already has such a function. Another one except `BigInteger#gcd`.
Albert
@Matt: Or to put it different: Java has the function `int java.lang.Math.max(int a, int b)`. Does it also have the function `int gcd(int a, int b)` somewhere?
Albert
No, hence why i posted one. But you can create a generic one if wanted to. Like stated above, you will have to tell it what it is though when you create an object to use that. public <E> GCD(<E> a, <E> b). GCD<Int> newGCD = new GCD<Int>(); while this may not be not be 100% correct, as i haven't touched java in years, but this should be someone correct and just to point out you can have a generic type.
Matt
+2  A: 

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;
}
Xorlev
Just to clarify: This is absolutely not what I was asking for.
Albert
In this case, you had not specified that you didn't want alternative implementations since one didn't exist. Only later did you edit your post not looking for implementations. I believe others had answered "no" more than adequately.
Xorlev
A: 

Because no one has really answered the original question yet:

No, there is no such function in Java.

Albert
I thought I did in the first sentence of my response...
Tony Ennis
@Tony: Ah yea, sorry, somehow I overread that I think :) (Because I just saw a bunch of code and thought that you also had misunderstood the question because I explicitly didn't want to get any source.)
Albert