# Modified Kaprekar Numbers

# Modified Kaprekar Numbers

+ 40 comments I failed case 6 several times before identifying the problem in my code. My problem was due to a mix use of int and long long. I thought I only needed int for the number being check and long long for its square. Turned out it was risky without careful casting, because when you assign the multiplication of two int's to a long long variable when the multiplication exceeds the int limit, the long long variable only gets the chunked portion available in the int bits. Hope this somehow contributes to the troubleshooting options.

+ 23 comments ### Java solution

From my HackerRank solutions.

It seems like the brute-force solution is the best we can do.

### Tips to avoid Integer Overflow

If you multiply an

*int*with another*int*, it may cause integer overflow. You can't simply sayint num = 99999; long squared = num * num; // causes integer overflow

since that may cause integer overflow. You need a typecast to convert the

*int*to a*long***before**doing the multiplication.int num = 999999; long squared = (long) num * num;

Alternatively, you can just store everything as a

*long*long num = 999999; long squared = num * num;

Also, if there are no Kaprekar numbers, you have to print "INVALID RANGE" exactly as I typed it (without the quotes).

### Working Code - passes 100% of test cases

import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int p = scan.nextInt(); int q = scan.nextInt(); scan.close(); boolean foundKaprekar = false; for (int num = p; num <= q; num++) { if (isKaprekar(num)) { foundKaprekar = true; } } if (!foundKaprekar) { System.out.println("INVALID RANGE"); } } private static boolean isKaprekar(int num) { long squared = (long) num * num; String str = String.valueOf(squared); String left = str.substring(0, str.length() / 2); String right = str.substring(str.length() / 2); int numL = (left.isEmpty()) ? 0 : Integer.parseInt(left); int numR = (right.isEmpty()) ? 0 : Integer.parseInt(right); if (numL + numR == num) { System.out.print(num + " "); return true; } else { return false; } } }

Let me know if you have any questions.

+ 10 comments here is my code in C language. and I think

int main() { long int n,m,r,f; int flag = 0; scanf("%ld\n%ld",&n,&m); for(long int i=n;i<=m;i++){ long int sum; long int a = 0; long int k = i; while(k>0){ k/=10 ; ++a; } long int p = i * i; long int g = pow(10,a); sum = p%g + p/g; if(sum == i){ printf("%ld ",sum); flag++; } } if(flag == 0) printf("INVALID RANGE"); return 0; }

it's very easy to understand for c programmers.

+ 4 comments Why is 10 not a Kaprekar number? or I am missing something here

+ 2 comments # Simple and easy to undersatand

Python code-:

count = 0 for i in range(int(input()),int(input())+1): s = str(i**2) x = s[len(s)//2:] y = s[0:len(s)//2] if x=='':x=0 if y=='':y=0 if int(x)+int(y)==i:print(i,end=' ');count+=1 if count==0:print("INVALID RANGE")

Sort 857 Discussions, By:

Please Login in order to post a comment