|
1 | 1 | package com.thealgorithms.maths;
|
2 | 2 |
|
| 3 | +import java.util.LinkedHashSet; |
| 4 | +import java.util.Set; |
| 5 | +import org.apache.commons.lang3.tuple.Pair; |
| 6 | + |
3 | 7 | /**
|
4 |
| - * Amicable numbers are two different numbers so related that the sum of the |
5 |
| - * proper divisors of each is equal to the other number. (A proper divisor of a |
6 |
| - * number is a positive factor of that number other than the number itself. For |
7 |
| - * example, the proper divisors of 6 are 1, 2, and 3.) A pair of amicable |
8 |
| - * numbers constitutes an aliquot sequence of period 2. It is unknown if there |
9 |
| - * are infinitely many pairs of amicable numbers. * |
| 8 | + * Amicable numbers are two different natural numbers that the sum of the |
| 9 | + * proper divisors of each is equal to the other number. |
| 10 | + * (A proper divisor of a number is a positive factor of that number other than the number itself. |
| 11 | + * For example, the proper divisors of 6 are 1, 2, and 3.) |
| 12 | + * A pair of amicable numbers constitutes an aliquot sequence of period 2. |
| 13 | + * It is unknown if there are infinitely many pairs of amicable numbers. |
10 | 14 | *
|
11 | 15 | * <p>
|
12 |
| - * link: https://en.wikipedia.org/wiki/Amicable_numbers * |
13 |
| - * |
| 16 | + * link: https://en.wikipedia.org/wiki/Amicable_numbers |
14 | 17 | * <p>
|
15 |
| - * Simple Example : (220,284) 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110 |
16 |
| - * } <- Sum = 284 |
17 |
| - * 284 is divisible by -> 1,2,4,71,142 and the Sum of that is. Yes right you |
18 |
| - * probably expected it 220 |
| 18 | + * Simple Example : (220, 284) |
| 19 | + * 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110} <- SUM = 284 |
| 20 | + * 284 is divisible by {1,2,4,71,142} <- SUM = 220. |
19 | 21 | */
|
20 | 22 | public class AmicableNumber {
|
21 |
| - |
22 |
| - public static void main(String[] args) { |
23 |
| - AmicableNumber.findAllInRange(1, 3000); |
24 |
| - /* Res -> Int Range of 1 till 3000there are 3Amicable_numbers These are 1: = ( 220,284) |
25 |
| - 2: = ( 1184,1210) 3: = ( 2620,2924) So it worked */ |
26 |
| - } |
27 |
| - |
28 | 23 | /**
|
29 |
| - * @param startValue |
30 |
| - * @param stopValue |
31 |
| - * @return |
| 24 | + * Finds all the amicable numbers in a given range. |
| 25 | + * |
| 26 | + * @param from range start value |
| 27 | + * @param to range end value (inclusive) |
| 28 | + * @return list with amicable numbers found in given range. |
32 | 29 | */
|
33 |
| - static void findAllInRange(int startValue, int stopValue) { |
34 |
| - /* the 2 for loops are to avoid to double check tuple. For example (200,100) and (100,200) |
35 |
| - * is the same calculation also to avoid is to check the number with it self. a number with |
36 |
| - * itself is always a AmicableNumber |
37 |
| - * */ |
38 |
| - StringBuilder res = new StringBuilder(); |
39 |
| - int countofRes = 0; |
| 30 | + public static Set<Pair<Integer, Integer>> findAllInRange(int from, int to) { |
| 31 | + if (from <= 0 || to <= 0 || to < from) { |
| 32 | + throw new IllegalArgumentException("Given range of values is invalid!"); |
| 33 | + } |
| 34 | + |
| 35 | + Set<Pair<Integer, Integer>> result = new LinkedHashSet<>(); |
40 | 36 |
|
41 |
| - for (int i = startValue; i < stopValue; i++) { |
42 |
| - for (int j = i + 1; j <= stopValue; j++) { |
| 37 | + for (int i = from; i < to; i++) { |
| 38 | + for (int j = i + 1; j <= to; j++) { |
43 | 39 | if (isAmicableNumber(i, j)) {
|
44 |
| - countofRes++; |
45 |
| - res.append("" + countofRes + ": = ( " + i + "," + j + ")" |
46 |
| - + "\t"); |
| 40 | + result.add(Pair.of(i, j)); |
47 | 41 | }
|
48 | 42 | }
|
49 | 43 | }
|
50 |
| - res.insert(0, "Int Range of " + startValue + " till " + stopValue + " there are " + countofRes + " Amicable_numbers.These are \n "); |
51 |
| - System.out.println(res); |
| 44 | + return result; |
52 | 45 | }
|
53 | 46 |
|
54 | 47 | /**
|
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 |
59 |
| - * otherwise false |
| 48 | + * Checks whether 2 numbers are AmicableNumbers or not. |
60 | 49 | */
|
61 |
| - static boolean isAmicableNumber(int numberOne, int numberTwo) { |
62 |
| - return ((recursiveCalcOfDividerSum(numberOne, numberOne) == numberTwo && numberOne == recursiveCalcOfDividerSum(numberTwo, numberTwo))); |
| 50 | + public static boolean isAmicableNumber(int a, int b) { |
| 51 | + if (a <= 0 || b <= 0) { |
| 52 | + throw new IllegalArgumentException("Input numbers must be natural!"); |
| 53 | + } |
| 54 | + return sumOfDividers(a, a) == b && sumOfDividers(b, b) == a; |
63 | 55 | }
|
64 | 56 |
|
65 | 57 | /**
|
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 |
69 |
| - * operator |
70 |
| - * @return sum of all the dividers |
| 58 | + * Recursively calculates the sum of all dividers for a given number excluding the divider itself. |
71 | 59 | */
|
72 |
| - static int recursiveCalcOfDividerSum(int number, int div) { |
73 |
| - if (div == 1) { |
| 60 | + private static int sumOfDividers(int number, int divisor) { |
| 61 | + if (divisor == 1) { |
74 | 62 | return 0;
|
75 |
| - } else if (number % --div == 0) { |
76 |
| - return recursiveCalcOfDividerSum(number, div) + div; |
| 63 | + } else if (number % --divisor == 0) { |
| 64 | + return sumOfDividers(number, divisor) + divisor; |
77 | 65 | } else {
|
78 |
| - return recursiveCalcOfDividerSum(number, div); |
| 66 | + return sumOfDividers(number, divisor); |
79 | 67 | }
|
80 | 68 | }
|
81 | 69 | }
|
0 commit comments