From ddfedc79aed1c103934618b7c730c6828ab385c9 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 19 May 2025 17:39:18 +0300 Subject: [PATCH] examples: add primes.v, that shows how to get command line arguments, and use loops and functions --- examples/primes.v | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/primes.v diff --git a/examples/primes.v b/examples/primes.v new file mode 100644 index 0000000000..2f14dc8d10 --- /dev/null +++ b/examples/primes.v @@ -0,0 +1,24 @@ +fn is_prime(n int) bool { + if n <= 1 { + return false + } + for i := 2; i * i <= n; i++ { + if n % i == 0 { + return false + } + } + return true +} + +fn main() { + how_many := arguments()[1] or { '10' }.int() + mut count := 0 + mut num := 2 + for count < how_many { + if is_prime(num) { + println(num) + count++ + } + num++ + } +}