+ 23

Plz Help give me logic of this Question.

Write a java program to enter a number and print Next 5 Prime number from that particular number. Eg: Entered number 5: Output: 7,11,13,17,19 [Next 5 Prime number]. It is not a homework and i don't want Full code just give me logic.

29th May 2017, 4:33 AM
Fuyuka Ayumi(冬花)
20 Answers
29th May 2017, 5:33 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 10
can you give me a simple example and I will see how to solve your problem?
29th May 2017, 4:41 AM
Said BAHAOUARY
Said BAHAOUARY - avatar
+ 10
here just the odd numbers @Baptiste
29th May 2017, 4:45 AM
Said BAHAOUARY
Said BAHAOUARY - avatar
29th May 2017, 5:11 AM
Sachin Artani
Sachin Artani - avatar
+ 10
Thank u all And @Luka i will read your program and understand.
29th May 2017, 7:48 AM
Fuyuka Ayumi(冬花)
29th May 2017, 3:01 PM
Said BAHAOUARY
Said BAHAOUARY - avatar
+ 8
actually i know i have to start loop from n but how do i know when it had to be ended i tried n+5 but it won't work. I humble request if any reference for just looping part please guys. If you know already help me.
29th May 2017, 4:54 AM
Fuyuka Ayumi(冬花)
+ 8
@Davye, is your code private?
29th May 2017, 5:14 AM
Sachin Artani
Sachin Artani - avatar
+ 7
@Baptiste Exactly.
29th May 2017, 4:45 AM
Fuyuka Ayumi(冬花)
+ 7
i would use a counter. count = 0 outside loop. condition of loop would be count <= 5
29th May 2017, 4:57 AM
jay
jay - avatar
+ 6
😀 I do not java. But one would need a way to generate prime numbers. here. https://en.m.wikipedia.org/wiki/Sieve_of_Eratosthenes
29th May 2017, 4:51 AM
jay
jay - avatar
+ 6
Yayz someone who wants the logic!# Let x = the entered number. What's a quick way to find out if a number is a prime?? * Find out if it's divisible by any number less than the square root of x, only checking odd numbers. (exception of 2) Try not to check divisibility of every number up to x like what lots of Sololearners do, it's painfully slow and NOT practical. How do we know how many primes we've found?? * Easy, use a variable to store that. int count; * Find 5 primes past x. Ok, just use a while loop until count is >= 5.. How do we know what numbers to check if prime? * We don't. Start at x+1, and just keep checking and incrementing x each time. Only check odds though. x += (x % 2 == 0) ? 1 : 2; // this will only check odds Once a prime is found, add it to a list or array and increment count. Note* If a list is used you can just keep track of the size() instead of using variable count.
29th May 2017, 5:22 AM
Rrestoring faith
Rrestoring faith - avatar
+ 5
@Dayve, it will not work as your stopping criteria will not allow to display 5 numbers with big starting numbers (such as 856000)
29th May 2017, 5:13 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 4
@Said not odd but prime (it misses the 9 with the input 4) @Fuyuka so, as you know what should be the output, what is the "logic" your searching for ?
29th May 2017, 4:47 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
Is it something like that ? Input : 4 numbers : 5, 7, 11, 13, 17 Input : 1 numbers : 2, 3, 5, 7, 11
29th May 2017, 4:43 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
Hmm... logic only? Maybe something like this? arrPrime[] Input start number While loop arrPrime length < 5 Increment input number by 1 If prime, push to arrPrime Print arrPrime Did you also need the logic for checking if number is prime? I think this would work for i = 2, thru sqrt(num), increment by one /*better yet, as somebody else mentioned, go by 2 and the odds*/ if num%i = 0, not prime else prime
29th May 2017, 5:47 AM
Joseph P French
Joseph P French - avatar
+ 1
public class GeneratePrimeNumbersExample { public static void main(String[] args) { //define limit int limit = 100; System.out.println("Prime numbers between 1 and " + limit); //loop through the numbers one by one for(int i=1; i < 100; i++){ boolean isPrime = true; //check to see if the number is prime for(int j=2; j < i ; j++){ if(i % j == 0){ isPrime = false; break; } } // print the number if(isPrime) System.out.print(i + " "); } } }
29th May 2017, 8:38 AM
VINEETH V
VINEETH V - avatar
+ 1
add a variable count so that u can stop after 5 nos
29th May 2017, 8:39 AM
VINEETH V
VINEETH V - avatar
0
write a new answer
29th May 2017, 2:18 PM
Fredy Antonio Benitez Sosa
Fredy Antonio Benitez Sosa - avatar