+ 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
3 odpowiedzi
+ 3
I have found the answer thanks.
+ 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")
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;}