greatest-common-divisor

Implementing Extended Euclidean algorithm

房东的猫 提交于 2019-12-13 09:57:27
问题 I want to make a function combine which given two integers n and m, returns a triple of integers (a, b, gcd(n, m)) such that: am + bn = gcd(n, m) Should not assume that the integers will always be positive. gcd :: Int -> Int -> Int gcd n m | n == m = n | n > m = gcd (n-m) m | n < m = gcd n (m-n) combine :: Int ->Int -> (Int,Int,Int) x1=1; y1=0; x2=0; y2=1 while ( m /=0 ) ( q=div n m ; r=mod n m ; n=m ; m=r t=x2 ; x2=x1-q*x2 ; x1=t t=y2 ; y2=y1-q*y2 ; y1=t ) combine n m = (x1,y1,gcd(n,m)) You

Nth HCF of two number

江枫思渺然 提交于 2019-12-13 06:15:52
问题 I came across a coding quiz, given two no A and B find the nth HCF of the two no for eg 16 , 8 HCF 8, 4, 2, 1 so 3rd HCF is 2 I solved like this 1. X = GCD(A,B) 2. Find all factor of X 3. Sort the factor in order But I want to know better approach Thanks 回答1: I think that the approach you have mentioned in description above is optimal except for the last step where you essentially do not need to sort the factors - you can simply generate them in increasing order. You can read this interesting

GCD algorithms for a large integers

可紊 提交于 2019-12-12 08:56:27
问题 I looking for the information about fast GCD computation algorithms. Especially, I would like to take a look at the realizations of that. The most interesting for me: - Lehmer GCD algorithm, - Accelerated GCD algorithm, - k-ary algorithm, - Knuth-Schonhage with FFT. I have completely NO information about the accelerated GCD algorithm, I just have seen a few articles where it was mentioned as the most effective and fast gcd computation method on the medium inputs (~1000 bits) They looks much

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

别等时光非礼了梦想. 提交于 2019-12-12 07:29:23
问题 This question already has answers here : How to find GCD, LCM on a set of numbers (13 answers) Closed 5 years ago . 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 both a and b. The GCD of any number and 1 is 1, and the GCD of any number and 0 is that number. One efficient way to compute the GCD

How Can I Write a Proper JUnit Test for this code?

↘锁芯ラ 提交于 2019-12-12 04:25:15
问题 I'm new to programming. I have to write a JUnit test for this program to find the GCD, shown here : public class CoprimeNumbersTest { /** * Given two integers, this returns true if they are relatively prime and false if they are not. Based upon the first * webpage I found ({@link "https://primes.utm.edu/notes/faq/negative_primes.html"}), the primality of negative * numbers is up for debate. This method will not treat negatives differently. * * @param a First integer to be tested * @param b

Recursive GCD not returning expected results [closed]

我与影子孤独终老i 提交于 2019-12-12 03:59:12
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . This function in Python is to find the greatest common divisor of 2 integers recursively. But I was not able to get it to work as it should be in the test def gcdRecur(a, b): if a > b: (a,b) = (b,a) if b%a == 0: #print("b%a == 0") print ("a is " + str(a)) return a else: gcdRecur(b%a,b) print("gcdRecur(45, 42) "

While loop to check user input for only positive integers

跟風遠走 提交于 2019-12-11 11:57:36
问题 I need help implementing a loop that keeps telling the user to enter only positive integers, but I don't know where to start. Can someone help me. import java.util.Scanner; public class GCD { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a positive integer: "); int firstN = input.nextInt(); System.out.print("Enter another positive integer: "); int secondN = input.nextInt(); System.out.println("The GCD of " + firstN + " and " + secondN

Algorithm - GCD and LCM problems

孤者浪人 提交于 2019-12-10 11:44:31
问题 Input for this problem is an array A of positive integers and single positive integer k. The output of the program is True if k is in the set S defined below, False otherwise. Define the set S as follows: if x is in A then x is in S if x and y are in S, then GCD(x,y) is in S if x and y are in S, then LCD(x,y) is in S Additional constraints: The size of the array A is ≤ 50000, k ≤ 10 12 , and x ≤ 10 12 for each x in A. The program must return an answer in 1 second or less. I don't have any

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

寵の児 提交于 2019-12-10 01:52:13
问题 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 (http://docs.python.org/py3k/library/fractions.html?highlight=fractions.gcd#fractions.gcd) don't help. Can anybody clue me in? 回答1: According to the 3.1.2 source code online, here's gcd as defined in Python-3.1.2/Lib/fractions.py : def gcd(a, b): """Calculate the Greatest Common Divisor of a

Look for the GCD (greatest common divisor) of more than 2 integers?

牧云@^-^@ 提交于 2019-12-08 19:00:57
问题 I already have a function that finds the GCD of 2 numbers. function getGCDBetween($a, $b) { while ($b != 0) { $m = $a % $b; $a = $b; $b = $m; } return $a; } But now, I would like to extend this function to find the GCD of N points. Any suggestion ? 回答1: There is a more elegant way to do this : // Recursive function to compute gcd (euclidian method) function gcd ($a, $b) { return $b ? gcd($b, $a % $b) : $a; } // Then reduce any list of integer echo array_reduce(array(42, 56, 28), 'gcd'); // ==