Submit | All submissions | Best solutions | Back to list |
PRIMEN - Sieve of Eratosthenes |
Heard of a procedure called sieve of Eratosthenes? Well, implement this:
- Fill an array num[n] (where 0 <= n <= 1000) with numbers from 1 to n.
- Starting with the second entry in the array, set all its multiples to zero.
- Proceed to the next non-zero element and set all its multiples to zero.
- Repeat step 3 until you have set up the multiples of all the non-zero elements to zero.
- At the conclusion of step 4, all the non-zero entries left in the array would be ... (obviously) prime numbers, so print out these numbers.
Input
First line consists of number of test cases t (0 <= t <= 100). The next lines refers to the values of n (0 <= n <= 1000).
Output
The prime numbers up to n with output of each test case separated by an extra line.
Example
Input: 2 5 10 Output: 1 2 3 5 1 2 3 5 7
Added by: | kousik |
Date: | 2013-09-13 |
Time limit: | 1s-5s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | ASM32-GCC MAWK BC C-CLANG C NCSHARP C++ 4.3.2 CPP CPP14 CPP14-CLANG COBOL COFFEE D-CLANG D-DMD DART ELIXIR FANTOM FORTH GOSU GRV JAVA JS-MONKEY JULIA KTLN NIM OBJC OBJC-CLANG OCT PICO PROLOG PYTHON PYPY PYPY3 PYTHON3 PY_NBC R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET |
hide comments