Results 1 to 4 of 4
  1. #1
    Need Help is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default Finding Prime Factors

    I need to write a program that produces a random integer between 1 and 200.

    Then that number needs to be listed along with all of its prime factors.

    I think I'm good with the creating of the random number. The other part of the code I think just says if the number is prime or not. I need to try to change it so it produces the prime factors. Can anyone help me write this?

    public Driver()
    {
    // initialise instance variables
    int p = (int)(Math.random()*200+1);
    System.out.println(p);
    }

    public boolean isPrime(int p) {
    int divisor;
    boolean looksPrime;
    looksPrime = true;
    divisor = 2;
    while (divisor <= Math.sqrt(p)) {
    if (p/divisor == (double)p/divisor) {
    looksPrime = false;
    }
    divisor++;
    }
    return looksPrime;
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    testing for prime you should consider using the modulus operator in that if clause. the modulus operator(%) returns the remainder of the 2 numbers being divided
    Java Code:
    5 % 2 == 1
    5 % 5 == 0
    Now, how much do you understand about finding prime factors? I consider you look up how and work a few out by hand and see if it helps you understand how to approach this problem. Then try coding it and post up what you get, with specific questions indicating what you are stuck on.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you'll need a loop to run from the number to 1 and add a list of prime numbers like this:

    Java Code:
    public java.util.List<Integer> getPrimeNumbers(int randomNumber) {
        //declare list
        java.util.List<Integer> primeNumbers = new java.util.ArrayList<Integer>();
        //loop backwards
        for (int i=randomNumber; i>0; i--) {
            boolean iIsPrime = randomNumber%i == 0;
            if (iIsPrime) {
                primeNumbers.add(i);
            }
        }
        //return result
        return primeNumbers;
    }

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    you'll need a loop to run from the number to 1 and add a list of prime numbers like this:

    Java Code:
    public java.util.List<Integer> getPrimeNumbers(int randomNumber) {
        //declare list
        java.util.List<Integer> primeNumbers = new java.util.ArrayList<Integer>();
        //loop backwards
        for (int i=randomNumber; i>0; i--) {
            boolean iIsPrime = randomNumber%i == 0;
            if (iIsPrime) {
                primeNumbers.add(i);
            }
        }
        //return result
        return primeNumbers;
    }
    That solution is not correct; any number n > 0 has the property that n%n == 0, so your first pass through the loop will show that n isn't prime.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 08:25 PM
  2. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 11:42 PM
  3. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 06:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •