-
-
Notifications
You must be signed in to change notification settings - Fork 767
Add Modular Arithmetic approach to RaindropConverter #3144
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: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -54,6 +54,32 @@ class RaindropConverter { | |
|
|
||
| For more information, check the [`Map` approach][approach-map]. | ||
|
|
||
| ## Approach: `Modular Arithmetic` | ||
|
|
||
| ```java | ||
| import java.math.BigInteger; | ||
| import static java.math.BigInteger.valueOf; | ||
|
|
||
| class RaindropConverter { | ||
|
|
||
| String convert (int n) { | ||
| switch ( valueOf(n).modPow( valueOf(12), valueOf(105) ).intValue() ) { | ||
| case (36): { return "Pling"; } | ||
| case (85): { return "Plang"; } | ||
| case (91): { return "Plong"; } | ||
| case (15): { return "PlingPlang"; } | ||
| case (21): { return "PlingPlong"; } | ||
| case (70): { return "PlangPlong"; } | ||
| case ( 0): { return "PlingPlangPlong"; } | ||
| default : { return String.valueOf(n); } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| ``` | ||
|
|
||
| For more information, check the [`Modular arithmetic` approach][approach-modular]. | ||
|
Member
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. The linked page describes the problem differently. It might be confusing because the page starts with "Fizz Buzz" instead and then generalizes the problem - it doesn't describe it in the same terms as the exercise. I think it might help to state the that the problem is like "Fizz" & "Buzz" and that the Modular arithmetic approach can be used to solve this type of problem. |
||
|
|
||
| ## Which approach to use? | ||
|
|
||
| Benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document, | ||
|
|
@@ -64,4 +90,5 @@ and no other code would need to be added. | |
| [remainder-operator]: https://www.geeksforgeeks.org/modulo-or-remainder-operator-in-java/ | ||
| [approach-if-statements]: https://exercism.org/tracks/java/exercises/raindrops/approaches/if-statements | ||
| [approach-map]: https://exercism.org/tracks/java/exercises/raindrops/approaches/map | ||
| [approach-modular]: https://philcrissman.net/posts/eulers-fizzbuzz/ | ||
| [jmh]: https://github.com/openjdk/jmh | ||
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.
Formatting