Todos os desafios
Médio 0 resolvidos 0% de aceitação

List Primes With a Sieve

Find primes up to a limit by marking composite multiples.

Return all prime numbers from 2 through limit. A prime is an integer greater than 1 divisible only by 1 and itself. Use the sieve idea: keep the next unmarked number and mark its larger multiples as composite. List results in ascending order.

Implement sieve_primes(limit).

Input

limit: The largest number that may be included.

Output

Return the primes in ascending order, or an empty list if none exist.

Restrições

0 ≤ limit ≤ 100,000. The upper bound is inclusive. Output tests verify results, not the exact algorithm used.

Exemplos

Exemplo 1
Entrada:
limit = 12

Saída esperada:
expected = [2, 3, 5, 7, 11]
Entrada:
long long limit = 12;

Saída esperada:
std::vector<long long> expected = {2, 3, 5, 7, 11};
Entrada:
long limit = 12;

Saída esperada:
long[] expected = new long[] {2, 3, 5, 7, 11};

Explicação

Mark multiples of 2 and 3 as composite. The remaining values up to 12 are 2, 3, 5, 7, and 11.

Exemplo 2
Entrada:
limit = 1

Saída esperada:
expected = []
Entrada:
long long limit = 1;

Saída esperada:
std::vector<long long> expected = {};
Entrada:
long limit = 1;

Saída esperada:
long[] expected = new long[] {};

Explicação

Prime numbers begin at 2, beyond this limit.

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.