/**
* This class defines methods to identify and display special properties of numbers within a specified range.
* It checks for properties such as Harshad, Happy, Munchausen, and Mersenne numbers.
*/
public class Main {
//TODO(Done): add "@author"-tag in class comment
/**
* @author- Pallavi and Azam
*/
//TODO(Done): these two values should be final values in the main-method
public static void main
(String[] args
) { final int START = -5;
final int END = 20;
System.
out.
printf("Testing numbers from %d to %d:\n", START, END
); int displayWidth = calcDisplayWidth(END);
for (int i = START; i <= END; i++) {
boolean hasProf = false;
StringBuilder properties = new StringBuilder();
if (isHappy(i)) {
properties.append("happy ");
hasProf = true;
} else {
properties.append(" ");
}
if (isHarshad(i)) {
properties.append("harshad ");
hasProf = true;
} else {
properties.append(" ");
}
if (isMunchausen(i)) {
properties.append("munchausen ");
hasProf = true;
} else {
properties.append(" ");
}
if (isMersenne(i))
{
properties.append("mersenne ");
hasProf = true;
}
else
{
properties.append(" ");
}
if (hasProf){
//TODO(Done): output has to be formatted like in the example in the assignment
System.
out.
printf("%" + displayWidth
+ "d is a %s number (%s)\n", i,properties.
toString(), toBinaryString
(i
)); }
}
}
/**
* Calculates the display width needed for the largest number in a range.
*
* @param num The number for which display width is calculated.
* @return The number of digits in the largest number.
*/
public static int calcDisplayWidth(int num) {
int length = 1;
while (num >= 10) {
num = num / 10;
length++;
}
return length;
}
/**
* Determines if a number is Happy.
* A number is called happy if it leads to 1 after a sequence of steps wherein each step number
* is replaced by the sum of the squares of its digit.
*
* @param n The number to check.
* @return true if the number is Happy, false otherwise.
*/
public static boolean isHappy(int n) {
if (n <= 0)
return false;
//TODO(Done): end the loop when current value is 4
while (n != 1 && n != 4) {
n = getNextHappy(n);
}
return n == 1;
}
/**
* Checks if a number is a Harshad number.
* A Harshad number is divisible by the sum of its digits.
* @param n The number to check.
* @return true if the number is a Harshad number, false otherwise.
*/
public static boolean isHarshad(int n) {
if (n <= 0)
return false;
int sum = getDigitSum(n);
return n % sum == 0;
}
/**
* Checks if a number is a Munchausen number.
* A Munchausen number is such that the sum of its own digits each raised to the power of themselves equals the number itself.
*
* @param n The number to check.
* @return true if the number is a Munchausen number, false otherwise.
*/
public static boolean isMunchausen(int n) {
if (n < 0)
return false;
if (n == 0)
return true; // Special case for 0 as a Munchausen number
int sum = 0 ;
int copy = n;
while (copy > 0) {
int digit = copy % 10;
sum = sum + powerBySelf(digit);
copy = copy/10;
}
return sum == n;
}
/**
* Checks if a number is a Mersenne number.
* A Mersenne number is in the form of 2^n - 1 where n is an integer.
*
* @param n The number to check.
* @return true if the number is a Mersenne number, false otherwise.
*/
public static boolean isMersenne(int n) {
if (n == -1)
return true; // Special case for -1 as a Mersenne number
if (n <= 0)
return false; // Mersenne numbers are positive, -1 is an exceptional handled case
//TODO(Done) : simplify the code
int num = n + 1;
// Check if num is a power of two using bit manipulation
return (num & (num - 1)) == 0;
}
/**
* Converts an integer to a binary string prefixed with "0b".
*
* @param number The number to convert.
* @return The binary string representation of the number.
*/
public static String toBinaryString
(int number
) { if (number == 0) return "0b0";
StringBuilder binary = new StringBuilder();
while (number != 0) { // extract each bit from the number and build the binary string
int bit = number & 1; // the Least significant bit of 1 is always 1
binary.insert(0, bit); //bits are being extracted from LSB to MSB,(to be assembled in the binary string)
number >>>= 1; //The number is shifted right by one position
}
return "0b" + binary.toString();
}
/**
* Calculates the sum of the digits of a number.
*
* @param n The number whose digits are summed.
* @return The sum of the digits.
*/
private static int getDigitSum(int n) {
int sum = 0;
while (n > 0) {
sum += (n % 10);
n /= 10;
}
return sum;
}
/**
* Raises a digit to the power of itself.
*
* @param n The digit to raise.
* @return The digit raised to the power of itself.
*/
private static int powerBySelf(int n) {
if (n == 0) return 1; // Special case to handle 0^0 as 1
int result = 1;
for (int i = 0; i < n; i++) {
result *= n;
}
return result;
}
/**
* Computes the next value in the sequence used to determine if a number is Happy.
*
* @param n The current number in the sequence.
* @return The next number in the sequence.
*/
private static int getNextHappy(int n) {
int totalSum = 0;
while (n > 0) {
int digit = n % 10;
totalSum += (digit * digit);
n /= 10;
}
return totalSum;
}
}