We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 75417c5 commit bb13e66Copy full SHA for bb13e66
1 file changed
examples/primes.v
@@ -1,24 +1,24 @@
1
-fn is_prime(n int) bool {
2
- if n <= 1 {
3
- return false
4
- }
5
- for i := 2; i * i <= n; i++ {
6
- if n % i == 0 {
7
+import math { log }
+
+n := arguments()[1] or { '10' }.int()
+sz := if n < 15 {
+ 50
+} else {
+ ln := log(f64(n))
8
+ int(f64(n) * (ln + log(ln))) + 1
9
+}
10
+mut sieve := []bool{len: sz}
11
+for i := 2; i * i < sz; i++ {
12
+ if !sieve[i] {
13
+ for j := i * i; j < sz; j += i {
14
+ sieve[j] = true
15
}
16
- return true
17
-
-fn main() {
- how_many := arguments()[1] or { '10' }.int()
- mut count := 0
- mut num := 2
- for count < how_many {
18
- if is_prime(num) {
19
- println(num)
20
- count++
21
22
- num++
+mut c := 0
+for i := 2; c < n && i < sz; i++ {
+ println(i)
+ c++
23
24
0 commit comments