+ 1
Implementation of prime number iterator
Could you give me a good implementation example of a Prime Number iterator? I would iterate several times, without limits, so I cannot preallocate a limit defined array of prime numbers. Keep in mind that calculating a high value prime would be very slow, so you have to calculate a number only if required. Please vote if you like it, please answer with a link to your code to get a vote! I will appreciate the smartest solutions. Thank you!!
5 Answers
+ 1
Hi @Blake Icabone, I already have an implementation of my request. It is like a contest..
Sorry, I don't like your implementation, to find a Prime Number you used to much lines.. ;)
I hope you would not be angry with me, I'm looking for smart thinking like the tags..
+ 1
Hey! Who disliked my post? Please tell me why!..
+ 1
Ok, there is my implementation..
I used only a class but a better way is implement a Prime Number class (static) and a Prime Number Iterator that is an extension of the best known java Iterator.. I call this implementation "smart" because has no limits (it uses an ArrayList and its elements are allocated only if required by the "hasNext" method).
https://code.sololearn.com/c6FnLRg2xBYC/?ref=app
+ 1
Hi @Blake Icabone, I appreciate your answer (and not very much your random -1 points), you've finally given me a thought (I mean the smart thinking I wrote in the title of this post). Thanks for this!
But if you allow me I would argue the following points:
- as first I asked for a specific implementation, yours would change a little (I asked for a iterator implementation and dynamic generation only if needed*);
- ok it's not so fast but it is known that iterators would decrease a little the benchmark;
- in any case I cannot take the Learncode compiler as a speed reference.. (on a IBM AIX server works very well);
- in a practical case the Prime Number generated would be about 2^50.. After 2^35 the iterator slowness is compensated by the speed of the generator code;
In your code I appreciated the use of only the odd numbers incremented by 2;
* Why I asked for these requisites? Because I found myself in this situation and I thought it could be a case study for all users like you.. Moreover these requests treat very interesting topics of java language..
I apologize to everyone, I do not want to teach anyone, I just want to share experiences!
0
Ok, so all SoloLearn users are newbies and there are not real coders!
I cannot believe that in this blog there is no one who is interested in smart coding..