Skip to content

Instantly share code, notes, and snippets.

@struts2spring
Created November 20, 2013 03:38
Show Gist options
  • Save struts2spring/7557290 to your computer and use it in GitHub Desktop.
Save struts2spring/7557290 to your computer and use it in GitHub Desktop.
Convert the following English description into code.
Question:
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.
2.1. Add the first number in numbers to the end of results.
2.2. Remove every number in numbers that is evenly divisible by (has no remainder when divided by) the number that you had just added to results.
How long is results?
To test your code, when n is instead 100, the length of results is 25.
n = 100
numbers = range(2, n)
results = []
while numbers:
i = numbers[0]
results.append(i)
for number in numbers:
if number % i == 0:
numbers.remove(number)
print numbers
print len(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment