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
Sort by
recency
|
1024 Discussions
|
Please Login in order to post a comment
Here is my c++ solution, you can what the explanation here : https://youtu.be/MttrQCHGu3w
C++ O((q−p+1) log(q)) solution:
My solution in java whitout using any String or StringBuilder method. Passes all test cases.
Python
def kaprekarNumbers(p, q): kap=[] for i in range(p,q+1): sqr=str(i*i) r=sqr[len(sqr)-len(str(i))::] l=round(int(sqr)/(10**len(r)),0) if l+int(r)==i: kap.append(i) if len(kap)!=0: print(*kap, sep=" ") else: print("INVALID RANGE")
Simpl JavaScript solution:
const range = (a, b) => { let arr = []; for (a; a <= b; a++) { arr.push(a) } return arr; };
function kaprekarNumbers(p, q) { // Write your code here const res = [] for(let num of range(p, q)){ console.log(num) let sqr = num*num; let str_sqr = sqr.toString(); let d = num.toString().length; let r = str_sqr.slice(-d); let l = str_sqr.slice(undefined,-d) || '0'; if((parseInt(r) + parseInt(l)) === num){ res.push(num) } }
}