Dudeney Number Checker in Java
Dudeney Number Checker in Java
The Java programs calculate the LCM using the formula LCM(a, b) = (a / GCD(a, b)) * b, where GCD is the greatest common divisor. Both methods rely on finding the GCD via the Euclidean algorithm recursively. The LCM is then found by multiplying one number by the quotient of the other number and the GCD. This approach efficiently computes LCM by leveraging the mathematical relationship between GCD and LCM, though different code variations may use iterative or recursive means for GCD computation .
The program identifies a 'Disarium' number by summing the digits raised to the power of their respective positions. A method countDigit first determines the number of digits. Then, through sumofDigits, the program calculates the sum of each digit powered by its positional index. If the total equals the original number, it is considered a Disarium number, illustrating the mathematical principle of digit power summations .
A 'Dudeney' number is determined when the cube of the sum of its digits equals the original number itself. The program prompts user input to define the number and then recursively sums the digits. If the cube of this sum matches the input number, it is identified as a Dudeney number, reflecting a unique relation between a number's digit sum and its numerical value .
The program verifies an 'Armstrong number' by raising each digit of the number to a power equal to the total number of digits (order) and summing these powered digits. It uses a while loop to extract digits, compute their power using Math.pow, and sum these values. If the sum equals the original number, it is confirmed as an Armstrong number. Such numbers are crucial in error checking algorithms and cryptography due to their unique properties of self-descriptive numbers .
The recursive method in the DeciHex class converts a decimal number to hexadecimal by repeatedly dividing the number by 16 and storing the remainder. Each remainder corresponds to a hexadecimal digit, selected from the array {'0', '1', '2', ..., 'F'}. This recursive process continues until the input number becomes zero. The remainder digits are concatenated in reverse order to form the complete hexadecimal string representation of the original decimal number .
The program checks if a number is a palindrome by reversing the digits through recursive method rev. It constructs the reversed number by recursively removing the last digit and adding it to the reverse multiply by tens until the original number is reduced to zero. Finally, it compares the reversed number with the original to determine if the number is a palindrome (i.e., it reads the same forwards and backwards).
The program finds the HCF using the recursive Euclidean algorithm, where it repeatedly replaces the larger number by the remainder of the division of the larger number by the smaller number until one of the numbers becomes zero. The non-zero number is then the GCD, which is the HCF. This method efficiently computes the greatest common divisor by exploiting the property that GCD of two numbers also divides their difference, maintaining computational simplicity and speed .
In the program, the number 17 is considered an 'Evil Number' because its binary representation contains an even number of 1's. The recursive function converts 17 into a binary form by using base 2 and multiplies partial results by 10 to create a full binary string representation. After obtaining binary 10001, the program counts the 1's; since there are an even number, it classifies 17 as 'Evil' .
A 'Pronic Number' is determined by checking if it can be stated as the product of two consecutive integers, n = x * (x + 1). The program calculates the square root of the number to find potential consecutive integers and checks the pronic condition by evaluating both x(x + 1) and (x - 1)x as potential forms. Pronic numbers are significant because they represent a sequence of integers that are explicitly products of consecutive integers, which have mathematical interest and use in combinatorial designs .
The program checks for a 'Perfect Number' by calculating the sum of its divisors (excluding itself), using a recursive function to accumulate the divisors that evenly divide the number. If this sum equals the original number, it is a perfect number. Perfect numbers are important in number theory; they are closely related to Mersenne primes and have properties that can contribute to encryption algorithms and other computational theories .