Skip to content

Add Modular Arithmetic approach to RaindropConverter#3144

Open
habere-et-dispertire wants to merge 2 commits into
exercism:mainfrom
habere-et-dispertire:patch-1
Open

Add Modular Arithmetic approach to RaindropConverter#3144
habere-et-dispertire wants to merge 2 commits into
exercism:mainfrom
habere-et-dispertire:patch-1

Conversation

@habere-et-dispertire

@habere-et-dispertire habere-et-dispertire commented Jun 22, 2026

Copy link
Copy Markdown

Pull Request

This looks a little more visual in raku with multi-dispatch supporting built-in types:

multi fizz-buzz ( 1) { <                   > }
multi fizz-buzz (36) { < Pling             > }
multi fizz-buzz (85) { <       Plang       > }
multi fizz-buzz (91) { <             Plong > }
multi fizz-buzz (15) { < Pling Plang       > }
multi fizz-buzz (21) { < Pling       Plong > }
multi fizz-buzz (70) { <       Plang Plong > }
multi fizz-buzz ( 0) { < Pling Plang Plong > }

sub raindrop ($heard) {

    [~] fizz-buzz $heard¹² mod 105 or ~$heard

}

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

## Approach: `Modular Arithmetic`

```java
import java.math.BigInteger;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Formatting

Suggested change
import java.math.BigInteger;
import java.math.BigInteger;

}
```

For more information, check the [`Modular arithmetic` approach][approach-modular].

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants