-
Notifications
You must be signed in to change notification settings - Fork 21.2k
NeonNumber algorithm in maths package #7420
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
base: master
Are you sure you want to change the base?
Changes from all commits
4ef6357
8fbf7f1
07b4672
50c8c1d
a2b3963
4fbfca5
e8fc048
5691cb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.thealgorithms.maths; | ||
|
|
||
| /** | ||
| * Neon Number algorithm. | ||
| * A number whose sum of digits of its square equals the number itself. | ||
| * Example: 9 - 9^2 = 81 - 8+1 = 9 | ||
| * | ||
| * @see <a href="https://en.wikipedia.org/wiki/Recreational_mathematics"> | ||
| * Wikipedia</a> | ||
| */ | ||
| public final class NeonNumber { | ||
|
|
||
| private NeonNumber() { | ||
| } | ||
|
|
||
| /** | ||
| * Check if a number is a Neon number. | ||
| * | ||
| * @param number the input number | ||
| * @return true if neon number, false otherwise | ||
| */ | ||
| public static boolean isNeon(final int number) { | ||
| int square = number * number; | ||
| int digitSum = 0; | ||
|
Contributor
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. No validation for negative numbers. isNeon(-5) won't crash Fix: Add at the start of method: |
||
| int temp = square; | ||
| while (temp > 0) { | ||
|
Contributor
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. When number = 0, temp = 0, so this loop never runs. Fix: |
||
| digitSum = digitSum + temp % 10; | ||
| temp /= 10; | ||
| } | ||
| return digitSum == number; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.thealgorithms.maths; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class NeonNumberTest { | ||
|
|
||
| @Test | ||
| public void testIsNeonTrue() { | ||
|
Contributor
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. assertTrue(NeonNumber.isNeon(0)) passes but for wrong reason — |
||
| assertTrue(NeonNumber.isNeon(0)); | ||
| assertTrue(NeonNumber.isNeon(1)); | ||
| assertTrue(NeonNumber.isNeon(9)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsNeonFalse() { | ||
|
Contributor
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. Missing edge case tests:
|
||
| assertFalse(NeonNumber.isNeon(2)); | ||
| assertFalse(NeonNumber.isNeon(5)); | ||
| assertFalse(NeonNumber.isNeon(10)); | ||
| } | ||
| } | ||
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.
Integer overflow risk! For large inputs (e.g. number = 50000),
number * numberexceeds Integer.MAX_VALUE and gives wrong result.Fix:
long square = (long) number * number;