File tree 1 file changed +16
-3
lines changed
src/main/java/com/thealgorithms/maths
1 file changed +16
-3
lines changed Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .maths ;
2
+ import java .util .*;
2
3
3
4
public class KaprekarNumbers {
4
5
5
6
/* This program demonstrates if a given number is Kaprekar Number or not.
6
7
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
7
8
digits and sum of these parts is equal to the original number. */
8
9
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
+ }
10
19
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 ) {
12
25
long numberSquared = number * number ;
13
26
if (Long .toString (number ).length () == Long .toString (numberSquared ).length ()){
14
27
return (number == numberSquared );
15
28
}
16
29
else {
17
- long leftDigits1 = 0 , leftDigits2 = 0 ;
30
+ long leftDigits1 = 0 , leftDigits2 ;
18
31
if (Long .toString (numberSquared ).contains ("0" )){
19
32
leftDigits1 = Long .parseLong (Long .toString (numberSquared ).substring (0 , Long .toString (numberSquared ).indexOf ("0" )));
20
33
}
You can’t perform that action at this time.
0 commit comments