Afters a short text on how Java intersect with Math it is time for something more mathematical. I am going to present to you a few types of numbers that you can find all around, and I bet that you never thought that they may even exist.
Let’s start with the Armstrong numbers.
Number Types
Armstrong Number
In general, an Armstrong number (or narcissistic number) is a number n in a given number base b (number of unique digits used to represent the number – the most common base currently is the decimal system) which can be expressed as a sum of its digits raised to the power of m.
Or in a more mathematical fashion:
Armstrong number: n = a1m + a2m + … + amm
- a – Natural number
- ai – Single digit from a
- m – Number of digits in a
We can also express it as a function:
- b – Number base
- n – Natural number
- k – The number of digits inside n in the given base b
It is calculated from the following equation:
k = floor(logb(n)) + 1
di is calculated from:
Then the number is an Armstrong number if F(n) = n. The simplest example of an Armstrong number other than 1 or 2-digit numbers is 153, used in many articles as an example – but here I would like to use a less common number (though still with 3 digits): let’s use 371.
Start with a basic equation.
Now let’s check if we get the same result via the usage of the function.
Types of Armstrong Numbers
We can differentiate four basic types of Armstrong numbers:
- Trivial – Where n fulfills the condition 0 < n < b for any base b. For example, 2 in the 10th base as 21 = 2 and 0 < 2 < 10.
- Non-trivial – Where n fulfills the condition n > b for any base b. Basically, all Armstrong numbers in the given b other than trivial ones. For example, the previously used 371 in the 10th base as 371 > 10 and we know that 371 is an Armstrong number.
- Sociable – A periodic point for F, for positive integer p (so the p-th element of F values set) and forms a cycle of period p. Each Armstrong number is a sociable narcissistic number with p = 1.
- Amicable – A sociable narcissistic number with p = 2.
Verifying if a Number Is Armstrong Number
Here I will implement a very short method to verify if a particular number is an Armstrong number.
public boolean isArmstrong(int n) {
int sum = 0;
int tmp = n;
int length = Integer.toString(n).length();
while (tmp > 0) {
sum += Math.pow(tmp % 10, length);
tmp /= 10;
}
return sum == n;
}
- First, I get the
length
of a number – to know the power to which I will have to raise all the digits within our number. - Then, I am getting the current last digit of the
tmp
number. - On the next line, I am rising the last digit to the power of
n
length. - Afterward, I am adding the value to the
sum
variable – representing the sum of all digits raised to the power of numbern
length. - In line 6, I am dividing the
tmp
variable by10
to effectively drop the last digit of the currenttmp
value. - Such a
while
loop guarantees that the program will iterate over all the digits from numbern
. - Lastly, I am returning the result of comparing values of
sum
andn
variables.
This algorithm assumes a decimal-based representation of numbers so it will fail for other number systems like Base-5 or Base-2.
Abundant Number
It is a positive integer whose aliquot sum – the sum of all divisors except the number itself – is greater than the original number. The value by which the original number is exceeded is called abundance.
In more mathematical terms: s(n) > n where s(n) is the aliquot sum function for number n.
For example, 12 is an abundant number as 1 + 2 + 3 + 4 + 6 = 16.
The abundance for 12 is 4 as 16 – 12 = 4.
Verifying if a Number Is an Abundant Number
public static boolean isAbundant(int n) {
int aliquotSum = aliquotSum(n);
return aliquotSum > n;
}
It is a one-line function. I am just comparing the aliquotSum
of n
with the initial n
value. Nothing more nothing less just a quick comparison comparison.
Amenable Number
It is a positive integer n for which there exists a list (with repetitions) of integers with size equal to the number value, that both sum to the n and when multiplied also return n. Numbers in the list do not have to be positive.
In more mathematical terms, assume n is defined by n = 4k + 1; then, the set can be expressed as 2k (+1)s, 2k (+1)s, and, n itself
For example, 5 can be expressed as a list (1 + (-1) + 1 + (-1) + 5).
Amicable Number
The term describes a pair of numbers related in such a way that their aliquot sums are equal to each other. In more mathematical terms, assume pair (a, b): the pair is amicable when s(a) = b & s(b) = a.
The first such pair is (220, 284) as divisors of 220 (1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110) sum to 284, and the divisors of 284 (1, 2, 4, 71, 142) sum to 220.
Verifying if a Number Is an Amicable Number
public static boolean isAmicable(int a, int b) {
int aliquotSumA = aliquotSum(a);
int aliquotSumB = aliquotSum(b);
return aliquotSumA == b && aliquotSumB == a;
}
I am calculating aliquot sums for a
and b
and then comparing them respectively – the aliquot sum of b
with a
and the aliquot sum of a
with b
.
Perfect Number
It is a positive integer n which is equal to its aliquot sum. It is the opposite of an abundant number whose aliquot sum is higher than the number itself. The concept is quite ancient at this point, as the first mentions of perfect numbers appear in Euclid’s Elements published around 300 BC.
In the form of the equation, the perfect number definition looks more or less like this:
- s(n) = n where s(n) is the aliquot sum function.
An example of a perfect number is 6 as 1 + 2 + 3 = 6.
Verifying if a Number Is a Perfect Number
public static boolean isPerfect(int n) {
int aliquotSum = aliquotSum(n);
return aliquotSum == n;
}
Similar to abundant numbers, it is a one-line function. I am just comparing the aliquotSum
of n
with the initial n
value. Nothing more nothing less just a quick comparison comparison.
Practical Number
The positive integer n whose distinct divisors can be used to create any smaller positive integers. The first work related to practical numbers is Fibonacci’s Liber Abaci; however, he is not the one who coined the term practical number. The term was created by Srinivasan, A. K., in his paper titled “Practical Numbers“.
12 is also an excellent example here as all the numbers from 1 to 11 can be expressed with the usage of 12 divisors – 1, 2, 3, 4, 6:
- 5 = 4 + 1
- 7 = 4 + 3
- 8 = 6 + 2
- 9 = 6 + 3
- 10 = 6 + 4
- 11 = 6 + 3 + 2
Parasitic Number
It is an integer whose decimal representation will make a right circular shift when multiplied by n where n is a single-digit integer. In other words, it means that the last digit of its decimal representation will be moved to the first place. However, the common convention does not allow leading zeros to be used.
Depending on the value of n, we call particular numbers n-parasitic numbers.
Examples:
- 128205 is a 4-parasitic number as 4 × 128205 = 512820
- 142857 is a 5-parasitic number as 5 × 142857 = 714285
- 025641 is not a 4-parasitic number even though 4 × 25641 = 102564
Untouchable Number
It is an integer that is not equal to the sum of all divisors of any positive integers. In other words, such numbers will never be returned by the aliquot sum function called for any number (including the number itself). Their history is quite old as they first appear around 1000 years ago in the works of the Arabic mathematician Abu Mansur al-Baghdadi.
Examples of such numbers are 5, 52, 88, and many others.
We can write 5 as 4 + 1 or 2 + 3.
We will have to find a number whose divisors are 2 and 3.
As for 4 + 1 if 4 is the divisor for the number n, then 2 is also a divisor of n and the equality does not hold because 2 + 4 + 1 is not 5.
Sociable Number
The term describes a number whose aliquot sums create a specific period sequence. Namely, a period sequence where the aliquot sum of the previous number is the value of the next number.
Sociable numbers are the generalization of perfect numbers and amicable numbers. When the period of sequence is equal to 1, then the number is a perfect number. On the other hand, if the period number is equal to 2, the two numbers in the period form an amicable pair.
Additionally, if the period is n, the number is called sociable of order n.
OEIS and Number Types
OEIS is an abbreviation for On-Line Encyclopedia of Integer Sequences, an online database of integer sequences founded by Neil Sloane in 1964. As of January 2022, it contains over 350,000 sequences and is the largest database of its kind.
The reason for mentioning it here is that all of the number types mentioned above are described there in their own entries.
For example:
- A005188 describes the sequence of Base-10 Armstrong numbers.
- A005101 describes the sequence of abundant numbers.
- A100832 describes the amenable numbers.
- A259180 describes the sequence of amicable numbers.
- A000396 describes the sequence of perfect numbers.
- A005153 describes the sequence of practical numbers.
- A092697 describes the sequence of parasitic numbers.
- A005114 describes the sequence of untouchable numbers.
- A090615 describes the smallest member of sociable quadruples.
LeetCode and Number Types
Some of the numbers from this list are the ideas behind LeetCode tasks, namely the perfect numbers and Armstrong numbers (premium), at least as far as I was able to check.
If you find more tasks like these, do not hesitate to let me know in the comments.
Conclusion
It was quite a ride over more and more strange types of numbers present in the world around us. Personally, I would never have thought that they even may exist, but mathematics surprised me. How about you?
What is more, as you can see, 12 is a really useful number – the base 12 system was even one of the first proper number systems in history – and it appears in many mathematical contexts.
I hope that I showed you something new and exciting.
Thank you for your time.
Comments are closed.