Starting from:
$35

$29

Lab 7: List client Solution

In this lab, you will implement a client of the List data structure. Namely, you will implement the Sieve of Eratosthenes, an ancient algorithm for determining every prime number up to some threshold.

Your TA will overview the opera ons in the List ADT, and will describe the Sieve of Eratosthenes algorithm.


Exercise




    • er the TA’s lesson, complete the following steps:


        1. Complete Sieve of Eratosthenes on paper to determine the primes under 60. Hint: There are 17 primes under 60.
        2. Download the provided code and read it over.

        3. Implement method primesUpTo(int max) . This method should use the Sieve of Eratosthenes to build and return a List<Integer> containing all of the primes below max . You may do this in two dis nct ways:


On one hand, you may directly manipulate a List<Integer> , removing values as you determine they are composite (i.e., not prime).

On the other hand, you may use a List<Boolean> , where a value of true at posi on i indicates that i is prime. Set values to false as you iden fy posi ons that represent composite values. Then, for each index in this list whose value is true , add that index to a new List<Integer> , your result.


The la  er is slightly more complex, but also more efficient (why?).


4. Test your program. You should be able to generate all primes under 1000 by running:
java cs445.lab7.SieveOfEratosthenes 1000


You should see the following output:



2357111317192329313741434753

59 61 67 71 73 79 83 89 97 101 103 107 109

113 127 131 137 139 149 151 157 163 167 173

179 181 191 193 197 199 211 223 227 229 233

239 241 251 257 263 269 271 277 281 283 293

307 311 313 317 331 337 347 349 353 359 367

373 379 383 389 397 401 409 419 421 431 433

439 443 449 457 461 463 467 479 487 491 499

503 509 521 523 541 547 557 563 569 571 577

587 593 599 601 607 613 617 619 631 641 643

647 653 659 661 673 677 683 691 701 709 719

727 733 739 743 751 757 761 769 773 787 797

809 811 821 823 827 829 839 853 857 859 863

877 881 883 887 907 911 919 929 937 941 947

953 967 971 977 983 991 997



Conclusion




In this lab, you implemented a sieving technique for determining all of the prime integers up to a threshold. More importantly, you prac ced using the List data structure as a client.

More products