Wednesday, November 20, 2013

Convert English description into code

Convert the following English description into code.
  1. Initialize n to be 1000. Initialize numbers to be a list of numbers from 2 to n, but not including n.
  2. With results starting as the empty list, repeat the following as long as numbers contains any numbers.
    • Add the first number in numbers to the end of results.
    • Remove every number in numbers that is evenly divisible by (has no remainder when divided by) the number that you had just added toresults.
How long is results?
To test your code, when n is instead 100, the length of results is 25.
CODE :
n = 1000
numbers = range(2, n)
results = []
 while numbers != []:
    results.append(numbers[0])
    numbers = [n for n in numbers if n % numbers[0] != 0]
 print len(results)
Output :
168