Reputation: 47
I'm learning python and im trying to make a function to simplify fractions, if I E.G have 20/25, I want that to print 4/5.
numerator= 20
denominator= 25
common = denominator
if numerator < denominator:
common = numerator
if common < 1:
if numerator % common == 0 or denominator % common == 0:
numerator %= common ;
denominator %= common
common += 1
print('Simplified:', int(numerator) , '/', int(denominator))
This is what I'm working with now, duno if my brain stopped working but can't get further. Can anyone help me out where im thinking wrong?
Edit: Without using further modules
Upvotes: 1
Views: 1319
Reputation: 42133
Dividing each term by their gcd will give you the simplified fraction:
def gcd(a,b): return b if a<1 else gcd(b%a,a) # or use math.gcd
def frac(n,d): c=gcd(n,d);return n//c,d//c
For example:
frac(20,25) --> (4,5)
frac(25,20) --> (5,4)
frac(12345,67890) --> (823, 4526)
frac(14641, 4913) --> (14641, 4913)
print("Simplified: {}/{}".format(*frac(20,25)))
# Simplified: 4/5
Upvotes: 0
Reputation: 5479
Here is a simple answer that just divides the numerator and denominator by all the integers that came before them, if divisible.
def fraction(numerator, denominator):
for i in range(2, numerator+1):
while numerator % i == 0 and denominator % i == 0:
numerator /= i
denominator /= i
return f"{int(numerator)}/{int(denominator)}"
Here is another solution that just divides by prime numbers and also takes care of improper fractions:
def fraction(numerator, denominator):
# Take care of improper fractions.
whole_number = int(numerator/denominator)
numerator -= whole_number * denominator
# List contraction to generate prime numbers
primes = [x for x in range(2, numerator+1) if all(x % y != 0 for y in range(2, x))]
# Repeatedly divide numerator and denominator by prime numbers
for index in range(len(primes)):
while numerator % primes[index] == 0 and denominator % primes[index] == 0:
numerator /= primes[index]
denominator /= primes[index]
# Format differently for proper and improper fractions
if whole_number == 0:
return f"{int(numerator)}/{int(denominator)}"
return f"{whole_number} {int(numerator)}/{int(denominator)}"
Upvotes: 1
Reputation: 1775
You can use your own GCD function:
def GCD(a, b):
while b != 0:
t = b
b = a % b
a = t
return a if a != 0 else None
def fixnstr(s):
s = s.rstrip("0").rstrip(".")
if len(s) == 0:
s = "0"
return s
n = float(input("Enter the numerator: "))
d = float(input("Enter the denominator: "))
gcd = GCD(n, d)
n = fixnstr(str(n/gcd))
d = fixnstr(str(d/gcd))
print("Simplify: "+n+"/"+d)
This also removes extra 0
s and decimal points when possible, and n
and d
are str
ings.
Upvotes: 0
Reputation: 35522
If you don't want to import math.gcd
, make your own:
def gcd(x, y):
while y != 0:
(x, y) = (y, x % y)
return x
numerator = 20
denominator = 25
common = gcd(numerator, denominator)
# integer division since you know gcd must be a factor
numerator //= common
denominator //= common
print(f"{numerator}/{denominator}")
Upvotes: 1
Reputation: 284
You can use the Fraction module
from fractions import Fraction
numerator= 20
denominator= 25
fraction_value = Fraction(numerator,denominator)
print(fraction_value)
Output
4/5
Upvotes: 1