Modified Kaprekar Numbers

I received a message from HackerRank to solve the Modified Kaprekar Numbers challenge. Had to look up on-line what a Kaprekar number is.

My first goal was to generate the actual Kaprekar numbers. Once I was done with that I went after the actual requirements for the challenge.

Following is a screen capture of the console of the Eclipse IDE when running my test code for the challenge:

1
99999
1 9 45 55 99 297 703 999 2223 2728 4950 5050 7272 7777 9999 17344 22222 77778 82656 95121 99999 

The following screen capture shows the comparison in the same range between the actual and the modified Kaprekar numbers:

1
99999
1 9 45 55 99 297 703 999 2223 2728      4950 5050      7272 7777 9999 17344 22222       77778 82656 95121 99999 
1 9 45 55 99 297 703 999 2223 2728 4879 4950 5050 5292 7272 7777 9999 17344 22222 38962 77778 82656 95121 99999

In the modified set some numbers were excluded.

Following is the Java 8 code for my submission:

import java.math.BigInteger;
import java.util.Scanner;

public class Solution {

	/**
	 * Generate and display the MODIFIED kaprekar numbers within the specified range.
	 */
	static void kaprekar(int p, int q) {
		Boolean found = false;
		
		// **** loop once per number in the specified range ****
		
		for (int i = p; i <= q; i++) {

			// **** square the number ****
			
			BigInteger bigI		= new BigInteger(Integer.toString(i));
			BigInteger xSquare 	= bigI.multiply(bigI);

			// **** generate a string representing the squared number ****
			
			String str = xSquare.toString();
		
			// **** prepend 0 (if odd number of characters) ****
			
			if ((str.length() % 2) == 1) {
				str = "0" + str;
			}
					
			// **** split the string ****
			
			int len = str.length();
			String l = str.substring(0, len / 2);
			String r = str.substring(len / 2, len);
				
			// **** convert right string to long ****
			
			long rr = Long.parseLong(r);

			// **** convert left string to long ****
			
			long ll = Long.parseLong(l);
			
			// **** add the numbers ****
			
			long sum = ll + rr;
		
			// **** check if this number is a Kaprekar ****
			
			if (sum == i) {
				System.out.print(i + " ");
				found = true;
			}
		}

		// **** check if Kaprekar number was NOT found ****
		
		if (!found) {
			System.out.println("INVALID RANGE");
		} else {
			System.out.println("");
		}
	}
	
	/**
	 * Test code.
	 */
	public static void main(String[] args) {

		// **** ****
		
		Scanner sc = new Scanner(System.in);
		
		// **** ****
		
		int p = sc.nextInt();
		sc.nextLine();
		int q = sc.nextInt();

		// **** ****
		
		sc.close();
		
		// **** ****
		
		kaprekar(p, q);
	}
}

I typically leave in the test code but in this case I removed it.

In the first passes I used integers. They overflowed. I then switched to long but they had the same fate. I then switched to BigIntegers.

If you have comments or questions regarding this or any other post in this blog, please do not hesitate and send me a message. Will reply as soon as possible and will not use your name unless you explicitly allow me to do so.

Enjoy;

John

john.canessa@gmail.com

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *