Solving a Java Program that Prints Prime Factors of Numbers

Click For Summary
the method should return false for any number that has divisors other than 1 and itself. The main issue in the program is that the factor method needs to be called within the for loop to print the prime factors for each number, rather than just at the end. Additionally, the factor method should be modified to recursively print all prime factors instead of just the smallest one. By implementing these changes, the program will correctly output the prime factors for all 30 numbers from 432731 to 432702. Properly structuring the code will enhance its functionality and ensure accurate results.
ek
Messages
182
Reaction score
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



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,
 
The book claims the answer is that all the magnitudes are the same because "the gravitational force on the penguin is the same". I'm having trouble understanding this. I thought the buoyant force was equal to the weight of the fluid displaced. Weight depends on mass which depends on density. Therefore, due to the differing densities the buoyant force will be different in each case? Is this incorrect?

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 97 ·
4
Replies
97
Views
9K