Primality testing is to test whether a number is prime or not. In this article, We learn how to do the test using Cizen, beginning from the basics of Elixir.
Let’s start with the basics of Elixir.
Because Elixir is a highly concurrent language, everywhere is a process, and sending and receiving messages is primitive.
Now to Cizen. Cizen is a library for building highly concurrent, monitorable, and extensible applications with a collection of automata. In a nutshell, it’s a toolset to create an application by subscribing (
listen) to and publishing (
Dispatch.dispatch("Hi") is similar to
send pid, “Hi". The difference is that the former sends the message to all processes which subscribe to it.
Side note: “Cizen” means “Nature” in Japanese.
Then our primality testing can be written using Cizen like as follow (by spawning 9999 processes that test a number is divisible). This is far too inefficient, but it seems to works.
This prints the below 4 lines for the input of 12. If a prime number is given, nothing is shown.
12 is not a prime number because 12 is divisible by 2
12 is not a prime number because 12 is divisible by 3
12 is not a prime number because 12 is divisible by 4
12 is not a prime number because 12 is divisible by 6
A joke? Yes, indeed, but I wanted to show that such a
unique programming experience of Cizen. By using Cizen in appropriate ways, we can build highly concurrent, monitorable, and extensible applications. The joy of coding is still with us.
By the way, the above code of primality testing is broken because “1” is wrongly determined as a prime number. Try to fix it, if you’d like to.