Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Iterate from p
to q
and check if actual number Kaprekar number (the addition of 2 parts of squared number is equal to actual number)
I created solution in:
All solutions are also available on my GitHub.
Java
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 |
import java.util.*; public class ModifiedKaprekarNumbers { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int lower = stdin.nextInt(); int upper = stdin.nextInt(); List<Integer> kaprekars = new ArrayList<>(); for(int i = lower; i <= upper; i++) { if(isKaprekar(i)) { kaprekars.add(i); } } if(kaprekars.isEmpty()) { System.out.println("INVALID RANGE"); } else { for(int k : kaprekars) { System.out.print(k + " "); } } stdin.close(); } private static boolean isKaprekar(int num) { String square = Long.valueOf((long) Math.pow(num, 2)).toString(); if(square.length() > 1) { long l = Long.parseLong(square.substring(0, square.length() / 2)); long r = Long.parseLong(square.substring(square.length() / 2)); return (l + r) == num; } else { return Long.parseLong(square) == num; } } } |
JavaScript
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 |
'use strict'; const processData = input => { const lines = input.split('\n').map(i => parseInt(i)); const lower = lines[0]; const upper = lines[1]; const kaprekarNums = []; for(let i = lower; i <= upper; i++) { if(isKaprekar(i)) { kaprekarNums.push(i); } } if(kaprekarNums.length > 0) { console.log(kaprekarNums.join(" ")); } else { console.log("INVALID RANGE"); } }; const isKaprekar = (num) => { const square = parseInt(Math.pow(num, 2)).toString(); if(square.length > 1) { const l = parseInt(square.substring(0, square.length / 2)); const r = parseInt(square.substring(square.length / 2)); return (l + r) === num; } else { return parseInt(square) === num; } } process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import scala.io.Source object ModifiedKaprekarNumbers extends App { val lines = Source.stdin.getLines().map(_.toInt).toList val lower = lines(0) val upper = lines(1) val kaprekarNums = (lower to upper).filter(isKaprekar) val output = if(!kaprekarNums.isEmpty) kaprekarNums.mkString(" ") else "INVALID RANGE" def isKaprekar(num: Int): Boolean = { val square = Math.pow(num, 2).toInt.toString if(square.length > 1) { val parts = square.splitAt(square.length / 2) val l = parts._1.toInt val r = parts._2.toInt return (l + r) == num } else { return square.toInt == num } } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def is_kaprekar(num) square = (num ** 2).to_s if(square.size > 0) l = square.slice(0, (square.size / 2).to_i).to_i r = square.slice((square.size / 2).to_i, (square.size)).to_i return (l + r) == num else return square.to_i == num end end lower = gets.to_i upper = gets.to_i kaprekars = [] for i in lower..upper kaprekars << i if(is_kaprekar(i)) end if(kaprekars.size > 0) puts kaprekars.join(" ") else puts "INVALID RANGE" end |