Archive for August, 2010

Project Euler Problem #243!

August 16th, 2010

Problem #243 says:

A positive fraction whose numerator is less than its denominator is called a proper fraction.
For any denominator, d, there will be d−1 proper fractions; for example, with d = 12:
1/12 , 2/12 , 3/12 , 4/12 , 5/12, 6/12 , 7/12 , 8/12 , 9/12 , 10/12 , 11/12 .

We shall call a fraction that cannot be cancelled down a resilient fraction.
Furthermore we shall define the resilience of a denominator, R(d), to be the ratio of its proper fractions that are resilient; for example, R(12) = 4/11 .
In fact, d = 12 is the smallest denominator having a resilience R(d) < 4/10 .

Find the smallest denominator d, having a resilience R(d) < 15499/94744 .


This is a problem where the answer came out of know where, I stared at it for a while then realized I have to minimize the euler totient. The simplest way to do that was to create a test number out of multiplying consecutive primes and going from there. So what the solution does is start with the first 5 prime numbers and multiplies them together then it steps through the a sequence from 1 to the last prime found. if it finds the number then it stops, else if goes till it hits a prime number bigger than the last one, multiplies the starting number by that prime and then starts back at 1 till it hits another prime or solves the problem.

Solution in python because that’s all I had when I came up with the answer, runs in well under a second.

def factor(n):
        if n == 1: return [1]
        i = 2
        limit = n**0.5
        while i <= limit:
                if n % i == 0:
                        ret = factor(n/i)
                        ret.append(i)
                        return ret
                i += 1
        return [n]  

def uniqify(seq):
        return list(set(seq))

def phi(x):
    t = x
    for k in uniqify(factor(x)):
        t -= t // k
    return t

def resilience(x):
    return phi(x) / (x-1.0)

lastprime = 11
base = 2*3*5*7*11.0
multiplier = 1.0
print "Starting value",  base

while (resilience(base * multiplier) > (15499.0/94744.0)):
    multiplier = multiplier +1.0
    if (multiplier > lastprime):
        if (len(factor(multiplier)) == 1):
            lastprime = multiplier
            base = base * multiplier
            print "New starting value for search",  base
            multiplier = 1.0

print "Answer :: ", base * multiplier

Tags: ,
Posted in Project Euler | Comments (0)