1 2 Previous Next 28 Replies Latest reply on Mar 24, 2008 3:29 PM by 807601

# Calculating prime factors for a number

Well this code is supposed to print all the prime factors for a given number. For example, 8 gives 2 2 2 ,4 gives 2 2 and 9 gives 3 3, but when you try a number like 6, it only prints 2. Any ideas?
``````public void pFactors(int n)
{
for(int i=2;i<n;i++)
{
if(isPrime(i))
{
while(n%i==0)
{
System.out.println(i);
n/=i;
}
}
}
}``````
• ###### 1. Re: Calculating prime factors for a number
Well this code is supposed to print all the prime factors for a given number. For example, 8 gives 2 2 2 ,4 gives 2 2 and 9 gives 3 3, but when you try a number like 6, it only prints 2. Any ideas?
Your code is doing exactly what you tell it to do. Try describing your algorithm in plain language, then examine your code for discrepancies.

~
• ###### 2. Re: Calculating prime factors for a number
hint: changing the value of a variable that your loop relies on is dangerous.
• ###### 3. Re: Calculating prime factors for a number
``hint: changing the value of a variable that your loop relies on is dangerous.``
I know that but I need to do it here. No shorter alternative.
• ###### 4. Re: Calculating prime factors for a number
I know that but I need to do it here. No shorter alternative.
In general, I prefer "working" to "short".

~
• ###### 5. Re: Calculating prime factors for a number
try it for more than small number, you'll see it doesn't work for anything other than proper powers of integers 2, 4, 8, 9, 16, 27, 32...

hint: while(n%i==0) what are you really telling the computer to do there?
• ###### 6. Re: Calculating prime factors for a number
lol. I do to. But changing the variable is a part of the algorithm. if you dont it prints the same value again and again.
• ###### 7. Re: Calculating prime factors for a number
L.u.c.i.f.e.r. wrote:
``hint: changing the value of a variable that your loop relies on is dangerous.``
I know that but I need to do it here. No shorter alternative.
If shorter is wrong, then it's not an alternative, that is unless you enjoy failing your programming classes.
• ###### 8. Re: Calculating prime factors for a number
But changing the variable is a part of the algorithm.
As requested previously, describe your algorithm.

~
• ###### 9. Re: Calculating prime factors for a number
Look at your while loop. It runs until n%i isn't zero...but you change the value of n, so that doesn't work like it should. You say it doesn't work for 6...look at your code, see what happens if you put in 6. You start with i = 2, so it tests 6%2 == 0 which is true. So it prints out 2, then changes n to 6/2 which is 3. But i is still 2...so your while loop tests 3%2 == 0, which is false. So your code says that 3 is not a prime factor of 6, because you're checking if its a factor of 2 instead of testing it against 6.
• ###### 10. Re: Calculating prime factors for a number
as yawmark says: describe your algo

also see what the algo is compareted to what you are saying here:

while(n%i==0)

That line is NOT an implementation of the algo for prime factors. If you need convincing of it, then do the factoring by hand and then dry run your code using the values from your manual factor... it's not even close.
• ###### 11. Re: Calculating prime factors for a number
OK the for loop is basically meant to run through all the values. The if checks if the number is divisible by the current value of i and if i is a prime number. If so, the while loop prints the factor and reduces the number to prevent continuous printing of the same value.
• ###### 12. Re: Calculating prime factors for a number
L.u.c.i.f.e.r. wrote:
OK the for loop is basically meant to run through all the values.
But you aren't running through all the values if you reduce n every time you find a factor. There's absolutely no reason to do that. Why use the while loop at all? Just print the value if it's prime and move on in your for loop...
• ###### 13. Re: Calculating prime factors for a number
``But you aren't running through all the values if you reduce n every time you find a factor. There's absolutely no reason to do that. Why use the while loop at all? Just print the value if it's prime and move on in your for loop...``
Not only am i printing the factors, I'm also printing the number of times they occur. Like i said, 8 should give 2 thrice.
• ###### 14. Re: Calculating prime factors for a number
So use recursion.
1 2 Previous Next