Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Project euler 1 understanding the python code

Tags:
  1. Jul 16, 2015 #1
    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.

    This is the code for python I found (didn't create) which I believe is correct:

    Code (Python):

    max = 1000
    result = 0

    for i in range(0,max):
        if i%3 == 0 or i%5 == 0:
            result += i

    print result
     
    233168

    Trying to see if I have this right. The max is setting the variable equal to 1000. and "for" is creating the for loop to find an integer in the range between 0 and max which is set to be 1000. What is going on with the if i%3 == 0 or i%5 == 0: I don't understand that line why the percent sign and why i? Is that saying the all integers which are either multiples or 3 or 5? why the == 0? and why the result += i?

    Thank you
    stand what the ddd
     
    Last edited by a moderator: Jul 16, 2015
  2. jcsd
  3. Jul 16, 2015 #2
    Please Google python modulo operator and python range
     
  4. Jul 16, 2015 #3

    jedishrfu

    Staff: Mentor

    I adjusted your code with code tags to display it better for others.

    The i%3==0 is true when a number is a multiple of three and the similarly for i%5==0

    so the if test allows numbers that are multiples of 3 or 5 to be added to the result.

    I leave it to you to find out what the += operator does but from my explanation you should be able to figure it out.
     
  5. Jul 16, 2015 #4
    Thanks for the replies I feel one step closer. I know the == is a comparison operator or equality operator, and = is used to assign values to variables. And % or modulo is used to extract the remainder between two positive integers. How does that work though? i%3==0 is the English equivalent to this saying that all remainders of positive integers divided by three within the constraints of 0-1000 that are not 0 will be counted, but why do the remainders mean anything? aren't we looking for the numbers which are multiples of 3 and 5 and not their remainders? I am sure I am missing the point of something here.
     
  6. Jul 16, 2015 #5

    Mark44

    Staff: Mentor

    The expression i % 3 is the remainder of the division of i by 3. If i is a multiple of 3, the remainder will be 0.

    Any number that is a multiple of 3 (i.e., 0, 3, 6, 9, ...) will have a remainder of 0 when it is divided by 3. Similarly, any number that is a multiple of 5 will have a remainder of 0 when that number is divided by 5. So 5 % 5 == 0, 10 % 5 == 0, 15 % 5 == 0, and so on. In contrast, 6 % 5 == 1 and 7 % 5 == 2.
     
  7. Jul 16, 2015 #6
    I see, anything that leaves the remainder zero would therefor have to be divisible by 3 or 5. Where is it understood in the program to the computer that it's the number's value and not just counting it consecutively each time like a ticker. Where is it defined to count, say 3,6,9 and not just 1,1,1 for the number of times the division is correct.
     
  8. Jul 16, 2015 #7

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    result += i adds the number i. So it is accumulating the sum of all the numbers that pass the 'if' test. It is not just incrementing a counter by 1.
     
  9. Jul 16, 2015 #8
    So each and every time the 'if' test is proven true then a new number from either the 3 or 5 side is moved to the next line which instructions are to take the "result" and add it. Odd question here but does it do this all instantaneously? or might it compute each number that passes it add it to the previous.
     
  10. Jul 16, 2015 #9
    I like how you call it a test. Is this how programming should be thought? such as else, when, for loops and operations.
     
  11. Jul 17, 2015 #10

    NascentOxygen

    User Avatar

    Staff: Mentor

    Think of a program as a sequence of operations, executed one statement at a time in a methodical planned sequence. The next statement to be executed can be determined by the result of a "test" of a value, or whether some other condition is found to be "true" or "false". So the data itself can control the direction in which the execution proceeds.
     
  12. Jul 17, 2015 #11

    jtbell

    User Avatar

    Staff: Mentor

    The operation of a computer program is usually sequential, with modifications according to specified rules for things like if-statements, for-statements, and other constructions. A common analogy is a recipe for cooking something, with a sequence of steps that must be performed in a certain order. Some compilers for some languages can take advantage of multiple processors in a computer by assigning groups of instructions to different processors and executing them simultaneously to speed them up, if the program logic allows it.
     
  13. Jul 17, 2015 #12
    Thanks guys! I understood. One last thing though. Why is the result=0 necessary in the beginning?
     
  14. Jul 17, 2015 #13

    Mark44

    Staff: Mentor

    The line of code result += i is the equivalent of writing result = result + i. Because the value being stored in result in the assignment depends on the current value of result, this variable has to be initialized to zero. Whenever a variable is being used to accumulate a result, as in this example, it has to have a starting value.
     
  15. Jul 18, 2015 #14

    jtbell

    User Avatar

    Staff: Mentor

    In many programming languages, if you don't store some value explicitly in a variable before using it in calculations, it contains whatever bit pattern happens to occupy that memory space when the program begins execution. It's not automatically initialized to zero.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Project euler 1 understanding the python code
  1. Project in Python (Replies: 2)

  2. Euler's Method in Python (Replies: 13)

Loading...