Page 4 of 4 FirstFirst ... 234
Results 31 to 36 of 36

Thread: Project Euler Problem 1 is driving me insane

  1. #31
    Join Date
    Aug 2007
    Beans
    30

    Re: Project Euler Problem 1 is driving me insane

    That should work, but again, is very slow since you are iterating over a list. Plus it won't work in general ([x]range has an upper limit, 10**20 won't work, for example).
    Last edited by generalguy; August 20th, 2008 at 09:33 PM.

  2. #32
    Join Date
    Jul 2006
    Location
    somewhere :)
    Beans
    535
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Project Euler Problem 1 is driving me insane

    Quote Originally Posted by generalguy View Post
    That should work, but again, is very slow since you are iterating over a list. Plus it won't work in general ([x]range has an upper limit, 10**20 won't work, for example).
    i wonder how slow that is. one hopes that set(args) just calls a malloc(num(args) * sizeof *ptr).

    oh hang on. numbers in python are objects to. oh, i see the problem. it may well have a clever hashing function to make checking for the presence of an object in a set a little faster, but we're still looking at O(n) for every insertion here. nasty.

    and this is, as you pointed out, horrible:

    Code:
    >>> set(range(1, 10**10))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: range() result has too many items
    there are 10 types of people in the world: those that understand binary and i don't know who the other F are.

  3. #33
    Join Date
    Jul 2008
    Location
    England
    Beans
    6
    Distro
    Ubuntu 8.04 Hardy Heron

    Smile Re: Project Euler Problem 1 is driving me insane

    I have been playing around with python 3. This is what I got for Project Euler Problem 1. Lster has been teaching me python.

    PHP Code:
    0
    for i in range(01000):
        if 
    i%3==or i%5==0:
            
    i

    print(t

  4. #34
    Join Date
    Feb 2007
    Location
    The hills of appalachia
    Beans
    966

    Re: Project Euler Problem 1 is driving me insane

    Of course the most efficient solution is simply

    Code:
    print 3*333*167 + 5*199*100 - 15*67*33
    In case you are wonder how I came up with this consider the following somewhat more verbose yet equivalent version (in python):

    Code:
    def triangle(n):
        return n*(n + 1)/2
        
    def euler1(m):
        m = m - 1
        return 3*triangle(m/3) + 5*triangle(m/5) - 15*triangle(m/15)
        
    print euler1(1000)
    If this is still not clear consider the fact that the sum of 1 to n is really the nth triangle number, which is what my triangle function above calculates. Now the sum of multiples of say 3 from 3 to 3*n is simply 3 times the nth triangle number by the distributive principle .

    Now in the problem Find the sum of all the multiples of 3 or 5 below 1000, we find the sum of the multiples of 3 add that to the sum of the multiples of 5. But since 3 and 5 both divide 15 we have added this twice so we then subtract the sum of the multiples of 15, hence

    Code:
    3*triangle(m/3) + 5*triangle(m/5) - 15*triangle(m/15)
    Which btw is really the equialvent to my first statement above:

    Code:
    3*333*167 + 5*199*100 - 15*67*33
    Last edited by y-lee; August 21st, 2008 at 03:51 PM.
    If you think you're free, there's no escape possible. Ram Dass

  5. #35
    Join Date
    Aug 2007
    Beans
    30

    Re: Project Euler Problem 1 is driving me insane

    Yep. That's all. If you want to generalize it to a set of divisors D, then you must use the inclusion-exclusion principle.

    PHP Code:
    from itertools import izip
    import operator 
    as op

    def sdiv
    (kn):
        
    max_div k
        
    return (((max_div)*(max_div+1)) / 2) * k

    def powerset
    (s):
        
    dict(izip
                
    ((1<<for i in xrange(len(s))),
                (
    set([e]) for e in s)
                ))
        
    subset set()
        for 
    i in xrange(11<<len(s)):
            
    subset subset d[& -i]
            yield 
    subset

    def euler1
    (sd,n):
        
    pset powerset(sd)
        
    sum 0
        
    for ss in pset:
            
    sum_divs sdiv(reduce(op.mulss),n-1)
            if 
    len(ss) % == 0:
                
    sum -= sum_divs
            
    else:
                
    sum += sum_divs
        
    return sum
            

    pset 
    powerset([3,5])
    facts = ( ((-1) ** ((len(ss) % 2) + 1)) * sdiv(reduce(op.mulss),1000-1) for ss in pset )
    print 
    sum(facts)
    print 
    euler1([3,5],1000
    The powerset generator is not mine ( i modifed it a little; it also does not return the empty set); it works because you can use binary numbers to model inclusion of elements in a powerset.

  6. #36
    Join Date
    Feb 2007
    Location
    The hills of appalachia
    Beans
    966

    Re: Project Euler Problem 1 is driving me insane

    Quote Originally Posted by generalguy View Post
    Yep. That's all. If you want to generalize it to a set of divisors D, then you must use the inclusion-exclusion principle.

    . . .

    The powerset generator is not mine ( i modifed it a little; it also does not return the empty set); it works because you can use binary numbers to model inclusion of elements in a powerset.
    Very Impressive code generalguy
    If you think you're free, there's no escape possible. Ram Dass

Page 4 of 4 FirstFirst ... 234

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •