File tree Expand file tree Collapse file tree
src/main/java/com/thealgorithms/maths Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .thealgorithms .maths ;
2+ import java .util .*;
23
34public class KaprekarNumbers {
45
56 /* This program demonstrates if a given number is Kaprekar Number or not.
67 Kaprekar Number: A Kaprekar number is an n-digit number which its square can be split into two parts where the right part has n
78 digits and sum of these parts is equal to the original number. */
89
9- // Checks whether a given number is Kaprekar Number or not
10+ // Provides a list of kaprekarNumber in a range
11+ public static ArrayList <Long > kaprekarNumberInRange (long start , long end ) throws Exception {
12+ long n = end -start ;
13+ if (n <0 ) throw new Exception ("Invalid range" );
14+ ArrayList <Long > list = new ArrayList <>();
15+
16+ for (long i = start ; i <= end ; i ++) {
17+ if (isKaprekarNumber (i )) list .add (i );
18+ }
1019
11- public static boolean isKaprekarNumber (long number ) {
20+ return list ;
21+ }
22+
23+ // Checks whether a given number is Kaprekar Number or not
24+ public static boolean isKaprekarNumber (long number ) {
1225 long numberSquared = number * number ;
1326 if (Long .toString (number ).length () == Long .toString (numberSquared ).length ()){
1427 return (number == numberSquared );
1528 }
1629 else {
17- long leftDigits1 = 0 , leftDigits2 = 0 ;
30+ long leftDigits1 = 0 , leftDigits2 ;
1831 if (Long .toString (numberSquared ).contains ("0" )){
1932 leftDigits1 = Long .parseLong (Long .toString (numberSquared ).substring (0 , Long .toString (numberSquared ).indexOf ("0" )));
2033 }
You can’t perform that action at this time.
0 commit comments