-1

hi I am a newbie of python. I was trying to find smallest positive number that is evenly divisible by all of the numbers from 1 to 20, but I was keep getting 20 which obviously is wrong; I don't know why is that.

def even_divisible():
  x=1
  for i in range(1,21):
    if x%i!=0:
      x+=1
  print x  
even_divisible()

anybody knows why ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

Your code has a significant logical flaw; when the for loop ends (after 20 loops), the current value of x is printed, without checking whether it is divisible by the appropriate numbers. You don't restart the for loop from 1 each time you increment x.

You need an outer loop to run until an appropriate x is found; a while loop, as we don't know how long it will run for. To minimally fix your code:

def even_divisible():
    x=1
    while True: # outer loop 
        for i in range(1,21): # inner loop
            if x % i != 0:
                x+=1
                break # break for loop
        else: # for loop ran to end without break
            break # break while loop
    print x  

You can simplify with any or all for the inner loop. There are a few mathematical simplifications, too; given that we know that x % 20 == 0, we only need look at multiples of 20, and since all integers are integer multiples of 1 we can start from 2:

def evenly_divisible(n):
    x = n
    while True: # outer loop over multiples of n
        if all(x % i == 0 for i in range(2, n+1)): # inner loop over 2-n
            return x
        x += n
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Keep in mind that the Project Euler challenges (which I'm certain the OP are trying here) are designed in such a way that the brute force method is a poor choice of method. If you're just learning Python it will work, but once you get your bearings with the language there are far more efficient methods. – TheSoundDefense Aug 28 '14 at 16:16
  • @TheSoundDefense that's true, and there are better implementations [elsewhere on SO](http://stackoverflow.com/a/8416789/3001761) (translation to Python left as an exercise for the reader). – jonrsharpe Aug 28 '14 at 16:19
  • thanks for you reply. but seemingly my code can check whether it is divisible. Starting from x=1, if x is not evenly divisible by i in range(1,20), x=x+1. I think this logic is correct though this method is slow, but I always got 20. Can u explain why ? – Chenxiong Yi Aug 28 '14 at 16:21
  • 1
    @ChenxiongYi yes, you **don't restart from 1** each time. Put `print i, x` inside your `for` loop and see what happens. – jonrsharpe Aug 28 '14 at 16:22
  • @jonrsharpe you're right. Everytime x is added by 1, x+1 is not divided by every number in range(1,21), but only the number that x cannot divide evenly. but I cannot figure out why. – Chenxiong Yi Aug 28 '14 at 16:39
  • @ChenxiongYi you should add a `while` loop to help you try every number, and a `break` to stop the inner `for` loop if `x` turns out to be a bad choice. – TheSoundDefense Aug 28 '14 at 16:40
  • @ChenxiongYi I have added this to my answer. Note that your code will take a while to run, though. – jonrsharpe Aug 28 '14 at 16:44