From ab9f74c2ee0dee9174616f2154f1161d600f68cd Mon Sep 17 00:00:00 2001 From: Subhro Acharjee <88422396+subhroblkbox@users.noreply.github.com> Date: Thu, 13 Jan 2022 01:53:53 +0530 Subject: [PATCH] Add BigInteger to karprekar number (#2899) Co-authored-by: Andrii Siriak --- .../thealgorithms/maths/KaprekarNumbers.java | 33 +++++++++++++------ .../maths/KaprekarNumbersTest.java | 14 +++++++- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java b/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java index d03854b8..7cdd182a 100644 --- a/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java +++ b/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java @@ -1,4 +1,5 @@ package com.thealgorithms.maths; +import java.math.BigInteger; import java.util.*; public class KaprekarNumbers { @@ -21,19 +22,31 @@ public class KaprekarNumbers { } // 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); + public static boolean isKaprekarNumber(long num) { + String number = Long.toString(num); + BigInteger originalNumber = new BigInteger(number); + BigInteger numberSquared = originalNumber.multiply(originalNumber); + if(number.length() == numberSquared.toString().length()){ + return number.equals(numberSquared.toString()); } else{ - long leftDigits1 = 0, leftDigits2; - if(Long.toString(numberSquared).contains("0")){ - leftDigits1 = Long.parseLong(Long.toString(numberSquared).substring(0, Long.toString(numberSquared).indexOf("0"))); + BigInteger leftDigits1 = new BigInteger("0"); + BigInteger leftDigits2; + if(numberSquared.toString().contains("0")){ + leftDigits1 = new BigInteger( + numberSquared.toString(). + substring(0, numberSquared.toString().indexOf("0") + ) + ); } - leftDigits2 = Long.parseLong(Long.toString(numberSquared).substring(0, (Long.toString(numberSquared).length() - Long.toString(number).length()))); - long rightDigits = Long.parseLong(Long.toString(numberSquared).substring(Long.toString(numberSquared).length() - Long.toString(number).length())); - return (number == (leftDigits1 + rightDigits)) || (number == (leftDigits2 + rightDigits)); + leftDigits2 = new BigInteger( + numberSquared.toString() + .substring(0, (numberSquared.toString().length() - number.length())) + ); + BigInteger rightDigits = new BigInteger(numberSquared.toString().substring(numberSquared.toString().length() - number.length())); + String x = leftDigits1.add(rightDigits).toString(); + String y = leftDigits2.add(rightDigits).toString(); + return (number.equals(x)) || (number.equals(y)); } } diff --git a/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java b/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java index 04f8cc09..1d08f810 100644 --- a/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java +++ b/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.maths; import org.junit.jupiter.api.Test; - +import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.*; public class KaprekarNumbersTest { @@ -55,4 +55,16 @@ public class KaprekarNumbersTest { assertFalse(KaprekarNumbers.isKaprekarNumber(98)); } + @Test + void testForRangeOfNumber() { try { + ArrayList rangedNumbers = KaprekarNumbers.kaprekarNumberInRange(1,100000); + long[] allTheNumbers = {1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4950, 5050, 7272, 7777, 9999, 17344, 22222, 77778, 82656, 95121, 99999}; + for (long i:allTheNumbers) { + assert rangedNumbers.contains(i); + } + } catch (Exception e) { + assert false; + } + } + } \ No newline at end of file