This content has been marked as final.
Show 28 replies

1. Re: Calculating prime factors for a number
807601 Mar 24, 2008 9:21 AM (in response to 807601)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
807601 Mar 24, 2008 9:26 AM (in response to 807601)hint: changing the value of a variable that your loop relies on is dangerous. 
3. Re: Calculating prime factors for a number
807601 Mar 24, 2008 9:28 AM (in response to 807601)
I know that but I need to do it here. No shorter alternative.hint: changing the value of a variable that your loop relies on is dangerous.

4. Re: Calculating prime factors for a number
807601 Mar 24, 2008 9:29 AM (in response to 807601)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
807601 Mar 24, 2008 9:30 AM (in response to 807601)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
807601 Mar 24, 2008 9:31 AM (in response to 807601)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
807601 Mar 24, 2008 9:33 AM (in response to 807601)L.u.c.i.f.e.r. wrote:
If shorter is wrong, then it's not an alternative, that is unless you enjoy failing your programming classes.
I know that but I need to do it here. No shorter alternative.hint: changing the value of a variable that your loop relies on is dangerous.

8. Re: Calculating prime factors for a number
807601 Mar 24, 2008 9:34 AM (in response to 807601)But changing the variable is a part of the algorithm.
As requested previously, describe your algorithm.
~ 
9. Re: Calculating prime factors for a number
807601 Mar 24, 2008 9:40 AM (in response to 807601)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
807601 Mar 24, 2008 9:43 AM (in response to 807601)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
807601 Mar 24, 2008 9:44 AM (in response to 807601)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
807601 Mar 24, 2008 9:46 AM (in response to 807601)L.u.c.i.f.e.r. wrote:
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...
OK the for loop is basically meant to run through all the values. 
13. Re: Calculating prime factors for a number
807601 Mar 24, 2008 9:49 AM (in response to 807601)
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.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...

14. Re: Calculating prime factors for a number
807601 Mar 24, 2008 9:53 AM (in response to 807601)So use recursion.