I'm trying to make a program that prints all the prime factors of a given number and the 29 numbers one less than it. These numbers iare 432731 -> 432702.

So here is my program:

public class evan5ccc { /* the method tests whether a number is prime*/

public static boolean Primes(int input) { for (int x=2; x < input; x++) if (input % x == 0) return false; return true; }

/* computes the smallest prime factor of the input number, returns that number divided by its smallest prime factor */

public static int factor (int input) { if (input == 1) return input; else { for (int x=2; x <= input; x++) if (input % x == 0) { System.out.print(" " + x ); return (input / x); } } return 0; }

public static void main(String args[]) { int number;// number to be decomposed

for (number=432731; number > 432701; number--)

System.out.println("Prime factors of " + number + ":");

while (number != 1) { number = factor(number); } } }

And here is the output:

Prime factors of 123456760: Prime factors of 123456761: Prime factors of 123456762: Prime factors of 123456763: Prime factors of 123456764: Prime factors of 123456765: Prime factors of 123456766: Prime factors of 123456767: Prime factors of 123456768: Prime factors of 123456769: Prime factors of 123456770: Prime factors of 123456771: Prime factors of 123456772: Prime factors of 123456773: Prime factors of 123456774: Prime factors of 123456775: Prime factors of 123456776: Prime factors of 123456777: Prime factors of 123456778: Prime factors of 123456779: Prime factors of 123456780: Prime factors of 123456781: Prime factors of 123456782: Prime factors of 123456783: Prime factors of 123456784: Prime factors of 123456785: Prime factors of 123456786: Prime factors of 123456787: Prime factors of 123456788: Prime factors of 123456789: 3 3 3607 3803

This is copied and pasted from the sample output, so the numbers are wrong, but it basically prints just the last number's prime factors, instead of all 30. Also, it prints it on the next line, because i used println, but if I use print then it all goes into a big mess.

The output is supposed to look like this:

Prime factors of 123456760: 2 2 2 5 7 271 1627 Prime factors of 123456761: 123456761 Prime factors of 123456762: 2 3 3 11 13 47963 Prime factors of 123456763: 4021 30703 Prime factors of 123456764: 2 2 617 50023 Prime factors of 123456765: 3 5 523 15737 Prime factors of 123456766: 2 1051 58733 Prime factors of 123456767: 7 17636681 Prime factors of 123456768: 2 2 2 2 2 2 2 2 3 160751 Prime factors of 123456769: 53 283 8231 Prime factors of 123456770: 2 5 29 425713 Prime factors of 123456771: 3 3 3 17 268969 Prime factors of 123456772: 2 2 30864193 Prime factors of 123456773: 11 83 135221 Prime factors of 123456774: 2 3 7 7 419921 Prime factors of 123456775: 5 5 13 19 19993 Prime factors of 123456776: 2 2 2 79 195343 Prime factors of 123456777: 3 5779 7121 Prime factors of 123456778: 2 23 1531 1753 Prime factors of 123456779: 109 173 6547 Prime factors of 123456780: 2 2 3 3 5 47 14593 Prime factors of 123456781: 7 41 149 2887 Prime factors of 123456782: 2 61728391 Prime factors of 123456783: 3 2797 14713 Prime factors of 123456784: 2 2 2 2 11 11 43 1483 Prime factors of 123456785: 5 24691357 Prime factors of 123456786: 2 3 20576131 Prime factors of 123456787: 31 31 128467 Prime factors of 123456788: 2 2 7 13 17 71 281 Prime factors of 123456789: 3 3 3607 3803

So how do I make all 30 prime factorizations happen? I've been staring at this code for some time now and I can't figure it out.

With the above formatting, can you see why only the last number is factored? If not, here's an explanation:

The for loop repeats only the first statement immediately following it. In this case, the println() statement is repeated for each value of number. Finally, teh while loop repeats and factors the current value of number (which happens to be the last one you want.

So to start fixing this problem, you need to make sure the for loop repeats the factoring alogrithm for each number. Something like this will get you started in the right direction:

This will get the repetition started. However, you have another problem: the variable number changes in the while loop. I'll let you play with this to see if you can come up with a solution to this problem.

When I put brackets around the for loop contents it just prints out one statement.

And how do I fix this part:

number = factor(number);

That has to be wrong. I have a feeling that is what is screwing it up.

Evan Keenlyside
Greenhorn

Joined: Nov 04, 2004
Posts: 9

posted

0

Anybody? Please, I really need help on this one. It's driving me crazy. I don't know how to resolve the variable problem. This is really frustrating. Everything I try and do either sends the program into a horrible infinite loop or makes the output just one line.

Hi Evan, I want to help you... I have changed your code like this...

And I have the result like this

I dont know whether the result is what you want or not. But I just remind you that you should use bracked for conditional logic. It will help us to know the behavior of our code inside that conditional.

correct me if I am wrong... Help this helps... thanks daniel

Evan Keenlyside
Greenhorn

Joined: Nov 04, 2004
Posts: 9

posted

0

WOW.

Thank you. I REALLY appreciate it.

I knew that stupid while loop was the problem. I'm just not good enough at java to fix the problem.