-1

I am just getting into coding and have been practicing on some basic math problems from Project Euler. I have simply been writing these programs in Jupyter notebooks. I am only on question 5, a very basic prompt, and a very basic answer, and for some reason it has become progressively harder to actually run the code and get an output. Here is the code I am trying to run:

nums = []
for n in range(1, 30000):
    i = 1
    while i < 11:
        quo = n % i
        if quo != 0: break
        if quo == 0: i =+ 1
        if i == 10 and quo == 0: nums.append(n)
print(nums)

When I run it, the program simply never stops running and so never gives me an output. I don't think it has anything to do with the code, because I ran it before and it worked. I also tried running it in Spyder but had the same problem. Is this a problem with my computer, or with the way I am running the code?

For context, here is the Euler prompt I was working on:

"2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"

The code above is just trying to reproduce the example results (get 2520 back).

izhang05
  • 744
  • 2
  • 11
  • 26

1 Answers1

2

I think that program never worked, but with a tiny change it might:

 if quo == 0: i += 1

Edit: Actually, to solve such issues, either use a debugger or put print statements into the code. Here: observe the variables i, n and quo. Try to understand each step upfront.

So I did that and saw that though quo was zero, i didn't increment. That fixes your eye-balls on that i=+1 bug right away.

Dr. V
  • 1,747
  • 1
  • 11
  • 14