Project euler 1 understanding the python code

In summary: That can lead to results that are unpredictable or at least hard to figure out. So it's a good habit to get into, to initialize any variables that you use in calculations. The Python language has a few shortcuts that let you be a little less explicit, but the main idea is the same.
  • #1
Niaboc67
249
3
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:

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:
Technology news on Phys.org
  • #2
Please Google python modulo operator and python range
 
  • #3
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.
 
  • #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.
 
  • #5
Niaboc67 said:
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.
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.
 
  • #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.
 
  • #7
Niaboc67 said:
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.
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.
 
  • #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.
 
  • #9
I like how you call it a test. Is this how programming should be thought? such as else, when, for loops and operations.
 
  • #10
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.
 
  • #11
Niaboc67 said:
Odd question here but does it do this all instantaneously? or might it compute each number that passes it add it to the previous.

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.
 
  • #12
Thanks guys! I understood. One last thing though. Why is the result=0 necessary in the beginning?
 
  • #13
Niaboc67 said:
Thanks guys! I understood. One last thing though. Why is the result=0 necessary in the beginning?
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.
 
  • #14
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.
 

1. What is Project Euler 1 and why is it important?

Project Euler 1 is a coding challenge that involves solving a mathematical problem using the Python programming language. It is important because it helps improve problem-solving skills and strengthens knowledge of programming concepts.

2. How does the Python code for Project Euler 1 work?

The Python code for Project Euler 1 works by using a for loop to iterate through a range of numbers and check if each number is a multiple of 3 or 5. If it is, the number is added to a running total. The final total is then printed as the solution to the problem.

3. Can I modify the Python code for Project Euler 1 to solve a different problem?

Yes, the code can be modified to solve a different problem by changing the conditions within the for loop. However, it is important to understand the logic behind the original code before attempting to make modifications.

4. How do I know if my solution to Project Euler 1 is correct?

You can check the accuracy of your solution by comparing it to the expected result provided by Project Euler. It is also helpful to have someone else review your code for any errors or potential improvements.

5. Are there any resources for understanding the Python code for Project Euler 1?

Yes, there are many online resources available such as tutorials, forums, and video explanations that can help you understand the Python code used in Project Euler 1. It can also be helpful to practice with simpler coding challenges before attempting Project Euler 1.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
875
  • Programming and Computer Science
Replies
2
Views
759
  • Programming and Computer Science
Replies
5
Views
2K
Replies
1
Views
1K
Replies
5
Views
877
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
7
Views
396
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
Back
Top