diff --git a/DIRECTORY.md b/DIRECTORY.md index f0981af0..43241663 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -127,6 +127,7 @@ * [GCDRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/GCDRecursion.java) * [MaxValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MaxValue.java) * [MinValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MinValue.java) + * [NumberOfDigits](https://github.com/TheAlgorithms/Java/blob/master/Maths/NumberOfDigits.java) * [PalindromeNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PalindromeNumber.java) * [ParseInteger](https://github.com/TheAlgorithms/Java/blob/master/Maths/ParseInteger.java) * [PerfectNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PerfectNumber.java) diff --git a/Maths/NumberOfDigits.java b/Maths/NumberOfDigits.java new file mode 100644 index 00000000..9e760727 --- /dev/null +++ b/Maths/NumberOfDigits.java @@ -0,0 +1,62 @@ +package Maths; + +/** + * Find the number of digits in a number. + */ +public class NumberOfDigits { + public static void main(String[] args) { + int[] numbers = {0, 12, 123, 1234, -12345, 123456, 1234567, 12345678, 123456789}; + for (int i = 0; i < numbers.length; ++i) { + assert numberOfDigits(numbers[i]) == i + 1; + assert numberOfDigitsFast(numbers[i]) == i + 1; + assert numberOfDigitsFaster(numbers[i]) == i + 1; + assert numberOfDigitsRecursion(numbers[i]) == i + 1; + } + } + + /** + * Find the number of digits in a number. + * + * @param number number to find + * @return number of digits of given number + */ + private static int numberOfDigits(int number) { + int digits = 0; + do { + digits++; + number /= 10; + } while (number != 0); + return digits; + } + + /** + * Find the number of digits in a number fast version. + * + * @param number number to find + * @return number of digits of given number + */ + private static int numberOfDigitsFast(int number) { + return number == 0 ? 1 : (int) Math.floor(Math.log10(Math.abs(number)) + 1); + } + + + /** + * Find the number of digits in a number faster version. + * + * @param number number to find + * @return number of digits of given number + */ + private static int numberOfDigitsFaster(int number) { + return number < 0 ? (-number + "").length() : (number + "").length(); + } + + /** + * Find the number of digits in a number using recursion. + * + * @param number number to find + * @return number of digits of given number + */ + private static int numberOfDigitsRecursion(int number) { + return number / 10 == 0 ? 1 : 1 + numberOfDigitsRecursion(number / 10); + } +} diff --git a/Others/Armstrong.java b/Others/Armstrong.java index bfdde2b0..10999328 100644 --- a/Others/Armstrong.java +++ b/Others/Armstrong.java @@ -1,49 +1,43 @@ package Others; -import java.util.Scanner; - /** - * A utility to check if a given number is armstrong or not. Armstrong number is - * a number that is equal to the sum of cubes of its digits for example 0, 1, - * 153, 370, 371, 407 etc. For example 153 = 1^3 + 5^3 +3^3 - * - * @author mani manasa mylavarapu + * An Armstrong number is equal to the sum of the cubes of its digits. + * For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370. + * An Armstrong number is often called Narcissistic number. */ public class Armstrong { - static Scanner scan; public static void main(String[] args) { - scan = new Scanner(System.in); - int n = inputInt("please enter the number"); - boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n); - if (isArmstrong) { - System.out.println("the number is armstrong"); - } else { - System.out.println("the number is not armstrong"); - } + assert (isArmStrong(0)); + assert (isArmStrong(1)); + assert (isArmStrong(153)); + assert (isArmStrong(1634)); + assert (isArmStrong(371)); + assert (!isArmStrong(200)); } /** - * Checks whether a given number is an armstrong number or not. Armstrong - * number is a number that is equal to the sum of cubes of its digits for - * example 0, 1, 153, 370, 371, 407 etc. + * Checks whether a given number is an armstrong number or not. * - * @param number - * @return boolean + * @param number number to check + * @return {@code true} if given number is armstrong number, {@code false} otherwise */ - public static boolean checkIfANumberIsAmstrongOrNot(int number) { - int remainder, sum = 0, temp = 0; - temp = number; + private static boolean isArmStrong(int number) { + int sum = 0; + int temp = number; + int numberOfDigits = 0; + while (temp != 0) { + numberOfDigits++; + temp /= 10; + } + temp = number; /* copy number again */ while (number > 0) { - remainder = number % 10; - sum = sum + (remainder * remainder * remainder); - number = number / 10; + int remainder = number % 10; + int power = 1; + for (int i = 1; i <= numberOfDigits; power *= remainder, ++i) ; + sum = sum + power; + number /= 10; } return sum == temp; } - - private static int inputInt(String string) { - System.out.print(string); - return Integer.parseInt(scan.nextLine()); - } }