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

Thread: Prime Factorization Method: Won't reproduce all Factors every time

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Post Prime Factorization Method: Won't reproduce all Factors every time

    	public ArrayList hasPrimeFactors(int x){
    		ArrayList list = new ArrayList();
    		if(isPrime(x)==true){
    			list.add(x);
    		}
    		for(long i = 2; i<x; i++){
    			while(x % i == 0){
    				System.out.println();
    				list.add(i);
    				x /= i;
    			}
    		}
    		if(list.size() == 0){
    			list.add("No Prime Factors");
    		}
    		return list;
    	}

    I suppose the issue arises in the While loop that is nested within the For Loop. I am guessing that the division x /= i; won't produce a remainder of 0 once the number is divided by the modulus again.. If that previous sentence doesn't make sense to you, ignore it. But, could someone guide me or go through this to see what's causing the issue? I've already debugged it, and noticed this trend...

    Input: 15
    Prime Factors: [3]

    Input: 25
    Prime Factors: [5, 5]

    Input 14:
    Prime Factors: [2]

    Input 50:
    Prime Factors: [2, 5, 5]

    Input 8:
    Prime Factors: [2, 2, 2]

    Here are several different scenarios... Any help would be greatly appreciated.
    Simplicity calls for Complexity. Think about it.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Prime Factorization Method: Won't reproduce all Factors every time

    In case of 14: The reason is first time you enter in the for loop, x's value is 14. So, in while loop, 14%2==0 and it enters and sets the value of x to 7 by your statement x/=i;
    And you know 7 has no factors at all except 7 but you are running your loop <x, means 6. So, that is why you are getting only one factor instead of 2.
    Note: Store the x value in some temporary variable and assign that temporary variable as a condition to for loop(outer loop).
    Hope it helps..

Similar Threads

  1. execute a method over a maximum period of time
    By PedroCosta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 24th, 2011, 03:06 PM
  2. trying to generate prime numbers
    By yingyang69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 28th, 2011, 12:21 PM
  3. prime numbers
    By tdz013 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2011, 11:24 AM
  4. composite and prime
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 3
    Last Post: October 13th, 2010, 10:01 PM
  5. 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, 06:40 PM