java - Random Prime Methods by calling another method -


I have a hard time trying to understand this program. I have made a method to figure out that the entry number is a prime number, but now I have to write a method that takes a positive int number in the form of a parameter and a random principal in the range of [0, num-1] Returns the number]. I'm not sure that I'm using the isPrime method correctly. Besides, I have to test this method by calling it primarily, but I'm not sure how to do it as well as it is. This is my code:

  public static boolean isPrime (int num) {for (int i = 2; i  

This is not an effective way of doing this. I advise you to make one and choose a random object.

Here's how I will do it:

  public static random Random = new random (system .currentTimeMillis ()); Public static int randomPrime (int num) {boolean [] sieve = new boolean [num + 1]; Sieve [0] = true; Sieve [1] = true; For (int i = 2; i & lt; num.length; i ++) for (inf j = 2 * i; j & lt; num.length; j + = i) sieve [j] = true; & Lt; Integer & gt; Primes = new linked list & lt; & Gt; (); For (int i = 0; i & lt; sieve.length; i ++) if (! Sieve [i]) primes.add (i); Return primes.isEmpty ()? -1: primes.get (rd.nextInt (primes.size ());}  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -