I just read this post on how to convert a number to a Roman numeral. The post outlines an iterative method, which works and is very fun. It got me thinking though. I've always had a strange interest in recursive algorithms. There is something very satisfying to me about writing a recursive method that works. If you don't know or need a reminder, a recursive algorithm is one which calls itself. A famous example is the Fibonacci sequence, which computes the value of the next number in the sequence by adding up the previous two. If you know the sequence starts with a pair of 1s, then it's pretty easy to write a recursive function which can compute the _n_th value in the sequence: int fib(int n) => switch (n) { 0 || 1 => 1, final n => fib(n - 1) + fib(n - 2), }; So I wondered whether it was possible to convert something to Roman numerals recursively. Turns out, it super is and I love it. const romanSymbols = <int, String>{ 1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC', 50:…
No comments yet. Log in to reply on the Fediverse. Comments will appear here.