Skip to content

Commit 4deae1e

Browse files
authored
Merge pull request #1301 from BenSchokoRiegel/master
Implementing AmicableNumber and VampireNumbers in the Math class
2 parents 63b2ec9 + 082f104 commit 4deae1e

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

Maths/AmicableNumber.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

Maths/VampireNumber.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package Maths;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
n number theory, a vampire number (or true vampire number) is a composite natural number with an even number of digits,
9+
that can be factored into two natural numbers each with half as many digits as the original number
10+
and not both with trailing zeroes, where the two factors contain precisely
11+
all the digits of the original number, in any order, counting multiplicity.
12+
The first vampire number is 1260 = 21 × 60.
13+
* *
14+
* <p>
15+
* * link: https://en.wikipedia.org/wiki/Vampire_number
16+
* * </p>
17+
* <p>
18+
*
19+
*/
20+
21+
22+
23+
24+
25+
26+
27+
public class VampireNumber {
28+
29+
public static void main(String[] args) {
30+
31+
test(10,1000);
32+
}
33+
34+
static void test(int startValue, int stopValue) {
35+
int countofRes = 1;
36+
StringBuilder res = new StringBuilder();
37+
38+
39+
for (int i = startValue; i <= stopValue; i++) {
40+
for (int j = i; j <= stopValue; j++) {
41+
// System.out.println(i+ " "+ j);
42+
if (isVampireNumber(i, j,true)) {
43+
countofRes++;
44+
res.append("" + countofRes + ": = ( " + i + "," + j + " = " + i*j + ")" + "\n");
45+
}
46+
}
47+
}
48+
System.out.println(res);
49+
}
50+
51+
52+
53+
54+
static boolean isVampireNumber(int a, int b, boolean noPseudoVamireNumbers ) {
55+
56+
// this is for pseudoVampireNumbers pseudovampire number need not be of length n/2 digits for example
57+
// 126 = 6 x 21
58+
if (noPseudoVamireNumbers) {
59+
if (a * 10 <= b || b * 10 <= a) {
60+
return false;
61+
}
62+
}
63+
64+
String mulDigits = splitIntoDigits(a*b,0);
65+
String faktorDigits = splitIntoDigits(a,b);
66+
67+
return mulDigits.equals(faktorDigits);
68+
}
69+
70+
71+
72+
// methode to Split the numbers to Digits
73+
static String splitIntoDigits(int num, int num2) {
74+
75+
StringBuilder res = new StringBuilder();
76+
77+
ArrayList<Integer> digits = new ArrayList<>();
78+
while (num > 0) {
79+
digits.add(num%10);
80+
num /= 10;
81+
}
82+
while (num2 > 0) {
83+
digits.add(num2%10);
84+
num2/= 10;
85+
}
86+
Collections.sort(digits);
87+
for (int i : digits) {
88+
res.append(i);
89+
}
90+
91+
92+
return res.toString();
93+
}
94+
}

0 commit comments

Comments
 (0)