-
Notifications
You must be signed in to change notification settings - Fork 20.1k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/com/thealgorithms/maths/KaprekarNumbers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.thealgorithms.maths; | ||
|
||
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 | ||
|
||
public static boolean isKaprekarNumber(long number) { | ||
long numberSquared = number * number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
58
src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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