greatest-common-divisor

Why is my GCD program in C not running?

拟墨画扇 提交于 2021-01-06 03:24:25
问题 I am trying to find the GCD of two numbers using Euclid's algorithm in C (recursively) and I do know that mathematically it's not completely perfect yet as it neglects negative number conditions, but I just want this one to work for positive numbers for now. #include <stdio.h> int gcd(int m, int n); int main() { return gcd(60, 24); } int gcd(int m, int n) { if (m < n) { //swapping both a and b m = m + n; n = m - n; m = m - n; } if (m == n) { return m; } else { return gcd(n, m % n); } } 回答1:

Python gcd for list

孤街醉人 提交于 2020-07-20 07:47:50
问题 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) % 12 else: return 12 % int(c) print Greatest_Common_Divisor(A) 回答1: here is the piece of code, that I used: from fractions import gcd from functools import reduce def find_gcd(list): x = reduce(gcd, list) return x 回答2: def gcd (a,b): if (b == 0): return a else: return gcd (b, a % b) A = [12,

Python gcd for list

帅比萌擦擦* 提交于 2020-07-20 07:47:08
问题 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) % 12 else: return 12 % int(c) print Greatest_Common_Divisor(A) 回答1: here is the piece of code, that I used: from fractions import gcd from functools import reduce def find_gcd(list): x = reduce(gcd, list) return x 回答2: def gcd (a,b): if (b == 0): return a else: return gcd (b, a % b) A = [12,

Check if an integer is GCD of some elements in a given set

只愿长相守 提交于 2020-01-17 14:07:08
问题 Given a set of positive integers and an integer k . All of the elements in the set is divisible by k . How to check if k is greatest common divisor of some elements in the set? My idea: For every element a[i] in the set, I divide it by k . Then I get GCD of all of the element in the set (which was changed after I divided). If the GCD is equal to 1, then k is GCD of some elements in the set. I have make some test cases and I see it right. But the online judge doesn't accept. Please give me an

Check if an integer is GCD of some elements in a given set

风流意气都作罢 提交于 2020-01-17 14:07:08
问题 Given a set of positive integers and an integer k . All of the elements in the set is divisible by k . How to check if k is greatest common divisor of some elements in the set? My idea: For every element a[i] in the set, I divide it by k . Then I get GCD of all of the element in the set (which was changed after I divided). If the GCD is equal to 1, then k is GCD of some elements in the set. I have make some test cases and I see it right. But the online judge doesn't accept. Please give me an

Check if an integer is GCD of some elements in a given set

空扰寡人 提交于 2020-01-17 14:07:04
问题 Given a set of positive integers and an integer k . All of the elements in the set is divisible by k . How to check if k is greatest common divisor of some elements in the set? My idea: For every element a[i] in the set, I divide it by k . Then I get GCD of all of the element in the set (which was changed after I divided). If the GCD is equal to 1, then k is GCD of some elements in the set. I have make some test cases and I see it right. But the online judge doesn't accept. Please give me an

LCM of two numbers

混江龙づ霸主 提交于 2020-01-06 07:06:46
问题 I am getting wrong result for my LCM program. Ifirst find gcd of the numbers and then divide the product with gcd. int gcd(int x, int y) { while(y != 0) { int save = y; y = x % y; x = save; } return y; } int lcm(int x, int y) { int prod = x * y; int Gcd = gcd(x,y); int lcm = prod / Gcd; return lcm; } Any help much appreciated. 回答1: Your gcd function will always return 0 . Change return y; to return x; Understand the Euclid's algorithm: RULE 1: gcd(x,0) = x RULE 2: gcd(x,y) = gcd(y,x % y)

LCM of two numbers

眉间皱痕 提交于 2020-01-06 07:06:45
问题 I am getting wrong result for my LCM program. Ifirst find gcd of the numbers and then divide the product with gcd. int gcd(int x, int y) { while(y != 0) { int save = y; y = x % y; x = save; } return y; } int lcm(int x, int y) { int prod = x * y; int Gcd = gcd(x,y); int lcm = prod / Gcd; return lcm; } Any help much appreciated. 回答1: Your gcd function will always return 0 . Change return y; to return x; Understand the Euclid's algorithm: RULE 1: gcd(x,0) = x RULE 2: gcd(x,y) = gcd(y,x % y)