We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Modified Kaprekar Numbers
Modified Kaprekar Numbers
+ 0 comments My Java Solution
public static void kaprekarNumbers(int p, int q) { // Write your code here boolean Found=false; for(int i=p;i<=q;i++){ int temp=i; String str=Integer.toString(temp); int len=str.length(); BigInteger I=new BigInteger(str); BigInteger N=I.multiply(I); String str2=N.toString(); int len2=str2.length(); if(len2==1){ if(N.equals(I)){ System.out.print(i+" "); Found=true; } }else{ int num1=Integer.parseInt(str2.substring(0, len2-len)); int num2=Integer.parseInt(str2.substring(len2-len, len2)); if((num1+num2)==i){ System.out.print(i+" "); Found=true; } }} if(!Found){ System.out.println("INVALID RANGE"); } }
+ 0 comments Java Solution
public static void kaprekarNumbers(int p, int q) { // Write your code here int flag=0; for(long i=p;i<=q;i++){ if(i==1){ System.out.print(1+" "); flag=1; } long sq= i*i; String iVal=String.valueOf(i); long ilen=iVal.length(); String sqVal=String.valueOf(sq); long sqlen=sqVal.length(); if(sqlen==1){ continue; } String rt=sqVal.substring(sqVal.length()-(int)ilen, sqVal.length()); String lt=sqVal.substring(0, sqVal.length()-(int) ilen); long sm=Long.valueOf(rt)+Long.valueOf(lt); if(sm==i){ System.out.print(i+" "); flag=1; } } if(flag==0){ System.out.println("INVALID RANGE"); } }
+ 0 comments Python3 solution
def kaprekarNumbers(p, q): nums = [] for i in range(p,q+1): d, sq = len(str(i)) ,str(i**2) if i ==1 or(len(str(i*i))> d and i == int(sq[-d:])+int(sq[:-d]) and int(sq[d:])!=0): nums.append(i) if len(nums) == 0: print('INVALID RANGE') else: print(*nums, end="")
+ 0 comments in python3;
def kaprekarNumbers(p, q): lst = [] for i in range(p, q+1): sq = i**2 d = len(str(i)) r = str(sq)[-d:] l = str(sq)[:-d] if l == '': l = 0 if int(l) + int(r) == i: lst.append(i) if len(lst) == 0: print('INVALID RANGE') else: print(*lst)
+ 0 comments i dont know whats wrong with my code?
public static void kaprekarNumbers(int p, int q) { // Write your code here for (int i = p; i < q; i++) { if (checkResult(i)) System.out.print(i + " "); } } private static boolean checkResult(int n) { int temp = n; n = n * n; int c = count(n); int length = c / 2; // System.out.println(length); String r = ""; while (length > 0) { int d = n % 10; r = d + r; n = n / 10; length--; } int res = Integer.parseInt(r) + n; if (res == temp) return true; else return false; } private static int count(int n) { int c = 0; do { c++; n = n / 10; } while (n != 0); return c; }
Load more conversations
Sort 917 Discussions, By:
Please Login in order to post a comment