NikhilReddy
NikhilReddy

Reputation: 6954

GCD calucation for 4 Numbers

Edit BoxesI am developing an application which consists of Edit Texts, I will explain clearly based on steps:

a) Based on Spinner some item will contain 3 edit text boxes, and some will contain 4 edit-text boxes.

b) for this i will to calculate GCD, Currently i am using using GCD calculation for two Edit boxes, how can i calculate for ** Three Edit Boxes and Four Edit Boxes**

private long gcd(long a, long b) {

        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }

How i can write code for Three and four Edit Boxes.

Upvotes: 1

Views: 776

Answers (3)

Matthias Meid
Matthias Meid

Reputation: 12523

You can combine the two-argument gcd function:

gcd(a, b, c, d) = gcd(gcd(gcd(a, b), c), d)

This works for basically any number of arguments using a recursive implementation.

Upvotes: 1

Achintya Jha
Achintya Jha

Reputation: 12843

If you are finding the gcd of four numbers (a,b,c,d) then split should work.

Try this way:

gcd(a,b,c,d) = gcd(gcd (a,b) , gcd(c,d))

Upvotes: 0

Related Questions