Add Modular Arithmetic approach to RaindropConverter#3144
Open
habere-et-dispertire wants to merge 2 commits into
Open
Add Modular Arithmetic approach to RaindropConverter#3144habere-et-dispertire wants to merge 2 commits into
habere-et-dispertire wants to merge 2 commits into
Conversation
kahgoh
requested changes
Jun 23, 2026
| ## Approach: `Modular Arithmetic` | ||
|
|
||
| ```java | ||
| import java.math.BigInteger; |
Member
There was a problem hiding this comment.
Formatting
Suggested change
| import java.math.BigInteger; | |
| import java.math.BigInteger; |
| } | ||
| ``` | ||
|
|
||
| For more information, check the [`Modular arithmetic` approach][approach-modular]. |
Member
There was a problem hiding this comment.
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.
Comment on lines
+66
to
+74
| switch ( valueOf(n).modPow( valueOf(12), valueOf(105) ).intValue() ) { | ||
| case ( 1): { return String.valueOf(n); } | ||
| case (36): { return "Pling"; } | ||
| case (85): { return "Plang"; } | ||
| case (91): { return "Plong"; } | ||
| case (15): { return "PlingPlang"; } | ||
| case (21): { return "PlingPlong"; } | ||
| case (70): { return "PlangPlong"; } | ||
| default : { return "PlingPlangPlong"; } // 0 |
Member
There was a problem hiding this comment.
Is the default always 0? I was thinking it might be better practice to use case (0) and use default for 1 as that would align more with the instructions.
Also, have you considered using switch expression? The solution would be a bit more compact
return switch ( valueOf(n).modPow( valueOf(12), valueOf(105) ).intValue() ) {
case 36 -> "Pling";
case 85 -> "Plang";
// ... etc ...
}
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Pull Request
This looks a little more visual in
rakuwith multi-dispatch supporting built-in types:I think it's a fun way to explore deeper into a generalized mathematical approach since it shows that this approach works also with a change in factors so long as they remain co-prime.
Reviewer Resources:
Track Policies