0 Replies - 315 Views - Last Post: 06 October 2010 - 01:49 PM

#1 Brewer   User is offline

  • Awesome
  • member icon

Reputation: 182
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Function to Check a Fraction for Reducibility

Posted 06 October 2010 - 01:49 PM

Description: Simply insert the snippet into your program. Make sure you call checkReduce(). (No arguments are needed)This snippet contains a function that can check whether or not a simple fraction (of the form x / y) is reducible!
def checkReduce():
    numerator = int(input("Numerator: "))
    denominator = int(input("Denominator: "))
    smaller = 0
    divisor = 0
    if numerator < denominator:
        smaller = numerator
    else:
        smaller = denominator
    for i in range(2,smaller+1):
        if numerator % i == 0 and denominator % i == 0:
            divisor = i

    if divisor == 0: print("Can not be reduced.")
    else: print("%i / %i" % (numerator/divisor, denominator/divisor))


Is This A Good Question/Topic? 0
  • +

Page 1 of 1