examples: add primes.v, that shows how to get command line arguments, and use loops and functions

This commit is contained in:
Delyan Angelov 2025-05-19 17:39:18 +03:00
parent d6031bae10
commit ddfedc79ae

24
examples/primes.v Normal file
View File

@ -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++
}
}