id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

bryce.hulett@hotmail.com > sieve
prev  |  next  |  chance

Write a method named sieve that uses the "Sieve of Eratosthenes" algorithm to generate a list of prime numbers between 2 and a given maximum. You will represent the numbers using an array but return an ArrayList of the primes.

Algorithm:
Create a list of booleans(or ints) isPrime[2..n], all set to true.
Starting with p = 2, mark all multiples of p (i.e., 2p, 3p, ...) as false.
Move to the next unmarked number and repeat step 2.

Note the test cases do not check for effieciency so brute force will pass the test cases but please use the algorithm.


sieve(2) → [2]
sieve(3) → [2, 3]
sieve(10) → [2, 3, 5, 7]

...Save, Compile, Run (ctrl-enter)

public List<Integer> sieve(int N){ }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200 Post-solution available

Copyright Nick Parlante 2017 - privacy