Improve Crystal's profile on Rosetta Code


There are a lot of code categories on Rosetta code with no Crystal examples.

An easy way to increase Crystal’s profile on the site, and in the coding world overall, is for people to provide the missing examples. The easiest way to do this is to just translate the Ruby version in each category, as a start.

Doing this would not only up Crystal’s profile, it would also be a good exercise for people (newbies, et al) looking to do something tangible with Crystal. This will surely benefit Crystal, by being more visible for more categories, and acting as coding tutorials too.

The instructions for adding new code is on the above site. It’s may be initially confusing at first (it was for me), but once you do it once it makes sense (it could be made much simpler though).


Previously added Miller-Rabin primality test.–Rabin_primality_test#Crystal

Just added Farey sequence (2019/3/23).

1 Like

I;ve added several entries and it is somawhat funny (similar to adventofcode, without tests and easier, but at least you aren’t just throwing your time away).

But in many cases you can just copy Ruby solution and fix syntax errors - this is boring and doesn’t seems to be the right way.


Submitting Rosetta Code examples works on multiple levels, for different types of people.

If you are a new (to Crystal) programmer, it’s a concrete way to learn the nuances of Crystal as compared to other languages. For the Farey sequence I learned (unfortunately) the Ruby example cannot be used. So I used the Lua version, and then went to Wikipedia to see where that implementation came from. So I learned a lot (on multiple levels) about what doesn’t work in Crystal and some math. In both cases I consider that a win-win.

The key point of the exercises is to start using Language X as a tutorial for the language. Like learning any subject, you start with the basics. If you already know the language, yes, it might seem boring (like taking Algebra 101 over again), but the examples are definitely helpful for people wanting to learn syntax, structure, and vocabulary for some language.

In fact for Crystal, the first thing to do is to translate the Ruby version, just to see the level of compatibility. I think people might be vexed to see how much Crystal’s typing system effects how much you have to do to get a direct Ruby translation to work.

Ultimately though, I hope it’s not arguable that having more Crystal examples is better, than not.


Added N'th (2019/3/25).'th#Crystal


Added 3 more low hanging fruit examples.

Quick Select

Ackermann Function

9 billion names of God the integer


To help people create submissions I created a template for rosettacode, with the most basic one shown below.

<lang ruby>




You can actually omit the lines after {{out}} if you don’t submit output. You just put in the code for whatever task you’re doing, then click Edit at the top of the page for the task, and insert the templated code in the alphabetically correct place in the list (usually before the D entry). It’s really fairly simple. All I did was see how other examples were done and copied them. Here’s the whole templated code for the Ackermann Function.

<lang crystal>def ack(m, n)
  if m == 0
    n + 1
  elsif n == 0
    ack(m-1, 1)
    ack(m-1, ack(m, n-1))

(0..3).each do |m|
  puts (0..6).map { |n| ack(m, n) }.join(' ')

1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 5 7 9 11 13 15
5 13 29 61 125 253 509

Once you submit the code in the correct place in the task list, you can preview it, and when satisfied that it’s correct, then you Save it, and supply a comment in the Summary field at the bottom. I usually put something like Added Crystal translation of Ruby version, if that was the case. This is logged into the history system for the site.

1 Like

Here are 5 more off the undone list (2019/3/29).

AKS test for primes

Averages/Root Mean Square

Julia Set

Roots of Unity

Runge Kata

1 Like

Using SDL to generate an actual image would be awesome for the Julia Set example


I had started translating the Ruby FFT example a couple of days ago, and hadn’t figured out exactly how to do it that way in Crystal, and went on and did some others first. So I went back today, and someone had beat me to it, and posted code for it, but there were a couple of problems with it. First, it wouldn’t compile as written (needed require "complex"), and take as input the example input vector. Second, it didn’t have an output statement and results.

However, the approach the person took was very similar to mine, and fixing the input vector issue to take the example array was simple. So I simplified it more, and added an output statement and results, and reposted it.

One thing I’ve noticed when using Complex numbers, sometimes the output results aren’t exact for whole number answers, e.g 1.0 is ouput as 0.99999999999. I know this is inherit in roundoff errors using floating point, but it would be very nice if internally Crystal would set a better limit parameter, or whatever. In Ruby I created my own custom cos and sin functions in my Roots gem to always give the correct whole number values for angles of 0, Pi/2, Pi, etc. I just made sure for these given angles I forced the result to be the correct integer.

Fast Fourier Transform

1 Like

Or stumpy_png


EDIT: Also added Harshad|Niven series for the haul of the day.

OK, 4 more (2019/4/1), no kidding (April 1st).

Harshad or Niven series

Haversine formula

Leonardo Numbers

Levenshtein distance

Actually for this last one someone had beat me again from when I first looked at it, but I learned two (2) things from submitting translations of the Ruby versions (which I wasn’t going to waste after doing).

  1. Crystal has a lenvenshtein module already (who knew? why? :smile:)
  2. In Ruby you can do: arry.each_slice(2) do |a, b| .... use a and b
    in Crystal must do: arry.each_slice(2) do |pair| a, b = pair ...

Interesting, here’s the code in the module.


Levenshtein is there for the compiler, for “did you mean?”. But its API is not the best one. We should probably move it to the Crystal namespace and hide it.


About automatic slicing in Ruby (that is, do |a, b|): you can do it in Crystal too but only with Tuple. If it’s an Array, or anything that’s indexable, you can use do |(a, b)|, which explicitly unpacks it (works with tuples too).


Levenhstein is super useful for shards that want to provide did you mean to enhance their libraries. If you don’t want it in core maybe a shard?

I’d also be happy to make a PR to clean up the API so it’s a bit easier to use but I think it’s also fine as is.

Here’s some example code where I’m using it:

1 Like

Yeah, maybe Levenshtein is generic enough right now and it’s just missing some docs.


Hey thanks!. I tried – do |(a,b)| – in Levenshtein example, it works, and changed the Rosetta code to show both versions. This isn’t documented for arrays though (at least I didn’t find it, and I looked hard).

Also, I’m learning so many Ruby->Crystal idiom changes it would be a nice to have a document on the website to point to specific list and describe them. I have found at least 8-10 now that I can list alone. It would have helped me if I had that list to work from.

I found this below, but it appears to be inactive.


There is But it’s by no means exhaustive.


Tu 2019/4/16

Combinations and permutations

Combinations with repetitions

Couldn’t (currently) get the same precision as Ruby, et al. See Ruby version for comparison.

Hickerson series of almost integers

1 Like

Sum of a series

Sum multiples of 3 and 5

1 Like