Have you ever thought about creating your own flashcards app?
Well, I have. Although there are plenty of applications like that I developed a simple one for my personal purposes.
Today I’d like to share the implementation of the SM-2 spaced repetition algorithm in the Kotlin language.
I did my best to keep this implementation as simple as possible but yet easy to understand and read.
I’d like to keep it this way because I’m not a big fan of the algorithm implementations which contains hundreds of lines of code and plenty of fancy variables such as k, p, V, etc.
The flashcard data model
The single flashcard will be represented by the simple data model. Take a look at this class:
As I previously mentioned, the implementation is using Kotlin.
I’m not going to explain the code in detail here, if you’re looking for a good reference, please take a look here.
To make sure it works, a proper test cases have been provided:
Please note that the original SM-2 algorithm has been published for the first time in 1988. There’s a newer, improved version of that algorithm (SM-18) already in use. But it’s not open source, so the most common applications (like Anki) use the variations of the SM-2 mostly.
Anyway, I hope you find it useful and you’re now ready to use the algorithm for your own spaced repetition-based application.
Good luck 👋