Topic was automatically imported from the old Question2Answer platform.
Asked By
Konrad
How to make “Fraction Simplification” ? Like
9/18 return 1/2
14/21 return 2/3
I did not find such a function in the documentation
We don’t know what you want. Do you want fractions to reduce to their smallest denominator? Do you want to maintain the fraction in the code? A division operation always returns a number in the form of an integer or a floating point number.
Ertain | 2021-01-03 19:43
How i can find “Greatest common factor” of 2 numbers?
When you divide int by int, you will get int as result (fraction part will be discarded). To get float in result, you must make one of the numbers float. This can be done in two ways:
If you have number, you can add .0 to one of the values: 9.0 / 18 will give you 0.5
If your number is variable, then you have to use explicit type conversion: float(n) / 18 will give you 0.5 for n = 9.
To find greatest common factor you can use next function:
func gcd(a: int, b: int) -> int:
return a if b == 0.0 else gcd(b, a % b)