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

Thread: Trial Division Loop Problem

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    California, Bay Area
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trial Division Loop Problem

    Hello everybody!

    I've been working on trial division loop problem and I hit a dead end

    Before I write about how I attempted to the problem before coming here, I have to show you guys the problem.
    THE PROBLEM-------------------------------
    Find and print out the eight prime integers between 99,900 and 99,999 using trial division (testing divisibility by possible factors). Write two nested loops: The outer loop will iterate over the 100 numbers being tested for primeness; the inner loop will check potential factors, from 2 up to the square root of the large dividend number (don't worry about which ones are or aren't prime). Use the modulo operator % to test for divisibility, and stop testing factors — cut the inner loop short — after finding one. Example: the first dividend to test is 99,900; its potential factors run from 2 to 316. Since ( 99900 % 2 == 0 ), it is not prime, so do not check factors 3 and higher, but exit the inner loop and go directly to processing 99,901.
    --------------------------------------------

    SO TO TACKLE THIS PROBLEM.
    Thanks for jps for the advice on writing out the steps.
    1. I set the min value to 99900 and the max value to 99999 and my outer loop will iterate for 100 times.
    2. The outer loop also has to test for primeness so I wrote an "if statement" to check for divisibility and if it is divisible by 2, a continue statement to end
    that loop and to continue to the next one.
    3. I wrote the inner loop to test for factors by trial division.
    4. Loop goes wrong and won't print the prime numbers.. anybody have any ideas on whats wrong?

    Heres my loop:
     
    /* This program is designed to find and print out the eight prime integers between 99,900 and
    99,999 using trial division*/
    // instructions = write two nested loops
    // the outer loop will iterate over the 100 numbers being tested for primeness
    // the inner loop will check potential factors from 2 up to the square root of the large dividend numbers.
    // This program will use % to test for divisibility
     
    public class assignment7
    {
            public static void main(String args[])
            {
            // The two numbers
            int divider = 2;
            int min;
            int max = 99999;
     
     
            // Loop iteration of the 100 numbers
            for(min = 99900; min < max; min++)
            {
             for(divider = 2; divider <= Math.sqrt(min); divider++)
             {
     
              if(min % divider == 0)
              {
              continue;
              }
             System.out.println("Prime: "+ min);
              }
            }
     
            }
    }


    any ideas or advice?
    Last edited by Wwong3333; October 7th, 2012 at 02:44 AM.
    work hard, play hard.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trial Division Loop Problem

    Sort the problem out into steps.

    from m=Min to Max
       test m for prime
          if m is found to not be prime during tests, stop testing m and move to m+1
          otherwise all tests failed, m must be prime, PRINT m OR SAVE m FOR PRINTING LATER ON
    The caps part marks the place in your loops where you would remember or print a prime number.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Location
    California, Bay Area
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trial Division Loop Problem

    Quote Originally Posted by jps View Post
    Sort the problem out into steps.

    from m=Min to Max
       test m for prime
          if m is found to not be prime during tests, stop testing m and move to m+1
          otherwise all tests failed, m must be prime, PRINT m OR SAVE m FOR PRINTING LATER ON
    The caps part marks the place in your loops where you would remember or print a prime number.
    Thanks for the reply!
    I understand the steps you gave me.. but is there a way in java where you can assign a variable a certain range?
    you put m = Min to Max
    work hard, play hard.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trial Division Loop Problem

    Quote Originally Posted by Wwong3333 View Post
    you put m = Min to Max
    The direct answer to the question is no.
    What I wrote up is pseudo code. That line would refer to the portion of your code where you set a for loop to run from 99900 to 99999, where Min=99900 and Max=99999. Using m as the "current number up for evaluation" as the loop progresses. So first cycle m=99900. Second cycle m=99901. ...and so on.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Location
    California, Bay Area
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trial Division Loop Problem

    Quote Originally Posted by jps View Post
    The direct answer to the question is no.
    What I wrote up is pseudo code. That line would refer to the portion of your code where you set a for loop to run from 99900 to 99999, where Min=99900 and Max=99999. Using m as the "current number up for evaluation" as the loop progresses. So first cycle m=99900. Second cycle m=99901. ...and so on.
    Ahh! pseudocode. Thanks for the steps you wrote down for me. I wrote an outer loop that runs from 99900 to 99999, and wrote a inner loop to test for primeness. However it goes wrong, and debugging is giving me major headaches.. any tips on how to fix it?
    work hard, play hard.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trial Division Loop Problem

    Quote Originally Posted by Wwong3333 View Post
    I wrote an outer loop that runs from 99900 to 99999,
    Nice. Does it work? This looks like a good place to do a println(indexOfTheLoop) just to make sure it prints 99900, 99901, 99902, ..., 99999 before moving on.



    Quote Originally Posted by Wwong3333 View Post
    and wrote a inner loop to test for primeness.
    Nice. Does this part work? Yet another stop-to-test-point the way I see it. You could println(allNumbersFoundToBePrime) just to have a list to compare and see that you are getting only primes, and all primes. There are many lists of prime numbers available through your favorite search engine to compare to.



    Quote Originally Posted by Wwong3333 View Post
    However it goes wrong,
    That does not sound good. But other than that, it does not tell us much.



    Quote Originally Posted by Wwong3333 View Post
    any tips on how to fix it?
    Nothing specific. My old eyes can not see your screen from here. Please post your code and error messages when posting a question. You can use comments in code to draw attention to areas you think need attention.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Location
    California, Bay Area
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trial Division Loop Problem

    Quote Originally Posted by jps View Post
    Nice. Does it work? This looks like a good place to do a println(indexOfTheLoop) just to make sure it prints 99900, 99901, 99902, ..., 99999 before moving on.


    Nice. Does this part work? Yet another stop-to-test-point the way I see it. You could println(allNumbersFoundToBePrime) just to have a list to compare and see that you are getting only primes, and all primes. There are many lists of prime numbers available through your favorite search engine to compare to.



    That does not sound good. But other than that, it does not tell us much.



    Nothing specific. My old eyes can not see your screen from here. Please post your code and error messages when posting a question. You can use comments in code to draw attention to areas you think need attention.
    Ah sorry, I'm new to using forums.

    // Loop iteration of the 100 numbers
            for(min = 99900; min < max; min++)
    The outer loop works fine. It iterates 100 times from 99900 to 99999.
    However, the problem resides in the inner loop.
     for(divider =2; divider <= Math.sqrt(min); divider++)
             {
              if(min % divider == 0)
              {
              continue;
              }
              else
              {
              System.out.println("Prime number: "min);
              }
             }
    I've tried using while & do-while loops but no output comes out.
    I am confused because my program outputs a whole bunch of numbers including even ones. As you are a expert, do you see any errors or flaws within my inner loop? Sorry.. I would try to work it out by myself but I spent 4 hours trying new things and debugging but nothing works..

    also not to mention feeling like a complete failure after all that hard work over 30 lines of code =\
    Last edited by Wwong3333; October 7th, 2012 at 11:01 PM.
    work hard, play hard.

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trial Division Loop Problem

    Seems to be a syntax error on this line:
    System.out.println("Prime number: "min);
    I assume that was related to transferring the code to the forum since you said the program prints, and obviously that line will not compile.



    Try to break the pseudo code down into smaller steps. When every detail is listed, translating to code will be much easier. What is the output exactly? Compare the output you are getting to the code. What line of code says even numbers are not prime, and should be ignored? If you can step through the code, inserting values in place of the variables, line by line, and see what the code is doing, you will eventually see the point where the code does something that does not match what you thought it would do.

    When posting code and questions, it is also a great idea to include the output from your sample run.

Similar Threads

  1. Simple Division not working heeeeelp
    By ashboi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 9th, 2012, 05:48 PM
  2. Division by Zero
    By mael331 in forum Java Theory & Questions
    Replies: 16
    Last Post: December 6th, 2011, 06:13 PM
  3. Variable Division Issues
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:57 PM
  4. division problem
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 30th, 2010, 01:51 PM
  5. [SOLVED] Java program to write a code to detect divisibility of any number by 11
    By Java'88 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 11th, 2009, 10:41 PM