Results 1 to 3 of 3

# Thread: students needs help

- 01-30-2008, 07:10 AM #1Member
- Join Date
- Jan 2008
- Posts
- 36

- Rep Power
- 0

## students needs help

i have created java code to give me perfect numbers, and it works...but now im hung up on modifying the main to give me AMICABLE NUMBERS. Any ideas???????

HERE IS MY PERFECT.JAVA CODE

----------------------------------------------

// Find all perfect numbers less than

// or equal to then entered upper bound.

import javax.swing.JOptionPane;

public class Perfect

{

public static void main(String[] args)

{

int upperBound, n, factor, sum;

upperBound = Integer.parseInt(JOptionPane.showInputDialog(

"Enter upper bound for perfect number search"));

for(n = 2; n <= upperBound; n++)

{

sum = 0;

for(factor = 1; factor <= n / 2; factor++)

if (n % factor == 0)

sum += factor;

if (n == sum)

System.out.println(n);

}

}

}

// Prompt of input dialog:

Upper bound for perfect number search:

// Sample input to input dialog:

10000

// Output in Terminal Window:

6

28

496

8128

- 01-30-2008, 07:16 AM #2Member
- Join Date
- Jan 2008
- Posts
- 36

- Rep Power
- 0

THESE ARE THE INSTRUCTIONS

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

-------------------------------------------------------------------

- 01-30-2008, 08:30 PM #3
## Solution

Hello jvasilj1

Use this class. It will solve your problem.

Java Code:import java.util.*; public class AmicablePairs{ protected static Vector<Integer> getFactors(int number){ Vector<Integer> result = new Vector<Integer>(); for (int i = 1 ; i < number; i++){ if (number % i == 0) result.add(new Integer(i)); } return result; } protected static int sumNumbers(Vector<Integer> numbers){ int result = 0; for (Integer integer : numbers) result += integer; return result; } protected static void printNumbers(Vector<Integer> numbers){ for (Integer integer : numbers) System.out.println(integer); System.out.println(); } public AmicablePairs(int bound){ for (int m = 2; m <= bound; m++){ for (int n = 2; n <= bound; n++){ if ( (sumNumbers(getFactors(m)) == n) && (sumNumbers(getFactors(n)) == m)) { System.out.println(m + ", " + n); } } } } }

Java Code:new AmicablePairs(1000);

Eyes dwelling into the past are blind to what lies in the future. Step carefully.

## Bookmarks