|
| 1 | +package Maths; |
| 2 | + |
| 3 | +/** |
| 4 | + * Amicable numbers are two different numbers so related |
| 5 | + * that the sum of the proper divisors of each is equal to the other number. |
| 6 | + * (A proper divisor of a number is a positive factor of that number other than the number itself. |
| 7 | + * For example, the proper divisors of 6 are 1, 2, and 3.) |
| 8 | + * A pair of amicable numbers constitutes an aliquot sequence of period 2. |
| 9 | + * It is unknown if there are infinitely many pairs of amicable numbers. |
| 10 | + * * |
| 11 | + * <p> |
| 12 | + * * link: https://en.wikipedia.org/wiki/Amicable_numbers |
| 13 | + * * </p> |
| 14 | + * <p> |
| 15 | + * Simple Example : (220,284) 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110 } <- Sum = 284 |
| 16 | + * 284 is divisible by -> 1,2,4,71,142 and the Sum of that is. Yes right you probably expected it 220 |
| 17 | + */ |
| 18 | + |
| 19 | +public class AmicableNumber { |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + |
| 23 | + AmicableNumber.findAllInRange(1,3000); |
| 24 | + /* Res -> Int Range of 1 till 3000there are 3Amicable_numbers These are 1: = ( 220,284) 2: = ( 1184,1210) |
| 25 | + 3: = ( 2620,2924) So it worked */ |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param startValue |
| 31 | + * @param stopValue |
| 32 | + * @return |
| 33 | + */ |
| 34 | + static void findAllInRange(int startValue, int stopValue) { |
| 35 | + |
| 36 | + /* the 2 for loops are to avoid to double check tuple. For example (200,100) and (100,200) is the same calculation |
| 37 | + * also to avoid is to check the number with it self. a number with itself is always a AmicableNumber |
| 38 | + * */ |
| 39 | + StringBuilder res = new StringBuilder(); |
| 40 | + int countofRes = 0; |
| 41 | + |
| 42 | + for (int i = startValue; i < stopValue; i++) { |
| 43 | + for (int j = i + 1; j <= stopValue; j++) { |
| 44 | + if (isAmicableNumber(i, j)) { |
| 45 | + countofRes++; |
| 46 | + res.append("" + countofRes + ": = ( " + i + "," + j + ")" + "\t"); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + res.insert(0, "Int Range of " + startValue + " till " + stopValue + " there are " + countofRes + " Amicable_numbers.These are \n "); |
| 51 | + System.out.println(res.toString()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Check if {@code numberOne and numberTwo } are AmicableNumbers or not |
| 56 | + * |
| 57 | + * @param numberOne numberTwo |
| 58 | + * @return {@code true} if {@code numberOne numberTwo} isAmicableNumbers otherwise false |
| 59 | + */ |
| 60 | + static boolean isAmicableNumber(int numberOne, int numberTwo) { |
| 61 | + |
| 62 | + return ((recursiveCalcOfDividerSum(numberOne, numberOne) == numberTwo && numberOne == recursiveCalcOfDividerSum(numberTwo, numberTwo))); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * calculated in recursive calls the Sum of all the Dividers beside it self |
| 67 | + * |
| 68 | + * @param number div = the next to test dividely by using the modulo operator |
| 69 | + * @return sum of all the dividers |
| 70 | + */ |
| 71 | + static int recursiveCalcOfDividerSum(int number, int div) { |
| 72 | + |
| 73 | + if (div == 1) { |
| 74 | + return 0; |
| 75 | + } else if (number % --div == 0) { |
| 76 | + return recursiveCalcOfDividerSum(number, div) + div; |
| 77 | + } else { |
| 78 | + return recursiveCalcOfDividerSum(number, div); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +} |
0 commit comments