Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions exercises/practice/raindrops/.approaches/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ class RaindropConverter {

For more information, check the [`Map` approach][approach-map].

## Approach: `Modular Arithmetic`

```java
import java.math.BigInteger;
Comment thread
habere-et-dispertire marked this conversation as resolved.
Outdated
import static java.math.BigInteger.valueOf;

class RaindropConverter {

String convert (int n) {
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
Comment thread
habere-et-dispertire marked this conversation as resolved.
Outdated
}
}

}
```

For more information, check the [`Modular arithmetic` approach][approach-modular].
Comment thread
habere-et-dispertire marked this conversation as resolved.
Outdated

## Which approach to use?

Benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document,
Expand All @@ -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