Submit | All submissions | Best solutions | Back to list |
ALICESIE - Alice Sieve |
Alice has recently learned to use the Sieve of Eratosthenes, an ancient algorithm for finding all prime numbers up to any given limit. As expected, she was really impressed by it's simplicity and elegancy.
Now, she has decided to design her own sieve method: The Sieve of Alice, formally defined by the following procedure, which determines the Sieve of Alice up to a given limit N.
- Create a list of consecutive integers from N to 2 (N, N-1, N-2, ..., 3, 2). All of those N-1numbers are initially unmarked.
- Initially, let P equal N, and leave this number unmarked.
- Mark all the proper divisors of P (i.e. P remains unmarked).
- Find the largest unmarked number from 2 to P – 1, and now let P equal this number.
- If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3.
Unfortunately, Alice has not found an useful application for it's Sieve. But she still wants to know, for a given limit N, how many integers will remain unmarked.
Input
The first line contains an integer T, the number of test cases (1 ≤ T ≤ 10^4) . Each of the next T lines contains an integer N (2 ≤ N ≤ 10^6).
Output
Output T lines, one for each test case, containing the required answer.
Example
Input: 3 2 3 5 Output: 1 2 3
Added by: | Paulo Costa |
Date: | 2012-02-06 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | UNI |
hide comments
|
|||||||
2015-10-27 16:07:07 moovon
It must have been tougher for the problem setter to set the problem than for me to write the code :P |
|||||||
2015-09-07 07:25:52 NIKHIL KUMAR SINGH
Sweet one :) |
|||||||
2015-08-30 00:32:18
3 lines in python after including an import statement, 36 characters in ruby. If the great Eratosthenes was alive, he would die laughing.. :D EDIT: I think this can be a good codegolf problem. Last edit: 2015-10-07 20:47:02 |
|||||||
2015-07-17 21:53:04 xxbloodysantaxx
I was thinking that Answer(Prime)=Answer(Prime-1)+1 ! And then for Composite numbers I couldn't figure it out!! Nice mathematics :) Healthy for mind :D |
|||||||
2015-06-01 21:30:26 Vaporeon
So Alice,you were no where near Eratosthenes.. :P :v |