Convert the following English description into code.
- Initialize
nto be 1000. Initializenumbersto be a list of numbers from 2 to n, but not includingn. - With
resultsstarting as the empty list, repeat the following as long asnumberscontains any numbers.- Add the first number in
numbersto the end ofresults. - Remove every number in
numbersthat is evenly divisible by (has no remainder when divided by) the number that you had just added toresults.
- Add the first number in
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
No comments:
Post a Comment