Project Euler Problem #53!

March 30th, 2009
by Serinox

Problem #53 says:

(slightly edited version)

There are exactly ten ways of selecting three from five, 12345:

123, 124, 125, 134, 135, 145, 234, 235, 245, and 345

It is not until n = 23, that a value exceeds one-million.

How many, not necessarily distinct, values for 1 ≤ n ≤ 100, are greater than one-million?

[math]C(n,r) = (N!) / (R!(n-r)!)[/math]

That’s the formula we have to implement given to us in the problem statement from projecteuler.net. Then we just have to loop through all of the possible combination’s making sure that r is never greater that n. Very simple to do however this problem generated very high numbers when calculation n! therefor the solution is provided with python so we can take advantage of the arbitrary length math it supports.

So here it is in python, takes a few seconds to run.

def Exclamation(i):
    count = 0
    Answer=1
    b=i
    while(count <= i):
        if (b <> 0):
            Answer= Answer * b
        count = count +1
        b= b-1
    return Answer

def Select(n,r):
    nex = Exclamation(n)
    rex = Exclamation(r)
    nrex = Exclamation (n-r)
    return (nex)/(rex*nrex)

NCount = 2
RCount = 1
NCRCount = 0
while (NCount <= 100):
    while (RCount <= NCount):
        if(Select(NCount,RCount) > 1000000):
            NCRCount = NCRCount+1
        RCount = RCount + 1
    RCount = 1
    NCount = NCount + 1

print NCRCount

Posted in Project Euler | Comments (0)

No comments yet

Leave a Reply

You must be logged in to post a comment.