diff --git a/examples/fib.par b/examples/fib.par new file mode 100644 index 0000000..1383362 --- /dev/null +++ b/examples/fib.par @@ -0,0 +1,16 @@ +fn fib[n: int]: int { + if n < 2 { + return n; + } + + var x0 = 0; + var x1 = 1; + while n > 0 { + var tmp = x0 + x1; + x0 = x1; + x1 = tmp; + n = n - 1; + } + + return x1; +} diff --git a/examples/gcd.par b/examples/gcd.par new file mode 100644 index 0000000..cb97ee4 --- /dev/null +++ b/examples/gcd.par @@ -0,0 +1,6 @@ +fn gcd[a: int, b: int]: int { + if b == 0 { + return a; + } + return gcd[b, a % b]; +} diff --git a/examples/sqrt.par b/examples/sqrt.par new file mode 100644 index 0000000..2fe1df8 --- /dev/null +++ b/examples/sqrt.par @@ -0,0 +1,9 @@ +fn sqrt[a: float]: float { + var x = 0.5 * a; + var i = 10; + while i > 0 { + x = 0.5 * (x + a / x); + i = i - 1; + } + return x; +}