Skip to content

Added KaprekarNumbersTest #2881

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
Dec 15, 2021
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions src/main/java/com/thealgorithms/maths/KaprekarNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.thealgorithms.maths;

public class KaprekarNumbers {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a function which will generate all the Kaprekar Numbers in a given range


/* 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

public static boolean isKaprekarNumber(long number) {
long numberSquared = number * number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use BigInteger here which will make this better.

if(Long.toString(number).length() == Long.toString(numberSquared).length()){
return (number == numberSquared);
}
else{
long leftDigits1 = 0, leftDigits2 = 0;
if(Long.toString(numberSquared).contains("0")){
leftDigits1 = Long.parseLong(Long.toString(numberSquared).substring(0, Long.toString(numberSquared).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));
}
}

}
58 changes: 58 additions & 0 deletions src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.thealgorithms.maths;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class KaprekarNumbersTest {

@Test
void testFor1()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(1));
}

@Test
void testFor45()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(45));
}

@Test
void testFor297()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(297));
}

@Test
void testFor2223()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(2223));
}

@Test
void testFor857143()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(857143));
}


@Test
void testFor3()
{
assertFalse(KaprekarNumbers.isKaprekarNumber(3));
}

@Test
void testFor26()
{
assertFalse(KaprekarNumbers.isKaprekarNumber(26));
}

@Test
void testFor98()
{
assertFalse(KaprekarNumbers.isKaprekarNumber(98));
}

}