DEV Community

lechat
lechat

Posted on

Calculate mathematical things by my original language

As I wrote before, I made my original programming language. It is still pretty incomplete now, but at least I could write the following algorithm by that language.

This calculate n-th of fibonacci sequence. The following calculate 50th of
fibonacci sequence.

fn fib (n) {
  let mut f0 = 0;
  let mut f1 = 1;
  let mut f2 = 0;

  for i in 1..n {
    f2 = f1 + f0;
    f0 = f1;
    f1 = f2;
  }
  println("Answer:"<<f1);
}
fib(50);
Enter fullscreen mode Exit fullscreen mode

You can see execution time by -t option.

$ ./target/release/oran -f  ./examples/hello.orn -t
Answer:12586269025
443.486µs
Enter fullscreen mode Exit fullscreen mode

443.486µs. Not bad.

Now let's calculate pi by Leibniz formula:

let mut s = 0;
for i in 0..(10^7) {
    s = s + (-1^i) / (2 * i + 1);
}
println("Ans: " << (4*s))
Enter fullscreen mode Exit fullscreen mode

This is pretty slow. When the number of loops is 10^8, it takes even 30 secs. Not sure why this is so slow.

Maybe the for loop implementation in the language can be improved by memoise?

Top comments (0)