Solving a Java Program that Prints Prime Factors of Numbers

In summary: Additionally, you should also make sure to reset the number variable back to 432731 after each iteration, otherwise it will continue to decrease and you will end up with incorrect output. In summary, to fix the issue, you need to change the for loop to a while loop and make sure to reset the number variable after each iteration.
  • #1
ek
182
0
Ok, I posted this in the software forum a this afternoon, but I guess all the java gurus are on vacation. So I thought I'd post it here. This is my problem:

Ok, I have a new problem.

This time 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.

I know the for loop is screwed up because it isn't executing the whole statement and I know the contents of the while loop are screwed because I have two variables called the same name.

Can somebody help me here? How do I go about fixing it?
 
Physics news on Phys.org
  • #2
The issue is that your for loop is only executing the first iteration of the loop, instead of looping through all 30 numbers. To fix this, you need to change the for loop to a while loop and make sure that you increment the number after each iteration. For example: int number = 432731;while (number > 432702) { System.out.println("Prime factors of " + number + ":"); while (number != 1) { number = factor(number); } number--;}This should loop through all 30 numbers and print out their prime factors.
 
  • #3



Firstly, I would suggest breaking down the problem into smaller steps and tackling them one at a time. This will make it easier to understand and debug any issues that may arise. For example, you can start by creating a method that prints out the prime factors of a single number. Once that is working correctly, you can then move on to creating a loop that calls this method for each number in the range.

Next, let's take a look at your current code and identify some potential issues. In your main method, you have a for loop that starts at 432731 and goes down to 432701. However, in each iteration of the loop, you are only printing "Prime factors of [number]:" but not actually calling the factor method to print the factors. This is why you are only getting the prime factors for the last number (432701).

To fix this, you can move the print statement inside the for loop and call the factor method for each number:

for (number=432731; number > 432701; number--) {
System.out.print("Prime factors of " + number + ":");
factor(number);
System.out.println(); // add a new line after each set of prime factors is printed
}

Next, let's take a look at your factor method. Currently, it is only returning the number divided by its smallest prime factor. However, it is not printing out the prime factors themselves. To fix this, you can modify the method to print the prime factors as it finds them, and then return the updated number divided by the 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 factor(input / x); // recursively call the method with updated number
}
}
}
return 0;
}

With these changes, your program should now print out the prime factors for each number in the range 432731 to 432701.

However, there is one more issue to address. Your Primes method is not actually checking if a number is prime. It is currently returning true if the input is not divisible by any number from 2 to the input, which will always be true for any number greater than 1. To fix this,
 

1. How do I determine if a number is a prime number in Java?

To determine if a number is prime in Java, you can use a for loop to check if the number is divisible by any number other than 1 and itself. If the number is only divisible by 1 and itself, then it is a prime number.

2. How do I find the prime factors of a number in Java?

To find the prime factors of a number in Java, you can use a for loop to iterate through all numbers from 2 to the given number and check if each number is a factor. If the number is a factor, you can then check if it is a prime number using the method described in the answer to the previous question.

3. How do I print the prime factors of a number in Java?

To print the prime factors of a number in Java, you can use the same method as described in the previous question to find the prime factors. Then, you can simply use a print statement to output the factors to the console.

4. Is there a faster way to find prime factors in Java?

Yes, there are several optimizations that can be made to the algorithm for finding prime factors, such as only checking up to the square root of the given number and only checking odd numbers. These optimizations can significantly improve the speed of the program.

5. Can I use a library or built-in function to find prime factors in Java?

Yes, there are several libraries and built-in functions in Java that can be used to find prime factors, such as the BigInteger class and the Java Math library. However, it is also a good exercise to implement your own algorithm for finding prime factors.

Similar threads

  • Programming and Computer Science
Replies
22
Views
757
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Calculus and Beyond Homework Help
Replies
16
Views
2K
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
3
Replies
97
Views
7K
  • Calculus and Beyond Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Programming and Computer Science
Replies
3
Views
995
Back
Top