Merge pull request #1411 from shellhub/dev
update armstrong number and add NumberOfDigits
This commit is contained in:
commit
1eb9dcb0e6
@ -127,6 +127,7 @@
|
|||||||
* [GCDRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/GCDRecursion.java)
|
* [GCDRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/GCDRecursion.java)
|
||||||
* [MaxValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MaxValue.java)
|
* [MaxValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MaxValue.java)
|
||||||
* [MinValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MinValue.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)
|
* [PalindromeNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PalindromeNumber.java)
|
||||||
* [ParseInteger](https://github.com/TheAlgorithms/Java/blob/master/Maths/ParseInteger.java)
|
* [ParseInteger](https://github.com/TheAlgorithms/Java/blob/master/Maths/ParseInteger.java)
|
||||||
* [PerfectNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PerfectNumber.java)
|
* [PerfectNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PerfectNumber.java)
|
||||||
|
62
Maths/NumberOfDigits.java
Normal file
62
Maths/NumberOfDigits.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,49 +1,43 @@
|
|||||||
package Others;
|
package Others;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A utility to check if a given number is armstrong or not. Armstrong number is
|
* An Armstrong number is equal to the sum of the cubes of its digits.
|
||||||
* a number that is equal to the sum of cubes of its digits for example 0, 1,
|
* For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
|
||||||
* 153, 370, 371, 407 etc. For example 153 = 1^3 + 5^3 +3^3
|
* An Armstrong number is often called Narcissistic number.
|
||||||
*
|
|
||||||
* @author mani manasa mylavarapu
|
|
||||||
*/
|
*/
|
||||||
public class Armstrong {
|
public class Armstrong {
|
||||||
static Scanner scan;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
scan = new Scanner(System.in);
|
assert (isArmStrong(0));
|
||||||
int n = inputInt("please enter the number");
|
assert (isArmStrong(1));
|
||||||
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
|
assert (isArmStrong(153));
|
||||||
if (isArmstrong) {
|
assert (isArmStrong(1634));
|
||||||
System.out.println("the number is armstrong");
|
assert (isArmStrong(371));
|
||||||
} else {
|
assert (!isArmStrong(200));
|
||||||
System.out.println("the number is not armstrong");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether a given number is an armstrong number or not. Armstrong
|
* Checks whether a given number is an armstrong number or not.
|
||||||
* number is a number that is equal to the sum of cubes of its digits for
|
|
||||||
* example 0, 1, 153, 370, 371, 407 etc.
|
|
||||||
*
|
*
|
||||||
* @param number
|
* @param number number to check
|
||||||
* @return boolean
|
* @return {@code true} if given number is armstrong number, {@code false} otherwise
|
||||||
*/
|
*/
|
||||||
public static boolean checkIfANumberIsAmstrongOrNot(int number) {
|
private static boolean isArmStrong(int number) {
|
||||||
int remainder, sum = 0, temp = 0;
|
int sum = 0;
|
||||||
temp = number;
|
int temp = number;
|
||||||
|
int numberOfDigits = 0;
|
||||||
|
while (temp != 0) {
|
||||||
|
numberOfDigits++;
|
||||||
|
temp /= 10;
|
||||||
|
}
|
||||||
|
temp = number; /* copy number again */
|
||||||
while (number > 0) {
|
while (number > 0) {
|
||||||
remainder = number % 10;
|
int remainder = number % 10;
|
||||||
sum = sum + (remainder * remainder * remainder);
|
int power = 1;
|
||||||
number = number / 10;
|
for (int i = 1; i <= numberOfDigits; power *= remainder, ++i) ;
|
||||||
|
sum = sum + power;
|
||||||
|
number /= 10;
|
||||||
}
|
}
|
||||||
return sum == temp;
|
return sum == temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int inputInt(String string) {
|
|
||||||
System.out.print(string);
|
|
||||||
return Integer.parseInt(scan.nextLine());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user