+ 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.
20 Answers
+ 11
These codes may help in faster prime number generation.
https://code.sololearn.com/cj0ipKu7uIEm/?ref=app
https://code.sololearn.com/c3p5K1MKR181/?ref=app
+ 10
can you give me a simple example and I will see how to solve your problem?
+ 10
here just the odd numbers @Baptiste
+ 10
I hope this helps 😅
https://code.sololearn.com/cPhw1ZF8gsjr/?ref=app
+ 10
Thank u all And @Luka i will read your program and understand.
+ 10
https://code.sololearn.com/cW7O2znnncA7/?ref=app try this one it's work
+ 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.
+ 8
@Davye, is your code private?
+ 7
@Baptiste Exactly.
+ 7
i would use a counter. count = 0 outside loop. condition of loop would be count <= 5
+ 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
+ 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.
+ 5
@Dayve, it will not work as your stopping criteria will not allow to display 5 numbers with big starting numbers (such as 856000)
+ 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 ?
+ 3
Is it something like that ?
Input : 4
numbers : 5, 7, 11, 13, 17
Input : 1
numbers : 2, 3, 5, 7, 11
+ 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
+ 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 + " ");
}
}
}
+ 1
add a variable count so that u can stop after 5 nos
0
write a new answer