views:

210

answers:

4

So I'm learning python so I'm going through some project euler problems. And I'm not sure if this is a python problem I'm having, or just me being retarded, but I seem to be getting the wrong answer for problem 53. Here's a link to the problem http://projecteuler.net/index.php?section=problems&id=53

and this is my code:


from math import factorial

def ncr(n,r):
    return (factorial(n)/(factorial(r)*factorial(n-r)))

i = 0

for x in range(1,100):
    for y in range(0,x):
        if(ncr(x,y) > 1000000):
            i=i+1

print i

I'm getting 3982 which is apparently the wrong answer. Am I just retarded? Or is something wrong that I'm doing that's specific to python?

+5  A: 

range( a, b) does not include b.

katrielalex
You were the fasted, although you also had the shortest response =P
Falmarri
We call that pythonic
Dominic Bou-Samra
+4  A: 

I think your code is correct, however, you should iterate x to 100 inclusive, so you should use

for x in range(1,101):

Hope that helps. Euler rocks!

Lex
+3  A: 

Note that n is greater than or equal to 1 AND smaller than or equal to 100. Currently your n goes from 1 to 99. You can use xrange too.

from math import factorial

def ncr(n,r):
    return (factorial(n)/(factorial(r)*factorial(n-r)))

i = 0

for x in range(1,101):
    for y in range(1,x+1):
        if(ncr(x,y) > 1000000):
            i=i+1

print i
vtorhonen
maybe mention that range returns an iterator in Python 3
hop
`xrange(101)` includes `0`
katrielalex
@hop: good point. @katrielalex: Ah, right. Better fix it then to range(), even though in this case it doesn't matter as ncr(0,1) is smaller than 1000000.
vtorhonen
`range(101)` also includes `0`. The right way is to do `(x)range(1, 101)`.
katrielalex
+1  A: 

If you are beginner I use this opportunity, considering project Euler's nature, to give coding alternative, which is self-contained and demonstrates lookup table approach to speed up recursive functions and saving the answers to dictionary and using the len as count.

Hope the 4075 is the right answer!

from __future__ import division
factorials={}

def factorial(n):
    """ factorial from lookup table ready or generate it to there """
    if n not in factorials:
        factorials[n]=1 if  n==0 else n*factorial(n-1)
    return factorials[n] 

def ncr(n,r):
    return (factorial(n)/(factorial(r)*factorial(n-r)))

bigones= [(x,y) for x in range(1,1+100) for y in range(x) if ncr(x,y) > 1000000 ]

print len(bigones)
Tony Veijalainen
Yeah I know I'm not doing it the most efficient way. But I know plenty of other languages and I'm just teaching myself python for a project I'm going to be working on at work. +1 for efficiency though
Falmarri
Hope you got some pythonic influence from the list comprehension and if else value doing factorial. By the way the factorials are exact not floats. It is not space efficient to keep all answers, but it is nice when you want to continue to experiment further. It also keeps the code cleaner. My code has 10 lines ignoring empty lines and the docstring, but the first import is just for safety and not actually needed. 1/3=0 in python 2.6/2.7 otherwise. Your code had 9 rows and is nice basic coding. First make it work, then optimize.
Tony Veijalainen