# Function To Reduce Basic Fractions

Page 1 of 1

## 8 Replies - 4509 Views - Last Post: 07 October 2010 - 03:28 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=193784&amp;s=ddbeda4db16bf3af85db55d15437886b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Brewer

• Awesome

Reputation: 178
• Posts: 1,044
• Joined: 14-June 10

# Function To Reduce Basic Fractions

Posted 06 October 2010 - 12:28 PM

I have written a function that is supposed to tell whether or not a fraction is reducible. If it is, then the reduced fraction is supposed to be returned; otherwise, the sentence "Can not be reduced." is supposed to be returned.

The problem is, nothing is being returned. Helpz?

```def checkReduce():
numerator = input("Numerator: ")
denominator = input("Denominator: ")
num = int(numerator)
den = int(denominator)
smaller = 0
if num < den:
smaller = num
else:
smaller = den
for i in range(2,int(smaller/2)+1):
if num % i == 0 and den % i == 0:
return(num / i) + " / " + (den / i)
else:
return("Can not be reduced.")
```

Is This A Good Question/Topic? 0

## Replies To: Function To Reduce Basic Fractions

### #2 Motoma

Reputation: 450
• Posts: 795
• Joined: 08-June 10

## Re: Function To Reduce Basic Fractions

Posted 06 October 2010 - 12:53 PM

There are a couple of problems. First, you're algorithm is wrong: You will want to count through all numbers up until the smaller of (numerator/2, denominator), and you probably don't want to quit when you hit your first reduction. These will cause small numbers to fail, but bigger fractions (80/10) will throw a second error...

The second error is caused by your first return statement. You cannot simply add a string to an integer in that order. What you most likely want is a format string similar to:
```return "%i / %i" % (num/i, den/i)

```

Also, if I remember correctly, you are using Python 3.1.2. In that case, you can just use the fractions module to get the GCD, and divide your numerator and denominator to get the most reduced fraction:
```from fractions import gcd
def checkReduce():
numerator = int(input("Numerator: "))
denominator = int(input("Denominator: "))
divisor = gcd(numerator, denominator)
if divisor <= 1:
print("You cannot reduce that fraction.")
else:
print("%i/%i" % (numerator/divisor, denominator/divisor))

```

This post has been edited by Motoma: 06 October 2010 - 12:54 PM

### #3 Brewer

• Awesome

Reputation: 178
• Posts: 1,044
• Joined: 14-June 10

## Re: Function To Reduce Basic Fractions

Posted 06 October 2010 - 01:03 PM

I have to go out to dinner so I will look more closely when I get back. However, at first glance I don't see how either of these would fix that fact that nothing is being returned.

### #4 Dogstopper

Reputation: 2816
• Posts: 10,856
• Joined: 15-July 08

## Re: Function To Reduce Basic Fractions

Posted 06 October 2010 - 01:15 PM

Motoma's code is very sound. Obviously, since it's a method that you want to return from, return instead of print. Don't expect us to hold your hand and give you ALL the correct code! Make some intuitive judgements and THEN if you need help, feel free to ask. I've seen you; you're smart enough to figure this one out...

### #5 Brewer

• Awesome

Reputation: 178
• Posts: 1,044
• Joined: 14-June 10

## Re: Function To Reduce Basic Fractions

Posted 06 October 2010 - 02:47 PM

@Motoma -- I understand what you are saying, I just had to take a look at it. There was nothing being returned because I didn't need anything returned. Switching out return for print gave me what I wanted. When I got it to print I understood why I should get it to stop at the first one I got, because there may be a higher divisor. Also, I realized that every time it checked a number, it printed that it was either reducible or that it wasn't, which isn't what I wanted.

Here is my new code, that works:

```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))
```

*Note: Didn't use GCF

### #6 Nallo

Reputation: 161
• Posts: 247
• Joined: 19-July 09

## Re: Function To Reduce Basic Fractions

Posted 06 October 2010 - 11:23 PM

There is one thing I would like to improve in your program:

You are looking for the biggest divisor of denominator and numerator. You dont want to check all possible divisors. So instead of looping over [2,3,...,smaller] and remember the last element that is a divisor it is smarter to loop over [smaller, smaller - 1, ..., 2] and break out of the loop at the fist divisor found (which incidentially is the biggest):

```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(smaller, 1 , -1):
if numerator % i == 0 and denominator % i == 0:
divisor = i
break

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

```

or written more compact:
```def checkReduce():
numerator = int(input("Numerator: "))
denominator = int(input("Denominator: "))
smaller = min(numerator, denominator)
for i in range(smaller, 1 , -1):
if numerator % i == 0 and denominator % i == 0:
print("%i / %i" % (numerator/i, denominator/i))
break
else:  #notice identitation. this else belongs to the for, not the if block
print("Can not be reduced.")

```

This post has been edited by Nallo: 06 October 2010 - 11:34 PM

### #7 Brewer

• Awesome

Reputation: 178
• Posts: 1,044
• Joined: 14-June 10

## Re: Function To Reduce Basic Fractions

Posted 07 October 2010 - 01:38 AM

I had considered doing a negative step, but I'm new to Python and I was too lazy to figure out whether or not you were aloud to do that.

Also: In print("%i / %i" % (numerator/i, denominator/i)), does the i in %i stand for integer?

This post has been edited by Jambr: 07 October 2010 - 01:45 AM

### #8 Nallo

Reputation: 161
• Posts: 247
• Joined: 19-July 09

## Re: Function To Reduce Basic Fractions

Posted 07 October 2010 - 11:49 AM

Yes, the i in %i stands for integer. (%f for decimal format float, %s for string, %e for exponential format float,...)

% is pythons string formating operator. If you put %formatoption inside a string python will take the corresponding value from the tuple behind the string and turn it into a string and substitute it. For only one value a tuple is not necessary.

Some examples:
```>>>"Hi, I am %s" %"Nallo"
"Hi, I am Nallo"

>>>a = 2
>>>"%i + %i is %i" %(a, a, 4)
"2 + 2 is 4"

>>>"%e" %42.33
'4.233000e+01'

>>>"%f" %96.768
'96.768'

>>>"%.2f" %96.768
'96.76'

>>>"%07.2f" %96.768
'0096.76'

>>>"x%8sx" %"a"
'x       ax'

>>>"%%"
'%'

>>>"%i" %3.44
'3'

>>>"%i" %"wont work"
TypeError: %d format: a number is required, not str

```

This post has been edited by Nallo: 07 October 2010 - 11:50 AM

### #9 Brewer

• Awesome

Reputation: 178
• Posts: 1,044
• Joined: 14-June 10

## Re: Function To Reduce Basic Fractions

Posted 07 October 2010 - 03:28 PM

That's pretty cool! I learned something useful today. +Rep