fork download
  1. import java.util.List;
  2. import java.util.Arrays;
  3. import java.util.function.IntPredicate;
  4. import java.util.function.BiConsumer;
  5. import java.util.stream.IntStream;
  6. class KaprekarNumbers {
  7. public static void main(String[] args) {
  8. List<List<Integer>> inputs = Arrays.asList(
  9. Arrays.asList(2, 100),
  10. Arrays.asList(101, 9000)
  11. );
  12.  
  13. IntPredicate isKaprekarNumber = x -> {
  14. String sqrd = Integer.toString((int) Math.pow(x, 2));
  15. return sqrd.length() > 1 && (Integer.parseInt(sqrd.substring(0, sqrd.length() / 2)) + Integer.parseInt(sqrd.substring(sqrd.length() / 2))) == x;
  16. };
  17.  
  18. BiConsumer<Integer, Integer> printKaprekarInRange = (low, high) -> IntStream.range(low, high + 1).filter(isKaprekarNumber).forEach(x -> System.out.print(x + " "));
  19.  
  20. inputs.forEach(cur -> {
  21. printKaprekarInRange.accept(cur.get(0), cur.get(1));
  22. System.out.println();
  23. });
  24. }
  25. }
Success #stdin #stdout 0.09s 711168KB
stdin
Standard input is empty
stdout
9 45 55 99 
297 703 999 2223 2728 4950 5050 7272 7777