Merge pull request #1411 from shellhub/dev

update armstrong number and add NumberOfDigits
This commit is contained in:
Du Yuanchao 2020-08-14 08:19:58 +08:00 committed by GitHub
commit 1eb9dcb0e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 32 deletions

View File

@ -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)

62
Maths/NumberOfDigits.java Normal file
View File

@ -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);
}
}

View File

@ -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());
}
}