+ 3

Please can someone write a program to find the highest common factor of two integers for me.... I have tried but I couldn't

10th Feb 2018, 10:34 PM
Adams Michael Okyere
Adams Michael Okyere - avatar
4 Answers
+ 3
I have found the answer thanks.
10th Feb 2018, 10:50 PM
Adams Michael Okyere
Adams Michael Okyere - avatar
+ 2
here's a python program that I believe does what you want. it shouldn't be hard to convert to C++. def gcd(x,y).... is equivalent to gcd(x,y) { ... } #! /usr/bin/python #recursive GCD program def gcd(x, y): if x == 0: return(y) else: return( gcd( y%x, x)) print( "\ngcd(x, y) finds the Greatest Common Divisor of x and y") x = 341 y = 527 print( "gcd("+str(x)+", "+str(y)+") = " +str( gcd(x ,y))) print( "\n")
19th Apr 2018, 4:57 AM
Rick Shiffman
Rick Shiffman - avatar
0
Simple brute force for positive numbers (min the smaller one, and max the bigger one): for (int i=min; ; --i) if (min % i == 0 && max % i == 0) return i; //or { std::cout << i; break;}
10th Feb 2018, 10:48 PM
Vlad Serbu
Vlad Serbu - avatar