mirror of
https://github.com/vlang/v.git
synced 2025-08-03 17:57:59 -04:00
examples: add primes.v, that shows how to get command line arguments, and use loops and functions
This commit is contained in:
parent
d6031bae10
commit
ddfedc79ae
24
examples/primes.v
Normal file
24
examples/primes.v
Normal 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++
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user