-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (46 loc) · 1.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'kaprekarNumbers' function below.
*
* The function accepts following parameters:
* 1. INTEGER p
* 2. INTEGER q
*/
function kaprekarNumbers(p, q) {
// Write your code here
let lowerNumber = p;
let upperNumber = q;
let resultNums = [];
for(let i = lowerNumber; i <= upperNumber; i++) {
let numDigits = (i + "").length;
let numberPow = Math.pow(i, 2);
let lengthNumbersPow = numberPow.toString().length;
let numberRigthPart = numberPow.toString().slice(- numDigits);
let numberLeftPart = (lengthNumbersPow - numDigits) > 0 ? numberPow.toString().slice(0, lengthNumbersPow - numDigits): 0;
let sumNumParts = parseInt(numberLeftPart) + parseInt(numberRigthPart);
if(sumNumParts === i) {
resultNums.push(i);
}
}
let result = resultNums.length >= 1 ? resultNums.join(" ") : "INVALID RANGE";
console.log(result);
}
function main() {
const p = parseInt(readLine().trim(), 10);
const q = parseInt(readLine().trim(), 10);
kaprekarNumbers(p, q);
}