Miller-Rabin Primality Test added to Rosetta Code

I was adding my Ruby and Nim code for the Miller-Rabin Primality Test to Rosetta Code and noticed there was no example for Crystal. In fact, there no Crystal examples for a lot of the code categories.

https://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_Crystal

So here’s my Crystal version of Miller-Rabin, which is almost a 100% exact translation of the Ruby version. I’ve also included the Nim version for comparison (Nim is compiled).

Crystal: https://rosettacode.org/wiki/Miller–Rabin_primality_test#Crystal

Ruby: https://rosettacode.org/wiki/Miller–Rabin_primality_test#Ruby

Nim: https://rosettacode.org/wiki/Miller–Rabin_primality_test#Nim

As an aside, I notice there is no syntax highlighting for the Crystal code as there is for Ruby.

The Ruby version is much faster because it has a fast BigInt implementation.

I think it would be a good exercise for people looking to do something tangible with Crystal, and get their feet wet with it, to implement any of the missing categories, to add them to the list. This will surely benefit Crystal, by being more visible for more categories, and acting as coding tutorials too. An easy way to jump into this is to just translate the Ruby versions into Crystal as a start.

2 Likes

That code though… mind blown. Always love knowing I’m a terrible coder. Puts my ego in check though.