Skip to main content

I know that Euclid'sEuclid’s algorithm is the best algorithm for getgetting the GCD (great common divisor) forof a list theof positive integer numbersintegers. But, in the practice, you can write two codes por evaluate the gcdcode this algorithm in various ways. (forIn my case, iI decided to use javaJava, but cC/c++C++ may be another option).

I need to getuse the most efficient code of two possibilities form to programmingpossible in my program.

Recursive ModeIn recursive mode, you can write...:

static long gcd (long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (b==0) ? a : gcd(b, a%b);
  }

And, in iterative mode, it looks like ...this:

static long gcd (long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

We can do that withThere is also the Binary algorithm for the GCD, and the easy code iswhich may be coded simply like thatthis:

int gcd (int a, int b)
{
    while(b) b ^= a ^= b ^= a %= b;
    return a;
}

I know that Euclid's algorithm is the best algorithm for get the GCD (great common divisor) for a list the positive integer numbers. But, in the practice, you can write two codes por evaluate the gcd (for my case, i decided use java, but c/c++ may be another option).

I need to get the most efficient code of two possibilities form to programming.

Recursive Mode, you can write...

static long gcd(long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (b==0) ? a : gcd(b, a%b);
  }

And, iterative mode, looks like ...

static long gcd(long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

We can do that with the Binary GCD, and the easy code is like that

int gcd(int a, int b)
{
    while(b) b ^= a ^= b ^= a %= b;
    return a;
}

I know that Euclid’s algorithm is the best algorithm for getting the GCD (great common divisor) of a list of positive integers. But in practice you can code this algorithm in various ways. (In my case, I decided to use Java, but C/C++ may be another option).

I need to use the most efficient code possible in my program.

In recursive mode, you can write:

static long gcd (long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (b==0) ? a : gcd(b, a%b);
  }

And in iterative mode, it looks like this:

static long gcd (long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

There is also the Binary algorithm for the GCD, which may be coded simply like this:

int gcd (int a, int b)
{
    while(b) b ^= a ^= b ^= a %= b;
    return a;
}
Tweeted twitter.com/#!/StackCompSci/status/605789898755301377
corrected typo in the recursive version as remarked by andrewm921
Source Link

I know that Euclid's algorithm is the best algorithm for get the GCD (great common divisor) for a list the positive integer numbers. But, in the practice, you can write two codes por evaluate the gcd (for my case, i decided use java, but c/c++ may be another option).

I need to get the most efficient code of two possibilities form to programming.

Recursive Mode, you can write...

static long gcd(long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (a==0b==0) ?b a : gcd(b, a%b);
  }

And, iterative mode, looks like ...

static long gcd(long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

Regards,

UPDATE

 

We can do that with the Binary GCD, and the easy code is like that

int gcd(int a, int b)
{
    while(b) b ^= a ^= b ^= a %= b;
    return a;
}

Great Discussion.

I know that Euclid's algorithm is the best algorithm for get the GCD (great common divisor) for a list the positive integer numbers. But, in the practice, you can write two codes por evaluate the gcd (for my case, i decided use java, but c/c++ may be another option).

I need to get the most efficient code of two possibilities form to programming.

Recursive Mode, you can write...

static long gcd(long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (a==0)?b:gcd(b, a%b);
  }

And, iterative mode, looks like ...

static long gcd(long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

Regards,

UPDATE

We can do that with the Binary GCD, and the easy code is like that

int gcd(int a, int b)
{
    while(b) b ^= a ^= b ^= a %= b;
    return a;
}

Great Discussion.

I know that Euclid's algorithm is the best algorithm for get the GCD (great common divisor) for a list the positive integer numbers. But, in the practice, you can write two codes por evaluate the gcd (for my case, i decided use java, but c/c++ may be another option).

I need to get the most efficient code of two possibilities form to programming.

Recursive Mode, you can write...

static long gcd(long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (b==0) ? a : gcd(b, a%b);
  }

And, iterative mode, looks like ...

static long gcd(long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
 

We can do that with the Binary GCD, and the easy code is like that

int gcd(int a, int b)
{
    while(b) b ^= a ^= b ^= a %= b;
    return a;
}
added 208 characters in body
Source Link

I know that Euclid's algorithm is the best algorithm for get the GCD (great common divisor) for a list the positive integer numbers. But, in the practice, you can write two codes por evaluate the gcd (for my case, i decided use java, but c/c++ may be another option).

I need to get the most efficient code of two possibilities form to programming.

Recursive Mode, you can write...

static long gcd(long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (a==0)?b:gcd(b, a%b);
  }

And, iterative mode, looks like ...

static long gcd(long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

Regards,

UPDATE

We can do that with the Binary GCD, and the easy code is like that

int gcd(int a, int b)
{
    while(b) b ^= a ^= b ^= a %= b;
    return a;
}

Great Discussion.

I know that Euclid's algorithm is the best algorithm for get the GCD (great common divisor) for a list the positive integer numbers. But, in the practice, you can write two codes por evaluate the gcd (for my case, i decided use java, but c/c++ may be another option).

I need to get the most efficient code of two possibilities form to programming.

Recursive Mode, you can write...

static long gcd(long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (a==0)?b:gcd(b, a%b);
  }

And, iterative mode, looks like ...

static long gcd(long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

Regards,

I know that Euclid's algorithm is the best algorithm for get the GCD (great common divisor) for a list the positive integer numbers. But, in the practice, you can write two codes por evaluate the gcd (for my case, i decided use java, but c/c++ may be another option).

I need to get the most efficient code of two possibilities form to programming.

Recursive Mode, you can write...

static long gcd(long a, long b){
    a = Math.abs(a); b = Math.abs(b);
    return (a==0)?b:gcd(b, a%b);
  }

And, iterative mode, looks like ...

static long gcd(long a, long b) {
  long r, i;
  while(b!=0){
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

Regards,

UPDATE

We can do that with the Binary GCD, and the easy code is like that

int gcd(int a, int b)
{
    while(b) b ^= a ^= b ^= a %= b;
    return a;
}

Great Discussion.

edited body
Source Link
Loading
Source Link
Loading