Junilu Lacar wrote:the many answers to the OP...
I would think more like two weeks. I think you can introduce Streams as soon as you introduce arrays. You say,Junilu Lacar wrote:If OP thinks arrays are too advanced right now, I'd bet dollars to donuts that streams are about two semesters worth of coursework above his learning level at this point.
You then have to explain a bit about how the Streams work, and then show how much nicer the code is than what you get with a for loop. I think there is going to more confusion about why my random array doesn't contain 10. Streams are objects, so it is a more object‑oriented solution than a loop.This is how you create an array filled with 100 instances of 999:-and this is how you create an array containing 100 “random” ints from 0 to less than 10:-and this is how you create an array containing the ints 101...200:-
Nor can I. The data structure, even if it is only numbers written on a sheet of paper, as shown in the Wikipedia page, is an essential part of a Sieve algorithm. Wikipedia also suggests there are other kinds of sieve.I can't think of a way to implement the SoE algorithm without some kind of data structure . . .
And you can use it in a similar fashion to what I showed with the bit set. In fact any data structure can be used in a similar fashion; it is the same concept and whether it isan array of boolean is the most common data structure used. . . .
Campbell Ritchie wrote:But if OP really hasn't been introduced to arrays, is this assignment over their head? OP is now stuck with the inelegant solution with a plain simple loop which Junilu showed.
S Connor wrote:
Instead it is asking, when testing values of n, what size number to you need to test up to to know you have found all the factors and thus whether n is a prime number. Is that correct?
OP wrote:"How many numbers up to 10,000 do you have to test to ensure that you have found all the primes?"
Deitel wrote:
(Chapter 6, exercise 25)
...
3. Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need only go as high as the square root of n. Why? Rewrite the program, and run it both ways.
a)Write a method that determines whether a number is prime. (I can do this part.)
b)Use this method in an application that determines and displays all the prime numbers less than 10,000. How many numbers up to 10,000 do you have to test to ensure that you have found all the primes?
Carey Brown wrote:One of the optimizations that can be made to the innards of isPrime() is have a loop whose limit is the sqrt(n). No sieve involved.
6.25 An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not.
1. Write a method that determines whether a number is prime.
2. Use this method in an application that determines and displays all the prime numbers less than 10,000. How many numbers up to 10,000 do you have to test to ensure that you have found all the primes?
3. Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need only go as high as the square root of n. Why? Rewrite the program, and run it both ways.
...Rewrite the program and run it both ways. Estimate the performance improvement.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |