Shriphani ‘PSP’ Palakodety

Weblog of an Aspiring Computer Scientist

Shriphani ‘PSP’ Palakodety header image 2

Curious Fractions

November 14th, 2008 · 7 Comments · Mathematics, python

While suffering from conjuctivitis, fever, a sore throat and what seems to be a case of swollen tonsils determined to make nutrition a pain for the next week or two, I decided to solve a problem on Project Euler. The question I picked was Problem 33: Find all fractions with the given unorthodox cancelling method (just click the fscking link for heaven’s sake!).

I thought this problem can be solved by beginning with a base fraction and adding a numbers in the tens place to both the numerator and the denominator, then adding a number in the units place to the numerator and the denominator, adding to the units place in the numerator and the tens in the denominator and the other way round.

The code needed (in Python2.6 – just love the Fractions module there):

#!/usr/bin/env python
#Author: Shriphani Palakodety
#Blog: http://shriphani.com/blog
#Mail: shriphani@shriphani.com

frac_list = []

from fractions import Fraction

prod = 1

def findLeft(num, den):
    ‘Given a denominator, numerator pair, return the double digit possibilities by appending chars to the left’
    global prod
    for i in xrange(1, 10):
        if i == num or i == den:
            continue
        else:
            if Fraction(10*i+num, 10*i+den)==Fraction(num, den):
                frac_list.append(Fraction(num, den))
                print num,den

def findRight(num, den):
    global prod
    for i in xrange(1, 10):
        if i == num or i == den:
            continue
        else:
            if Fraction(10*num+i, 10*den+i)==Fraction(num, den):
                print num, den
                frac_list.append(Fraction(num, den))

def findAlt(num, den):
    global prod
    for i in xrange(1, 10):
        if i == num or i == den:
            continue

        elif Fraction(10*num+i, 10*i+den)==Fraction(num, den):
            frac_list.append(Fraction(num, den))

        elif Fraction(10*i+num, 10*den+i)==Fraction(num, den):
            frac_list.append(Fraction(num, den))

for i in xrange(1, 10):
    for j in xrange(1, 10):
        if i==j:
            continue
        else:
            findLeft(i, j)
            findRight(i, j)
            findAlt(i, j)

prod = 1
for i in xrange(len(frac_list)):
    if frac_list[i] < 1/1:
        prod *= frac_list[i]

print prod

BTW, it is funny how total nutjobs with no clue about probes, payloads, science or physics take up the job of reporting the Chandrayaan’s progress.

Peace…..

Tags:

7 Comments so far ↓

  • André Roberge

    While it’s nice to see people interested in project euler, I believe it is against that site’s philosophy to post solutions.

  • Shriphani

    >> “against that site’s philosophy to post solutions.”

    Can you please show me the link to their policy? If that is the case, then I should have violated their policy quite a lot of times (and so should a lot others) on this site.

  • André Roberge

    If you look at the bottom of a page with problems being listed, you will find the following statement:

    “NOTE: Please do not contact Project Euler if you are unable to solve a particular problem. If you can’t solve it, then you can’t solve it!”

    This statement, plus the fact that the thread for a particular problem is locked until you solve it, is, for me, an indication that solutions should not be posted.

    (I seem to recall having read a stronger statement somewhere else but can’t find it right now…)

  • Steve Holden

    It certainly seems unfair not to even list this entry as a “spoiler”. Project Euler is meant to be solved by hard work, not by the use of Google … the Project Euler home page includes the following:

    I solved it by using a search engine, does that matter?

    Making use of the internet to research a problem is to be encouraged as there could be hidden treasures of mathematics to be discovered beneath the surface of many of these problems. However, there is a fine line between researching ideas and using the answer you found on another website. If you photocopy a crossword solution then what have you achieved?

    Sorry to hear about your illnesses. Hope you’re feeling better.

  • Matt

    Thanks for posting your soluton. I am up to this question and have been stumped. Primarilly because I dont understand the problem. I write my solutions in another language so coping is not an issue, I am just after clues. I post all my solutions too

  • Adi

    Actually, I believe the problem can be solved even without programming. One of the curious fractions is revealed by project Euler and somebody who has read something about “funny math” knows the others.
    It may be one of the few Euler problems that can be solved in one’s head.

Leave a Comment