Wednesday, February 11, 2015

Find Armstrong Number

I thought of sharing this little question with you all. This is very simple question, but which will put your analytical and technical approach in test.
First place Amstrong number , for those who have never heard this, it's a very wonderful arithmetic art or classic we observe with base ten (we are normally using our number is base 10 format,it can be any format - this is also known as Narcissistic Number).

Amstrong number is any number , where sum of its own digits raised to the power of number of digits.

eg : If we take three digit number, Amstrong number , where sum of cubic value of each digit number equals to the number.
If get a number xyz then ,
 eg:  xyz = x^3 + y^3 + z^3

For further explain, let's take Amstrong number 153

153 = 1^3 + 5^3 + 3^3 = 1 +125 +27 = 153

See how nice  is that.

We continue example with 3 digit number, and extend the program for any number.

So it's very simple arithmetic operation to check whether a number is Armstrong ,
1. break number in to digit
2. get the sum of  cubic  value of each number.

So when come to the  implementation of the logic step 1 is where you will be tested, break the number in to digits. Obviously step 2 has very little where  getting sum of each cubic value- very simple and straight forwards writing of same arithmetic operation in any language.

How to break the number in to digits.

You can come up with various solutions. Convert to String and make substrings of size one, convert to char array , but any operation relate with strings and characters will have high cost and some implementation logic.

But we can use division and modulus operator to achieve this. Below code snippet will show you the method to check whether number is Armstrong.

public class ArmstrongNumber {

/*
* @return true if number is Armstrong number or return false
*/
// how to create the logic.
// we need to get the each digit of the number one by one
// get the cubic value, value to the power three
// and get the sum of all cubic value
//
// how to get digit by digit
// we can have so many approaches
// simple arithmetic way is to use modulus to break to digits
// and division to break the number exclusive of modulus , like taking
// substring except last character
public static boolean isArmStrong(int number) {
int result = 0;
int orig = number;
while (number != 0) {
int remainder = number % 10;
result = result + remainder * remainder * remainder;
number = number / 10;
}
// number is Armstrong return true
if (orig == result) {
return true;
}

return false;
}

public static void main(String[] args) {
int number = 153;
System.out.println("Number " + number + " is Amstrong ="
+ isArmStrong(number));

}

}


----------------

If I am going to explain further

Modulus operator is used to break the number in to digits. In base ten , if we need to break number in to digits all we need to do is , get the modulus by ten where you can get the last number.
So from modulus we can the last number.
Now problem , how to break the remaining part and continue to get the last digit.

int remainder = number % 10;

It's very simple, as same as getting the modulus of ten to get the last digit, if we get the division value of that number from 10, then we get the remaining part of the number(two digits to right side) - which is obviously to the right side of the number.

number = number / 10;

Two simple operation to break then number.
We continue to replace the number with division value itself, until number becomes zero - where no number to divide (number has been reduced to one digit)

while (number != 0)


Same arithmetic logic can be used to check whether number in Palindrome and reverse a given number.


So above program is designed to only to 3 digits number, but the concept has been implemented. Only thing is to get the power to number of digits.
So how we can find the number of digits in any number.
We have mathematical solution.

If we take any number any base, we take the log value of the number in the same base we get one less of the number of digits in the number.

So as we discussed for base ten, we get the number of digits as, or in simple length of the number.

int length = (int)(Math.log10(n)+1);

taking the power of number , you can few ways

Math.pow(remainder, length); // where length is the power - this takes double values as input and returns double

BigInteger.valueOf(3).pow(length)  // BigInteger has pow() method which needs and instance.

any way I have done the example using "int" values and code looks like below

-----------------------------------------------------------------------

public static boolean isArmStrong(int number) {
int result = 0;
int orig = number;
int length = (int)(Math.log10(number)+1);
while (number != 0) {
int remainder = number % 10;
result = result + (int)Math.pow(remainder, length);
number = number / 10;
}
// number is Armstrong return true
if (orig == result) {
return true;
}

return false;
}

-------------------------------------------------------------------------




No comments:

Post a Comment