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 18 of 18

Thread: Prime number solver.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Prime number solver.

    Good day. I want to create a program to calculate prime numbers up to an integer limit. I am extremely new to Java ( I'm on hour 13 of Sam's teach yourself Java in 24 hours), so bear with me.

    I have established that I must find the factors of a number, then deduce that if the only factors are 1 and the number itself, then i must print that number. After much trial and error, I have given up. My code has changed considerably, and I have added new sections on after every error i received, however now when I run the file, there is simply no output. The requirements for printing the prime number are not being fulfilled, it may be a problem with the loops or the array. Apologies for the messy code.

    class Primes {
        public static void main(String[] args) {
     
            //  Factors of a number - if the only ones on that list are 1 and the 
            //  nunber itself then print it.
     
            for (int number = 1; number <= 10; number++) {
                for (int counter = 1; counter < number; counter++) {
                    int factor = number / counter;
                    int remainder = number % counter;
     
                    int factorArray[] = new int[10];
     
                    if (remainder == 0) {
                    factorArray[counter] = factor;
                    }
     
                      if (counter == number) {
                          for (int newCounter = 1; newCounter < 10; newCounter++) {
                          int newFactor = 0;
                          newFactor = newFactor + factorArray[newCounter];
                if (newCounter == 9) {
                    if (newFactor == (number + 1))
                        System.out.println(number);
                }
            }
                      }  
     
            }
     
        }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Prime number solver.

    You have a lot of loops and if tests. What are they all for? Why do the loops use 9 & 10 as end points? What is the array for?
    It looks like you wrote a bunch of code without doing a design first. Can you make a list of the steps that the code should do to solve the problem? When you get a logical list of steps, then try writing the code for them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Prime number solver.

    Just a helpful bit of information, the maximum number of loops required for finding a prime number is the square root of that number. Then really after that you just need to check if the number in your loop is divisible by your counter variable. To do this you can use the modulus operator. So for instance if your number % counter is equal to zero, the number is not prime; else continue on, incrementing your counter variable.

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prime number solver.

    @ Norm. Yes i did not think about a design before writing the code, it is something I need to start doing. I have written the logical list of steps:


    1: Loop through some numbers up to a certain limit ( in this case I put it as 10 )

    2: Find all of the Factors of the current number in the loop

    3: Determine that if the only factors of that number are 1 and the current number, display that number.

    The array was used to hold the current factors of the number. The newCounter loop was designed to loop through the array elements, and add them to the newFactor variable. If newFactor variable was equal to 1, plus the number itself, then it is a prime number and must be printed. Does this make sense?


    @ Parranoia. If I understand you correctly, my loop should look like this.

    for (int counter = 1; counter < number.(sqrt function here); counter++)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Prime number solver.

    If the variable: number is the counter of prime numbers you want to find, why is it used anywhere else in the code? It should only count the number of prime numbers found.
    2: Find all of the Factors of the current number in the loop
    What variable contains the "current number"?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Prime number solver.

    Quote Originally Posted by Danny123 View Post
    @ Norm. Yes i did not think about a design before writing the code, it is something I need to start doing. I have written the logical list of steps:


    1: Loop through some numbers up to a certain limit ( in this case I put it as 10 )

    2: Find all of the Factors of the current number in the loop

    3: Determine that if the only factors of that number are 1 and the current number, display that number.

    The array was used to hold the current factors of the number. The newCounter loop was designed to loop through the array elements, and add them to the newFactor variable. If newFactor variable was equal to 1, plus the number itself, then it is a prime number and must be printed. Does this make sense?


    @ Parranoia. If I understand you correctly, my loop should look like this.

    for (int counter = 1; counter < number.(sqrt function here); counter++)
    You are correct, sir

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prime number solver.

    @ Norm. I want to print out the prime numbers, not the amount of them calculated. The variable number ( which is declared in the very first for loop ) holds each number that may or may not be a prime. The code is ran through for number = 1, then it is incremented and ran through for number 2 etc.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Prime number solver.

    This part was confusing:
    program to calculate prime numbers up to an integer limit
    Is the number the max value for a prime number or the number of prime numbers you want to find?
    In pseudo code:
    begin loop i = 1 to number
    if i is prime
      save i
    end loop
     
    vs
     
    cnt=0
    nbr = 1
    while cnt <= number
      if nbr is prime {
        save nbr
        cnt++
      }
    nbr++
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prime number solver.

    @ Norm. I apologize for not wording it correctly. I will try my best to convey it to you. I want to have a list of numbers, ( 1 to 10 in this case ) and I want to pick out the primes from this list, and print each one. I do not want to find the total number of primes, or their max value. I simply want to print out what numbers are prime from a given list.

    Looking at your Pseudo code is interesting. You state if i is prime. Could you possibly tell me how to tell if a number is prime? That is want I want to do. ( In my code I figured that a number must be prime, if the sum of the factors of that number only add up to 1 + that number )

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Prime number solver.

    I would ask Google how to determine if a number is prime.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Prime number solver.

    If a number has a factor other than one or itself, then it's not prime.

    Basically
    if number % otherNumber = 0 and otherNumber !=1 and otherNumber != number, then it's not prime.

  12. #12
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Prime number solver.

    Quote Originally Posted by javapenguin View Post
    If a number has a factor other than one or itself, then it's not prime.

    Basically
    if number % otherNumber = 0 and otherNumber !=1 and otherNumber != number, then it's not prime.
    That method is inefficient though. I've been running a program for over an hour calculating the sum of every prime number up to 10,000,000,000 using that method. I'm stuck with it because of the range limitations of an int but for what he's looking into, try studying a little bit of sieving. It's incredibly fast, all the primes up to 2000000 in .07 seconds, vs about .8 seconds I think while using that brute force method.

  13. The Following User Says Thank You to aesguitar For This Useful Post:

    copeg (May 31st, 2012)

  14. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Prime number solver.

    Quote Originally Posted by aesguitar View Post
    That method is inefficient though. I've been running a program for over an hour calculating the sum of every prime number up to 10,000,000,000 using that method. I'm stuck with it because of the range limitations of an int but for what he's looking into, try studying a little bit of sieving. It's incredibly fast, all the primes up to 2000000 in .07 seconds, vs about .8 seconds I think while using that brute force method.

    ...for instance, Sieve of Eratosthenes - Wikipedia, the free encyclopedia

  15. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (June 1st, 2012)

  16. #14
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prime number solver.

    @ Norm. I know what prime numbers are, don't be so cheeky.

    @ Javapenguin. Thank you. otherNumber !=1 and otherNumber != number. This is the code i needed. Can I ask you if my method is still viable though. ( If the sum of all the factors of number are only equal to 1 + number, then number is a prime ).

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Prime number solver.

    My response was to this question:
    Could you possibly tell me how to tell if a number is prime?
    Have you found how to do it?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #16
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prime number solver.

    Then I apologize. I was under the impression that you thought I did not know what a prime was. I think I know the algorithm now.

    Find the factors of a number ( by using if number % otherNumber = 0 ).
    Then say if otherNumber !=1 and otherNumber != number.
    That means the number is not prime.

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Prime number solver.

    No problem. I don't have the algorithm in my head and have to look it up if I need one.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #18
    Junior Member
    Join Date
    May 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prime number solver.

    check this code..... it may helpfull....

    class Primes {
    public static void main(String[] args) {
    int num = 10;
    System.out.println("Prime numbers below number"+num+" are:");
    for (int number1 = 1; number1 <= num; number1++) {
    int count = 0;
    for (int number2 = 1; number2 <= number1; number2++) {
    if (number1 % number2 == 0)
    count++;
    }
    if (count < 3)
    System.out.print(number1 + " , ");
    }
    }
    }
    Last edited by vijaykamma; June 5th, 2012 at 12:54 AM.

Similar Threads

  1. display all prime factors of a number
    By mia_tech in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2012, 07:55 PM
  2. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2012, 12:07 AM
  3. Automatic Solver
    By ppme in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 5th, 2011, 04:15 PM
  4. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 01:05 AM
  5. THE THREAD SOLVER
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 16th, 2010, 09:49 PM