Apocalyptic Number in Java
In this article we will discuss what apocalyptic numbers are, Qualities they possess and how it is possible to implement them into code using Java.
Apocalyptic Numbers – or “Doomsday Numbers” as they are more widely termed in some texts on number theory – are a genus of integers with one specific trait. In mathematics, an apocalyptic number is one that contains “666” in its decimal notation. It is the natural phenomenon of establishing numerical events involved in superstitions. In Java or any programming language, a person can identify an apocalyptic number by checking if its decimal representation has the substring “666”.
Although the words “Apocalyptic Numbers” may connote a threatening or darker nature, it is as close to a purely mathematical curiosity as can be. The subject matter of its intriguing landmark relationship to exponential and arithmetic properties captured the minds alike of mathematicians and programmers.
Example 1:
Input: N = 157
Output: 157 is an Apocalyptic Number.
Explanation:
Check:
2^n
2^157= 182687704666362864775460604089535377456991567872
Yes, 157 is an Apocalyptic Number because, on calculating which comes out to be a large number which contains the substring “666”.
Example 2:
Input: N = 10
Output: 10 is not an Apocalyptic Number.
Explanation:
Check:
2^n
2^10= 1024
No, 10 is not an Apocalyptic Number because, on calculating, it does not contain the substring “666”.
Example 3:
Input: N = 110
Output: 110 is not an Apocalyptic Number.
Explanation:
Check:
2^n
2^110= 1298074214633706907132624082305024
No, 110 is not an Apocalyptic Number because, on calculating, it does not contains the substring “666”.
Basic Approach
Identification of apocalyptic numbers by implementation of the program, as it follows from an algorithm is a relatively simple task.
Algorithm
Step 1: Calculate using BigInteger.valueOf(2).pow((int) number).
Step 2: Convert the result to a string (numberString).
Step 3: Initialize a counter (count) to track consecutive occurrences of '6'.
Step 4: Iterate through each character in numberString.
Step 5: If the current character is '6', increment the count.If count becomes 3, return true (substring "666" found). If the current character is not '6', reset count to 0.
Step 6: If the loop completes without finding the substring "666", return false.
Step 7: If true, the number is an apocalyptic number; otherwise, it is not.
FileName: BasicApocalypticNumbers.java
import java.math.BigInteger;
public class BasicApocalypticNumbers {
public static void main(String[] args) {
long number = 157;
BigInteger powerOfTwo = BigInteger.valueOf(2).pow((int) number);
if (contains666(powerOfTwo)) {
System.out.println(number + " is an apocalyptic number.");
} else {
System.out.println(number + " is not an apocalyptic number.");
}
}
private static boolean contains666(BigInteger number) {
String numberString = number.toString();
int count = 0;
for (int i = 0; i < numberString.length(); i++) {
if (numberString.charAt(i) == '6') {
count++;
if (count == 3) {
return true;
}
} else {
count = 0;
}
}
return false;
}
}
Output
157 is an Apocalyptic Number.
Complexity Analysis
Calculating using BigInteger.valueOf(2).pow((int) number) has a time complexity of O(log n), where n is the exponent. Checking for the presence of the substring "666" by iterating through the digits of the BigInteger has a linear time complexity O(log m), where m is the value of . The overall time complexity is O(log n + log m), where n is the exponent and m is the value of . The overall space complexity is O(log m), where m is the value of .
Recursive Approach
Taking the exploration of apocalyptic numbers a step further, one can delve into recursive algorithms to generate a sequence of apocalyptic numbers. By starting with a seed apocalyptic number and iteratively applying the apocalyptic condition, a fascinating sequence of numbers with unique properties can be revealed.
Algorithm
Step 1: Input positive integer
Step 2: If number is 0 return 1 Otherwise, return recursivePowerOfTwo
Step 3: Convert recursivePowerOfTwo(2, number) to a string (numberString).
Step 4: Initialize count to 0.
Step 5: If numberString contains "666", return true. Otherwise, return false.
Step 6: If step 3 returns true, print that the number is an apocalyptic number. Otherwise, print that the number is not an apocalyptic number.
FileName: RecursiveApocalypticNumber.java
import java.math.BigInteger;
public class RecursiveApocalypticNumbers {
public static void main(String[] args) {
long number = 10;
// Calculate 2^10
BigInteger powerOfTwo = recursivePowerOfTwo(BigInteger.valueOf(2), number);
// Check if any digit in the result is '6' three times in a row
if (contains666(powerOfTwo.toString(), 0, 0)) {
System.out.println(number + " is an apocalyptic number.");
} else {
System.out.println(number + " is not an apocalyptic number.");
}
}
// Recursive method to calculate 2^n
private static BigInteger recursivePowerOfTwo(BigInteger base, long exponent) {
if (exponent == 0) {
return BigInteger.ONE;
} else {
return base.multiply(recursivePowerOfTwo(base, exponent - 1));
}
}
// Recursive method to check if the string contains the substring "666"
private static boolean contains666(String numberString, int index, int count) {
if (index == numberString.length()) {
return false;
}
if (numberString.charAt(index) == '6') {
count++;
if (count == 3) {
return true;
}
} else {
count = 0; // Reset the count if a non-'6' digit is encountered
}
return contains666(numberString, index + 1, count);
}
}
Output
10 is not an Apocalyptic Number.
Complexity Analysis
The time and space complexity of recursive solution is same as above basic approach.
Optimization Approach
While the basic and the recursive algorithm provided earlier serves the purpose of identifying apocalyptic numbers, optimization techniques can be employed to enhance efficiency, especially when dealing with larger numbers. For instance, utilizing a more efficient method for counting digits using contains method.
Algorithm
Step 1: Set the value of the variable number to the desired number.
Step 2: Use the BigInteger.valueOf(2).pow((int) number) to calculate , storing the result in the powerOfTwo variable. This involves efficient binary exponentiation.
Step 3: Convert the powerOfTwo BigInteger to a string representation using the toString() method.
Step 4: Check if the string representation contains the substring "666" using the contains("666") method.
Step 5: If the substring "666" is present, output that the number is an apocalyptic number; otherwise, output that it is not an apocalyptic number.
FileName: OptimizedApocalypticNumber.java
import java.math.BigInteger;
public class OptimizedApocalypticNumber {
public static void main(String[] args) {
long number = 157;
// Calculate 2^157
BigInteger powerOfTwo = BigInteger.valueOf(2).pow((int) number);
// Check if the result contains the substring "666"
if (powerOfTwo.toString().contains("666")) {
System.out.println(number + " is an apocalyptic number.");
} else {
System.out.println(number + " is not an apocalyptic number.");
}
}
}
Output
157 is an Apocalyptic Number.
Complexity Analysis
The BigInteger.pow method has a time complexity of approximately O(log n), where 'n' is the exponent (157 in this case). The complexity arises from the binary exponentiation algorithm used internally. Converting the BigInteger result to a string involves iterating over its digits, and the number of digits is proportional to log(), which simplifies to 157. The contains method typically has a linear time complexity in the length of the string. Since the length of the string is proportional to the exponent, the time complexity is O(log n).
Therefore, the overall time complexity of the code is dominated by the calculation of , which is O(log n).
Exploring Apocalyptic Numbers Further
Properties of Apocalyptic Numbers
Exponential Growth: The examples of Apocalyptic Numbers demonstrate a great progression of 2 raises to the amount that decimal representation digits have.
Unique Arithmetic Relationship: The fact that 2n=10y+1 distinguishes apocalyptic numbers as a unique subset of integers is lying in equality.
Apocalyptic Primes
A branch from this family of Apocalyptic numbers that has received further notice is the idea of “Apocalyptic Primes” which are prime numbers considered to be apocalyptic. The problem of apocalyptic primes becomes even more complicated and interesting as these elements are clearly needed to understand the phenomenon.
Applications and Significance
Cryptography:
Although the use of number theory and numbers possessing unique properties (such as the apocalyptic numbers) does not have direct implications in today's cryptography, their study leads to a better understanding of mathematical structures underlying crypto algorithms.
Educational Purposes:
In addition, detecting a pattern of any form makes the apocalyptic number’s study an alluring exercise among the students and others who are interested in number theory or programming. In this approach of creating algorithms to identify some special numbers is rather hands-on in nature; hence, it helps the students learn.
Algorithmic Challenges:
Designing efficient algorithms for finding apocalyptic numbers with particular intricacy in cases of high numerals is an attractive algorithm problem. Achieving performance and scalability optimization in implementing this programming exercise piles an additional level of complexity on the core functionality.v