-1

I'v been studying Python for less than a month and am currently working on Project Euler.

So ironically I'm stuck on the first question. Check out my app road below and I would love some input as to where I have messed up. Or using the wrong logic.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

My Logic/Code Flow

sum = multiple_three + multiple_five
multiple_three = []
multiple_five = []

def multiple_three(bignumber):
    for number in range(bignumber):
        if number % 3 == 0:
            multiple_three.append(number)

def multiple_five(bignumber):
    for number in range(bignumber):
        if number % 5 == 0:
            multiple_five.append(number)

bignumber = 1000
multiple_five(bignumber)
multiple_three(bignumber)

print multiple_three
print multiple_five
vaultah
  • 44,105
  • 12
  • 114
  • 143
goldkin
  • 31
  • 8
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Morgan Thrapp Jul 09 '15 at 20:13
  • 1
    so what exactly is the problem? – letsc Jul 09 '15 at 20:15
  • wow ._. so a variable and function name cannot be the same. Got it! Thanks Morgan! Im just trying to get the list filled out right now. Then I want to tackle adding them together and print "Sum". Is my overall logic of attacking this problem correct or am I working too hard? – goldkin Jul 09 '15 at 20:15
  • Remember that some numbers (like 15) will be both multiples of 3 and 5. – DSM Jul 09 '15 at 20:18
  • There are many error like calling a function before it is defined and calling a function without `()` and not returning the value please do a lot more learning – The6thSense Jul 09 '15 at 20:18
  • Find the sum of `1`, `2`, `3`, ... `n`, in terms of `n` and see how it relates to `3`, `6`, `9` and `5`, `10`, `15`, etc. – Peter Wood Jul 09 '15 at 20:23

3 Answers3

1

This is what worked for me. Let me know if it worked for you too.

def multiples_of_3_or_5():
    for number in xrange(1000):
        if not number % 3 or not number % 5:
            yield number

print sum(multiples_of_3_or_5())
Destry
  • 228
  • 1
  • 12
  • Thanks for that haha don't thought I got all of it! – Destry Jul 09 '15 at 20:19
  • Thanks for this Destry. Quick questions.. 1) Why do you use xrange and not range? 2) why "if not number % 3 or not number % 5:" shouldn't it be only "if number % 3 (if number is divisible by 3)? – goldkin Jul 09 '15 at 20:22
  • No problem, the xrange is described as so. Range creates a list, so if you do range(1, 10000000) it creates a list in memory with 10000000 elements. xrange is a generator, so it is a sequence object is a that evaluates lazily. found here https://stackoverflow.com/questions/94935/what-is-the-difference-between-range-and-xrange-functions-in-python-2-x. And the reason its like that is because it is faster to do it that way for the computer since the computer doesnt care which way it gets done. – Destry Jul 09 '15 at 20:27
  • Why did you use if not instead of just if? – goldkin Jul 09 '15 at 21:01
  • try looking [here](https://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python). This explains it better than I can – Destry Jul 09 '15 at 22:14
1

Let's see.

We can start by using the fact that range has a step argument. So something like range(0,10,3) would get you 0,3,6,9.

So, for starters, we have

sum(range(0,1000,3)) + sum(range(0,1000,5))

But wait! Numbers divisible by 15 are divisible by both 5 and 3! We're counting them twice! Uh, uh, let's subtract them out

sum(range(0,1000,3)) + sum(range(0,1000,5)) - sum(range(0,1000,15))

Hum. I think we're still missing something here. Is there an easier way to add together an arithmetic progression? Could have sworn that there was...

something like (a_0 + a_n)*n/2, where n is the number of terms, maybe...

def sum_of_arithmetic_progression(start,stop,step):
    n = (stop-start)//step #floor division :P
    end = start + step*n
    return (start + end)*n/2.0

sum_of_arithmetic_progression(0,1000,3)+sum_of_arithmetic_progression(0,1000,5)-sum_of_arithmetic_progression(0,1000,15)
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
  • Do you automatically think of this amazing logic or does it take time and practice? This is simply impressive! Was my approach bad or just too much work? Btw thanks for this i'v read it like 10 times and I'm trying to understand how you thought of (a_0 +a_n)*n/2 part – goldkin Jul 09 '15 at 20:34
  • 1
    uh, it involved googling "summation arithmetic progression" :P But you can prove it yourself trivially with some time and some graph paper :P – NightShadeQueen Jul 09 '15 at 20:35
  • Your approach is naive but not unexpected for someone who's just started learning python :P There's a lot of [little hidden features](http://stackoverflow.com/questions/101268/hidden-features-of-python) that'll you'll pick up over time as well that makes things a lot easier. – NightShadeQueen Jul 09 '15 at 20:37
  • Was testing this out during lunch and the relation wit 15 became very evident. was about to mention it. Great work , +1 – letsc Jul 09 '15 at 21:12
  • @goldkin See the [story about Gauss as a child, calculating the sum of the numbers 1 to 100 in a few seconds](http://nrich.maths.org/2478) (this is different to summing the numbers, which is a laborious and time consuming and is arithmetic, rather than mathematics). – Peter Wood Jul 10 '15 at 20:49
0

You are not printing sum. But apart from that, you are counting numbers like 15 twice, i.e as a multiple of 3 as well as 5. A quick solution would be to

 print(sum(set(multiple_three+multiple_five)))

Here multiple_three+multiple_five gives a concatenated list. set identifies the unique elements, and sum will output the sum. Better would be to use "or"operator and append the numbers in a single loop as @Destry Amiott has suggested.

Akshat Harit
  • 814
  • 1
  • 7
  • 13