Questions tagged [greatest-common-divisor]

The greatest common divisor (GCD) of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder.

The greatest common divisor (GCD), also known as the greatest common factor (GCF), or highest common factor (HCF), of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder.

432 questions
108
votes
16 answers

Java: get greatest common divisor

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)…
Albert
  • 65,406
  • 61
  • 242
  • 386
72
votes
13 answers

How to find GCD, LCM on a set of numbers

What would be the easiest way to calculate Greatest Common Divisor and Least Common Multiple on a set of numbers? What math functions can be used to find this information?
user339108
  • 12,613
  • 33
  • 81
  • 112
57
votes
3 answers

JS how to find the greatest common divisor

I would like to find the greatest common divisor using JavaScript. Anyone done that before and willing to share?
bboy
  • 1,357
  • 2
  • 15
  • 31
47
votes
8 answers

"Approximate" greatest common divisor

Suppose you have a list of floating point numbers that are approximately multiples of a common quantity, for example 2.468, 3.700, 6.1699 which are approximately all multiples of 1.234. How would you characterize this "approximate gcd", and how…
44
votes
15 answers

what is the fastest way to find the gcd of n numbers?

what is the fastest way to compute the greatest common divisor of n numbers?
Themasterhimself
  • 1,016
  • 4
  • 17
  • 25
34
votes
11 answers

Euclidean algorithm (GCD) with multiple numbers?

So I'm writing a program in Python to get the GCD of any amount of numbers. def GCD(numbers): if numbers[-1] == 0: return numbers[0] # i'm stuck here, this is wrong for i in range(len(numbers)-1): print…
Tetramputechture
  • 2,911
  • 2
  • 33
  • 48
27
votes
13 answers

Python gcd for list

I want to calculate gcd for a list of numbers. But I don't know what's wrong with my code. A = [12, 24, 27, 30, 36] def Greatest_Common_Divisor(A): for c in A: while int(c) > 0: if int(c) > 12: c = int(c) %…
redmouse
  • 271
  • 1
  • 3
  • 3
25
votes
5 answers

GCD function in c++ sans cmath library

I'm writing a mixed numeral class and need a quick and easy 'greatest common divisor' function. Can anyone give me the code or a link to the code?
Connor Black
  • 6,921
  • 12
  • 39
  • 70
20
votes
1 answer

RSA: Private key calculation with Extended Euclidean Algorithm

I'm a high school student writing a paper on RSA, and I'm doing an example with some very small prime numbers. I understand how the system works, but I can't for the life of me calculate the private key using the extended euclidean algorithm. Here's…
mads
  • 225
  • 1
  • 2
  • 8
18
votes
6 answers

Euclidean greatest common divisor for more than two numbers

Can someone give an example for finding greatest common divisor algorithm for more than two numbers? I believe programming language doesn't matter.
Bogdan Gusiev
  • 8,027
  • 16
  • 61
  • 81
15
votes
3 answers

How to simplify a fraction

I want to simplify a fraction in my application. The fraction is like, x/y where x and y are integers. I want to simplify the fraction to its simplest form. Can anyone please give me hints how to do it. Thanks in advance.
user379888
15
votes
4 answers

Finding the GCD without looping - R

So I'm trying to learn R and using a number of resources including a book called "Discovering Statistics using R" and a bunch of other cool eBooks. I understand a great method in programming is the Euclid's Algorithm. Implementing it in a loop can…
Reanimation
  • 3,151
  • 10
  • 50
  • 88
13
votes
3 answers

What algorithm does Python employ in fractions.gcd()?

I'm using the fractions module in Python v3.1 to compute the greatest common divisor. I would like to know what algorithm is used. I'm guessing the Euclidean method, but would like to be sure. The docs…
Justin R.
  • 23,435
  • 23
  • 108
  • 157
12
votes
5 answers

How does the Euclidean Algorithm work?

I just found this algorithm to compute the greatest common divisor in my lecture notes: public static int gcd( int a, int b ) { while (b != 0) { final int r = a % b; a = b; b = r; } return a; } So r is the…
John Curtsy
  • 541
  • 3
  • 8
  • 15
12
votes
8 answers

How to write a simple Java program that finds the greatest common divisor between two numbers?

Here is the question: "Write a method named gcd that accepts two integers as parameters and returns the greatest common divisor of the two numbers. The greatest common divisor (GCD) of two integers a and b is the largest integer that is a factor of…
user1029481
  • 291
  • 3
  • 6
  • 12
1
2 3
28 29