title: What is the best algorithm to solve this puzzle?
body: <p>So I came across this question:</p>
  
  <p>How many numbers are there from 1 to 1000 which are not divisible by the digits 2, 3 and 5?</p>
  
  <p>It seems pretty easy at first, so I wrote a quick python program to solve it:</p>
  
  <pre><code>count = 0
  for number in range(1,1000):
      if number % 2 != 0 and number % 3 != 0 and number % 5 != 0:
          count += 1
  print(count)
  </code></pre>
  
  <p>I got the correct answer (266), but I thought that doing it that way was a lot of typing if I ever wanted to check more than just 3 values. I also wanted to do a mathematical solution so I came across this:</p>
  
  <p><code>1000 - ((1000/2 +1000/3 +1000/5) -(1000/2x3 +1000/2x5 + 1000/3x5)+ (1000/2x3x5)) = 1000-((500+333+200) - (166 +100 + 66) + 33) = 1000- 734 = 266</code></p>
  
  <p>I thought it was a good approach so I implemented it in code:</p>
  
  <pre><code>def foo(ln = 1000), numbers = [2,3,5]:
      div = 0
      muldiv = 0
      totdiv = 1
  
  
      for n in numbers:
          div += ln/n
      for i in numbers:
          for n in range(numbers.index(i)+1, len(numbers)):
              muldiv += ln/(i * numbers[n])
  
      for n in numbers:
          totdiv *= n
  
      answer = ln - (div - muldiv + ln/totdiv)
      print("answer is ", math.floor(answer))
  </code></pre>
  
  <p>Now I am pretty sure I messed up somewhere in my second function because it doesn't seem to work for more numbers. For example, if I were to try to find </p>
  
  <p>How many numbers are there from 1 to 1000 which are not divisible by the digits 2, 3, 5 and 7?</p>
  
  <p>the first method returns <code>228</code> and <code>foo(numbers = [2,3,5,7])</code> returns <code>300</code>... I'm pretty sure <code>228</code> is the correct answer since one more number would mean that there are LESS factors instead of more, but where did I go wrong? and is there a better way to solve this problem?</p>