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

Thread: display all prime factors of a number

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default display all prime factors of a number

    guys, calculating whether a number is prime or not is pretty straight forward

    static boolean isPrime(int p)
        {
            for(int i = 2; i < p; i++)
            {
                if(p % i == 0) return false;
            }
            return true;
        }

    given the fact that I have a code that tells me whether a number is prime or not, how could I implement another method that display all prime factors of a number

    Thanks


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: display all prime factors of a number

    Well, how do you know if a number is a factor of another number (your prime code gives a pretty good hint)?

    So, for every factor of a given number, just call your isPrime(int) method and send it the factor. If the method returns true, you know the factor is a prime (and then continue to check the rest of the factors).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: display all prime factors of a number

    Thanks... once you explained it like that I was able to see it right away. But now I have a small problem with my code. For example, lets take the number 20.
    my code displays

    20 = 2, 5

    which are both prime factors of 20, but I'm not sure if the correct way of displaying is 2, 5 or 2 * 2 * 5, or 2^2 * 5. the first way I think is right as my question ask for all prime factors of the number, and since 2 is repeated twice. But lets say that I would like to display 2^2 * 5 or 2 * 2 * 5. How could I approach the problem then?

    Thanks
    Last edited by mia_tech; May 18th, 2012 at 05:19 PM.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: display all prime factors of a number

    Once you have found that 2 is a prime factor you could check higher and higher powers of 2 to find the highest power that is a factor.

  5. #5
    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: display all prime factors of a number

    20/2 = 10;
    10/2 = 5;
    5/2 = 2.5

    20%2 = 0;
    10% 2 = 0;
    5%2 = 1;


    20/5 = 4;
    20%5 = 0;
    4%5 = 4;
    4/5 = .8

    4/2 = 2
    4%2 = 0;
    2/2 = 1
    2%2 = 0;



    12

    12/2 = 6;
    6/2 = 3;
    3/3 = 1;

    2,3

    9216

    9216/2 = 4608
    4608 /2 = 2304
    2304/2 = 1152
    1152/2 = 576
    576/ 2 = 288
    288/2 = 144
    144/2 = 72
    62/ 2 = 36
    36/2 = 18
    18/2 = 9;
    9/2 = 4.5
    9/3 = 3;
    3/3 = 1;
    1/3 = 0.33333

    2^10 * 3 ^2

    I have an idea for the implementation, though it's probably inefficient.

    I'm working on it right now though.

    It was basically to store all your prime factors in an ArrayList, then go through and divide them like above and have some kind of temporary variable inside a loop and increment it every time you can divide by that factor until you can't anymore. After that, put that factor into a HashMap and continue the same way with other factors, and then at the end, you'll have a HashMap with all of your factors, with the number as the key and the exponent as the value.

    So you're hash map for 9216 would be

    [2,10] [3,2]

    or something like that.

    That's the best I can think of. There might be, or probably is, a better and more efficient way.
    Last edited by javapenguin; May 18th, 2012 at 07:03 PM.

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: display all prime factors of a number

    ups... post before reading last thread.

Similar Threads

  1. Display number of threads in each running process
    By Waseem Usman in forum Threads
    Replies: 1
    Last Post: March 23rd, 2012, 09:51 AM
  2. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2012, 11:07 PM
  3. Prime Factorization Method: Won't reproduce all Factors every time
    By Staticity in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2011, 05:16 AM
  4. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 12:05 AM
  5. Display prime numbers from 100 to 200 in Java
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 25th, 2011, 02:14 PM