View Single Post
  #3 (permalink)  
Old 01-30-2008, 09:30 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Solution
Hello jvasilj1

Use this class. It will solve your problem.
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); } } } } }
Just use the constructor to run it:
Code:
new AmicablePairs(1000);
That should help you. Ask, if you need help to understand this class.
__________________
If your ship has not come in yet then build a lighthouse.
Reply With Quote