Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: prime numbers

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default 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?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: prime numbers

    Yeah so far I only have entering the random number part.

    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();
    Last edited by copeg; January 9th, 2011 at 11:20 AM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default 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.
    Last edited by copeg; January 9th, 2011 at 11:25 AM.

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: prime numbers

    this is what you looking for Sieve of Eratosthenes

Similar Threads

  1. Can't get seven random numbers, only one.
    By alpvii in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2010, 04:39 PM
  2. composite and prime
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 3
    Last Post: October 13th, 2010, 09:01 PM
  3. Need help.. Counting Prime #'s up to 50 w/while loop
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 6th, 2010, 05:40 PM
  4. Generate prime numbers within fiboncacci series?
    By Manish87 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 7th, 2010, 12:24 PM
  5. Need Help - Factoring & Prime Finding Code
    By prodigytoast in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 5th, 2009, 06:38 AM