# prime numbers

• January 9th, 2011, 11:08 AM
tdz013
prime numbers
So I understand the basics of finding prime numbers, but I'm trying to do something a bit different. I want to find the prime number that follows or comes before any random number. So for example if I picked 12, the prime numbers would be 13 and 11. I've thought about using a loop for it, but I'm not quite sure. Any ideas?
• January 9th, 2011, 11:11 AM
copeg
Re: prime numbers
What have you done so far? Do you know how to generate a random number? Do you have a method which generates all prime numbers?
• January 9th, 2011, 11:13 AM
tdz013
Re: prime numbers
Yeah so far I only have entering the random number part.

Code java:

```import java.util.Scanner; public class prog335t { public static void main(String[]args) {   Scanner input=new Scanner(System.in); int n; System.out.print("Enter an integer greater than or equal to 2: ");   n= input.nextInt();```
• January 9th, 2011, 11:23 AM
copeg
Re: prime numbers
For future reference, please surround your code with either the [highlight=java][/highlight] or Y[code][/code] tags (makes it much more readable). I've edited your post above.

First thing is first, you mentioned in your post above you understand the basics of finding prime numbers....do you have any code which finds prime numbers, or validates a number for being prime? Validating is important, for example a very simple brute-force algorithm to solve the original problem would be to continually increment/decrement the entered value - validating each resulting number until a number validates as being prime.
• January 13th, 2011, 10:24 AM
IDK12
Re: prime numbers
this is what you looking for Sieve of Eratosthenes