This is a simple task but i can't seem to figure out how to do it
Here is a sample function structure
private double GetGCD(double num1, double num2)
{
//should return the GCD of the two double
}
test data
num1 = 6;
num2 = 3;
*return value must be 3*
num1 = 8.8;
num2 = 6.6;
*return value must be 2.2*
num1 = 5.1;
num2 = 8.5;
*return value must be 1.7*
note: maximum decimal places is 1. programming language is not important. i just need the algorthm
please help.. thank you!
If you have only one decimal place, multiply the numbers by 10, convert them to integers and run an integer GCD function.
This will also save you floating point precision errors.
Quoting this answer, the base Euclidean algorithm in Python (for integers!) is:
def gcd(a, b):
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a, b = b, a%b
return a
So, your code should be something like:
def gcd_floats(x,y):
return gcd( int(x*10), int(y*10) )/10