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 Here is my c++ solution, you can what the explanation here : https://youtu.be/MttrQCHGu3w
int getSum(long a){ long sq = a * a; int digit = (int)log10(a) + 1; int result = sq % (int)pow(10, digit); int rest = (int)log10(sq) + 1 - digit; if(rest > 0) result += stoi(to_string(sq).substr(0, rest)); return result; } void kaprekarNumbers(int p, int q) { bool valid_range = false; for(int i = p; i <= q; i++){ int s = getSum(i); if(s == i){ cout << i << " "; valid_range = true; } } if(!valid_range) cout << "INVALID RANGE"; }
+ 0 comments My Javascript solution
function kaprekarNumbers(p, q) { // Write your code here let array = []; //find all numbers between the range of p and q and add them to an array for (let range = p; range <= q; range++) { array.push(range) } //get square numbers of that array let squared = array.map(num => num * num); //create a second array to store the square roots of the square numbers that are equal to the two parts of the split square number (int1 + int2). The split and conversion from string and back to int is done in the for loop below, looping through the 'squared' array let array2 = []; for (let i = 0; i < squared.length; i++) { let str = squared[i].toString(); let max = str.length; let str1 = str.slice(0, max / 2); let str2 = str.slice(max / 2, max); let int1 = parseInt(str1); let int2 = parseInt(str2); if (Math.sqrt(squared[i]) == (int1 + int2) || Math.sqrt(squared[i]) == squared[0]) { array2.push(Math.sqrt(squared[i])) } } if (array2.length == 0) { console.log('INVALID RANGE') } else { console.log(array2.join(' ')) }
}
+ 0 comments Python 3
def kaprekarNumbers(p, q): # Write your code here kapN = [] for i in range(p, q+1, 1): sqr = str(i**2) if i == 1: kapN.append(str(i)) elif len(sqr) < 2: pass else: d = math.ceil(len(sqr) / 2) r = int(sqr[-d:]) l = int(sqr[:-d]) if r > 0 and l > 0 and r+l == i: kapN.append(str(i)) if len(kapN) == 0: print('INVALID RANGE') print(' '.join(kapN))
+ 0 comments Python 3 1-liner
def kaprekarNumbers(p, q): if not [print(n, end=" ") for n in range(p, q + 1) if (int(str(n*n)[:-len(str(n))] or 0) + int(str(n*n)[-len(str(n)):])) == n]: print("INVALID RANGE")
Almost same code but unraveled
def kaprekarNumbers(p, q): found = False for n in range(p, q + 1): num = str(n*n) l = num[:-len(str(n))] r = num[-len(str(n)):] if int(l or 0) + int(r) == n: found = True print(n, end=" ") if not found: print("INVALID RANGE") return
+ 0 comments Here is my c++ solution, you can what the explanation here : https://youtu.be/MttrQCHGu3w
int getSum(long a){ long sq = a * a; int digit = (int)log10(a) + 1; int result = sq % (int)pow(10, digit); int rest = (int)log10(sq) + 1 - digit; if(rest > 0) result += stoi(to_string(sq).substr(0, rest)); return result; } void kaprekarNumbers(int p, int q) { bool valid_range = false; for(int i = p; i <= q; i++){ int s = getSum(i); if(s == i){ cout << i << " "; valid_range = true; } } if(!valid_range) cout << "INVALID RANGE"; }
Load more conversations
Sort 951 Discussions, By:
Please Login in order to post a comment