Created
November 20, 2013 03:38
-
-
Save struts2spring/7557290 to your computer and use it in GitHub Desktop.
Convert the following English description into code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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