In this problem you will be given a range 'n', and you are supposed to find all…
- User
In this problem you will be given a range 'n', and you are supposed to find all composite numbers within that range. Make your program efficient as time limit of the program is set to 1 second. Choose an efficient algorithm to find all the composite numbers within the range 0 to n. Input
n - a single positive Integer.
Output
Space separated composite-numbers within the range of 0 and n.
Constrains
1 ≤ n ≤ 10000
SAMPLE INPUT 8
SAMPLE OUTPUT 4 6 8
Assistant
n = int(input()) if n < 4: print('') else: sieve = [True] * (n + 1) sieve[0] = sieve[1] = False for i in range(2, int(n**0.5) + 1): if sieve[i]: sieve[i*i : n+1 : i] = [False] * len(sieve[i*i : n+1 : i]) composites = [str(i) for i in range(2, n + 1) if not sieve[i]] print(' '.join(composites))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.