Skip to content

Added kaprekarNumberInRange #2894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/main/java/com/thealgorithms/maths/KaprekarNumbers.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.thealgorithms.maths;
import java.util.*;

public class KaprekarNumbers {

/* This program demonstrates if a given number is Kaprekar Number or not.
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
digits and sum of these parts is equal to the original number. */

// Checks whether a given number is Kaprekar Number or not
// Provides a list of kaprekarNumber in a range
public static ArrayList<Long> kaprekarNumberInRange(long start, long end) throws Exception {
long n = end-start;
if (n <0) throw new Exception("Invalid range");
ArrayList<Long> list = new ArrayList<>();

for (long i = start; i <= end; i++) {
if (isKaprekarNumber(i)) list.add(i);
}

public static boolean isKaprekarNumber(long number) {
return list;
}

// Checks whether a given number is Kaprekar Number or not
public static boolean isKaprekarNumber(long number) {
long numberSquared = number * number;
if(Long.toString(number).length() == Long.toString(numberSquared).length()){
return (number == numberSquared);
}
else{
long leftDigits1 = 0, leftDigits2 = 0;
long leftDigits1 = 0, leftDigits2;
if(Long.toString(numberSquared).contains("0")){
leftDigits1 = Long.parseLong(Long.toString(numberSquared).substring(0, Long.toString(numberSquared).indexOf("0")));
}
Expand Down