Tag Archives: digits

Testing for a Kaprekar number in Java

Kaprekar numbers for a given base is a non-negative integer, the representation of whose square in that base can be split into two parts that add up to the original number again. For instance, 45 is a Kaprekar number, because 45² = 2025 and 20+25 = 45.

A question related to Kaprekar number was asked in ISC Computer Science Paper 2 (Practical) in 2011. There are numerous solutions floating on the Internet on ‘How to test whether a given positive integer is a Kaprekar number or not. I will advice the students to query Google regarding the same (http://www.google.co.in/search?q=java+isc+kaprekar+number ) to view a few of them before proceeding with this post. Some solutions use String methods in which the number is first converted into string so that length() and substring() function of the String class can be used to facilitate calculation of length and extraction of the required number of digits. Students must understand that String and Math functions though convenient to used are computationally expensive. These functions are considerably slower than regular mathematical operations using the operators provided in Java and their use should be avoided as far as possible. By using the above functions the number of lines in our program may decrease but one must understand that the decrease in number of lines of codes does not translates into increase of performance because these function internally use a lot of code for testing various extreme conditions as well as loops and local variables etc.

Today in this post, I will demonstrate how we can test for a Kaprekar number efficiently and elegantly using only one loop. We will take a variable divisor and initialize it to 1. Then we will take a loop which will iterate for each digit of a loop and then we will multiply our variable divisor by 10 for each digit found in the number. This way if we have one digit in our number our divisor would be 10, if we have two digits in our number our divisor would be 100, if we have three digits in our number our divisor would be 1000 and so on. We will then use this divisor to split the square of the number into two part by calculating the quotient and the remainder of the square of the number divided by the divisor. The code implementing the above logic will be as follows:

class Kaprekar{
    public static boolean isKaprekar(int n){
        int square=n*n, factor=1;
        for(int temp=n;temp>0; temp/=10){
            factor*=10;
        }

        return n==(square/factor)+(square%factor);
    }

    public static void main(String args[]){
        for(int i=1; i<=10000;i++){
            if(isKaprekar(i)) System.out.println(i);
        }
    }
}

The main function in the above program prints all Kaprekar number within the range of 1 and 10,000. As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.