fork download
  1. /**
  2.  * This class defines methods to identify and display special properties of numbers within a specified range.
  3.  * It checks for properties such as Harshad, Happy, Munchausen, and Mersenne numbers.
  4.  */
  5. public class Main {
  6. //TODO(Done): add "@author"-tag in class comment
  7.  
  8. /**
  9.   * @author- Pallavi and Azam
  10.   */
  11. //TODO(Done): these two values should be final values in the main-method
  12. public static void main(String[] args) {
  13. final int START = -5;
  14. final int END = 20;
  15. System.out.printf("Testing numbers from %d to %d:\n", START, END);
  16. int displayWidth = calcDisplayWidth(END);
  17. for (int i = START; i <= END; i++) {
  18. boolean hasProf = false;
  19. StringBuilder properties = new StringBuilder();
  20. if (isHappy(i)) {
  21. properties.append("happy ");
  22. hasProf = true;
  23. } else {
  24. properties.append(" ");
  25. }
  26. if (isHarshad(i)) {
  27. properties.append("harshad ");
  28. hasProf = true;
  29. } else {
  30. properties.append(" ");
  31. }
  32. if (isMunchausen(i)) {
  33. properties.append("munchausen ");
  34. hasProf = true;
  35. } else {
  36. properties.append(" ");
  37. }
  38. if (isMersenne(i))
  39. {
  40. properties.append("mersenne ");
  41. hasProf = true;
  42. }
  43. else
  44. {
  45. properties.append(" ");
  46. }
  47. if (hasProf){
  48. //TODO(Done): output has to be formatted like in the example in the assignment
  49. System.out.printf("%" + displayWidth + "d is a %s number (%s)\n", i,properties.toString(), toBinaryString(i));
  50. }
  51. }
  52. }
  53.  
  54. /**
  55.   * Calculates the display width needed for the largest number in a range.
  56.   *
  57.   * @param num The number for which display width is calculated.
  58.   * @return The number of digits in the largest number.
  59.   */
  60. public static int calcDisplayWidth(int num) {
  61. int length = 1;
  62. while (num >= 10) {
  63. num = num / 10;
  64. length++;
  65. }
  66. return length;
  67. }
  68. /**
  69.   * Determines if a number is Happy.
  70.   * A number is called happy if it leads to 1 after a sequence of steps wherein each step number
  71.   * is replaced by the sum of the squares of its digit.
  72.   *
  73.   * @param n The number to check.
  74.   * @return true if the number is Happy, false otherwise.
  75.   */
  76. public static boolean isHappy(int n) {
  77. if (n <= 0)
  78. return false;
  79. //TODO(Done): end the loop when current value is 4
  80.  
  81. while (n != 1 && n != 4) {
  82. n = getNextHappy(n);
  83. }
  84.  
  85. return n == 1;
  86. }
  87. /**
  88.   * Checks if a number is a Harshad number.
  89.   * A Harshad number is divisible by the sum of its digits.
  90.   * @param n The number to check.
  91.   * @return true if the number is a Harshad number, false otherwise.
  92.   */
  93. public static boolean isHarshad(int n) {
  94. if (n <= 0)
  95. return false;
  96. int sum = getDigitSum(n);
  97. return n % sum == 0;
  98. }
  99.  
  100. /**
  101.   * Checks if a number is a Munchausen number.
  102.   * A Munchausen number is such that the sum of its own digits each raised to the power of themselves equals the number itself.
  103.   *
  104.   * @param n The number to check.
  105.   * @return true if the number is a Munchausen number, false otherwise.
  106.   */
  107. public static boolean isMunchausen(int n) {
  108. if (n < 0)
  109. return false;
  110. if (n == 0)
  111. return true; // Special case for 0 as a Munchausen number
  112. int sum = 0 ;
  113. int copy = n;
  114. while (copy > 0) {
  115. int digit = copy % 10;
  116. sum = sum + powerBySelf(digit);
  117. copy = copy/10;
  118. }
  119. return sum == n;
  120. }
  121.  
  122. /**
  123.   * Checks if a number is a Mersenne number.
  124.   * A Mersenne number is in the form of 2^n - 1 where n is an integer.
  125.   *
  126.   * @param n The number to check.
  127.   * @return true if the number is a Mersenne number, false otherwise.
  128.   */
  129. public static boolean isMersenne(int n) {
  130. if (n == -1)
  131. return true; // Special case for -1 as a Mersenne number
  132. if (n <= 0)
  133. return false; // Mersenne numbers are positive, -1 is an exceptional handled case
  134. //TODO(Done) : simplify the code
  135. int num = n + 1;
  136. // Check if num is a power of two using bit manipulation
  137. return (num & (num - 1)) == 0;
  138. }
  139.  
  140. /**
  141.   * Converts an integer to a binary string prefixed with "0b".
  142.   *
  143.   * @param number The number to convert.
  144.   * @return The binary string representation of the number.
  145.   */
  146. public static String toBinaryString(int number) {
  147. if (number == 0) return "0b0";
  148.  
  149. StringBuilder binary = new StringBuilder();
  150. while (number != 0) { // extract each bit from the number and build the binary string
  151. int bit = number & 1; // the Least significant bit of 1 is always 1
  152. binary.insert(0, bit); //bits are being extracted from LSB to MSB,(to be assembled in the binary string)
  153. number >>>= 1; //The number is shifted right by one position
  154. }
  155. return "0b" + binary.toString();
  156. }
  157.  
  158.  
  159. /**
  160.   * Calculates the sum of the digits of a number.
  161.   *
  162.   * @param n The number whose digits are summed.
  163.   * @return The sum of the digits.
  164.   */
  165. private static int getDigitSum(int n) {
  166. int sum = 0;
  167. while (n > 0) {
  168. sum += (n % 10);
  169. n /= 10;
  170. }
  171. return sum;
  172. }
  173.  
  174. /**
  175.   * Raises a digit to the power of itself.
  176.   *
  177.   * @param n The digit to raise.
  178.   * @return The digit raised to the power of itself.
  179.   */
  180. private static int powerBySelf(int n) {
  181. if (n == 0) return 1; // Special case to handle 0^0 as 1
  182. int result = 1;
  183. for (int i = 0; i < n; i++) {
  184. result *= n;
  185. }
  186. return result;
  187. }
  188.  
  189. /**
  190.   * Computes the next value in the sequence used to determine if a number is Happy.
  191.   *
  192.   * @param n The current number in the sequence.
  193.   * @return The next number in the sequence.
  194.   */
  195. private static int getNextHappy(int n) {
  196. int totalSum = 0;
  197. while (n > 0) {
  198. int digit = n % 10;
  199. totalSum += (digit * digit);
  200. n /= 10;
  201. }
  202. return totalSum;
  203. }
  204. }
  205.  
Success #stdin #stdout 0.15s 58392KB
stdin
fwp
stdout
Testing numbers from -5 to 20:
-1 is a                          mersenne  number (0b11111111111111111111111111111111)
 0 is a               munchausen           number (0b0)
 1 is a happy harshad munchausen mersenne  number (0b1)
 2 is a       harshad                      number (0b10)
 3 is a       harshad            mersenne  number (0b11)
 4 is a       harshad                      number (0b100)
 5 is a       harshad                      number (0b101)
 6 is a       harshad                      number (0b110)
 7 is a happy harshad            mersenne  number (0b111)
 8 is a       harshad                      number (0b1000)
 9 is a       harshad                      number (0b1001)
10 is a happy harshad                      number (0b1010)
12 is a       harshad                      number (0b1100)
13 is a happy                              number (0b1101)
15 is a                          mersenne  number (0b1111)
18 is a       harshad                      number (0b10010)
19 is a happy                              number (0b10011)
20 is a       harshad                      number (0b10100)