Calculate GCD in Java
Calculate GCD in Java
The program determines if a number is a Harshad number by calculating the sum of its digits and then checking if the original number is divisible by this sum without a remainder. This uses the mathematical concept of divisibility: if a number is divisible by the sum of its digits, it is a Harshad number .
The program terminates its loop when it finds a multiple of both input numbers by incrementing the larger of the two numbers until it is divisible by both. This condition ensures it finds the least common multiple (LCM) as it continuously checks using the modulus operator .
The program identifies a 'Magic Number' by iteratively summing its digits until a single digit remains. If this final digit is 1, the number is a Magic Number, otherwise it is not. This checks if repeated applications of summing the digits reduce the number to 1 .
The program reverses a number by repeatedly extracting the last digit of the number using the modulus operator, accumulating these digits into a new variable as its least significant digits, and then dividing the original number by 10 to remove the processed digit. This approach is effective because it directly transforms the number digit by digit into its reverse .
The program relies on the Euclidean algorithm to compute the greatest common divisor (GCD) of two numbers. It iteratively sets one number to the remainder of the division of the two numbers until the remainder becomes zero. The other number at this point is the GCD .
The program uses string conversion to directly compare the ending digits of the square and the original number. This method leverages string operations, which handle leading zeros and varying digit lengths, effectively addressing potential numeric precision issues and manual manipulation errors .
The program verifies if a number is Automorphic by first calculating the square of the number and converting both the number and its square to strings. It checks if the square string ends with the number string, thus confirming the automorphism property .
The program swaps two numbers without using a third variable by employing arithmetic operations. It adds both numbers, stores the result in one, then reconstructs the original values by subtracting from the sum. Specifically, it sets a = a + b, assigns b = a - b, thus b becomes the original a, and then assigns a = a - b, making a the original b .
Identifying the factors of a number is significant for various computational problems, including optimization, cryptography, and algorithm performance evaluation. The program precisely finds all divisors up to the number, establishing a foundation for more complex operations such as prime factorization, which is crucial in encryption algorithms .
The program calculates the sum of digits by using a loop to repeatedly extract the last digit of the number using the modulus operator and adding it to an accumulator. It then truncates the last digit by integer division, effectively iterating over each digit. Its complexity is O(n), where n is the number of digits, since it processes each digit exactly once .