diff options
author | Juan J. Martinez <jjm@usebox.net> | 2022-07-18 12:12:28 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2022-07-18 12:12:28 +0100 |
commit | b6b68929a7efb19962795211c58d52dd08249235 (patch) | |
tree | d3f3307382626d39f4b483db6d2a5b53acc28ab6 /examples/wilsonp.micro | |
parent | f7686379eb2fdc740b76a714f80435b71be9c24c (diff) | |
download | micro-lang-b6b68929a7efb19962795211c58d52dd08249235.tar.gz micro-lang-b6b68929a7efb19962795211c58d52dd08249235.zip |
Added examples
Diffstat (limited to 'examples/wilsonp.micro')
-rw-r--r-- | examples/wilsonp.micro | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/wilsonp.micro b/examples/wilsonp.micro new file mode 100644 index 0000000..9bc626f --- /dev/null +++ b/examples/wilsonp.micro @@ -0,0 +1,34 @@ +// +// Show prime numbers using the Wilson theorem: https://en.wikipedia.org/wiki/Wilson%27s_theorem#Primality_tests +// +// Also using a recursive function for the loops +// + +def loop(from number, to number, fn func (number) bool) { + if from > to { + return; + } + if !fn(from) { + return; + } + return loop(from + 1, to, fn); +} + +def wilsonPrime(n number) bool { + var acc number = 1; + def fact(i number) bool { + acc = (acc * i) % n; + return acc != 0; + } + + loop(2, n - 1, fact); + + if acc == n - 1 { + println(n, " is prime"); + } + + return true; +} + +loop(2, 100, wilsonPrime); + |