Goal: write an application that computes all of the amicable
numbers less than or equal to a number input by an input dialog.
Look at the Perfect and JOptionPane examples in Review.zip.
This project is designed to give you practice using structured programming
constructs in Java, in particular the if..else and for constructions.
The computations will be done in the main method output will
be shown by printing directly to the command line with
System.out.println.
Two numbers a and b are amicable if the factors of a sum to b
and the factors of b sum to a.
The factors of 284 are 1, 2, 4, 71, 142;
1 + 2 + 4 + 71 + 142 = 220.
Therefore, 220 and 284 a pair of amicable numbers.
A simpler problem is finding all the perfect numbers as shown in the
Perfect example Perfect.java.
A number is perfect if its factors sum to itself.
The two perfect numbers less than 100 are 6 and 28 because
6 = 1 + 2 + 3 and 28 = 1 + 2 + 4 + 7 + 14.
Modify this program to find all amicable numbers less than the value
input by the input dialog. Make sure it works when you enter
5000. Remember how to change the string input by the
input dialog into an integer.
To check if n belongs to an amicable pair, find the sum (sum1)
of the factors of n.
Then find the sum (sum2) of all the factors of sum1. If sum2 == n,
you have found a pair of amicable numbers.
Do not print any pair of perfect numbers in your output.
Do not print a pair of
amicable numbers more than once.
Here are some hints for writing the source code for this project:
Write a static method (below your main method) defined like this:
public static int sumFactors(int n)
{
... // Body of method goes here.
}
It should compute the sum of all the factors of the input n.
Test your sumFactors method for finding perfect numbers before you
try to use it for finding amicable numbers.
In your main method, write a double for loop that checks all the
numbers n and m between 2 and upperBound. Check that the sum of the
factors of n equals m and that the sum of the factors of m equals n.