Forked from romanitalian/2020_who_is_faster_golang.sh
Created
December 15, 2020 20:37
-
-
Save mwf/7fa9321d68b083bc8d1d2e978e2913a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
"time" | |
) | |
func isPrime(num int) bool { | |
if num == 2 { | |
return true | |
} | |
if num == 1 || num%2 == 0 { | |
return false | |
} | |
to := int(math.Sqrt(float64(num))) | |
for div := 3; div <= to; div += 2 { | |
if num%div == 0 { | |
return false | |
} | |
} | |
return true | |
} | |
func do(N int) { | |
for i := 0; i < N; i++ { | |
prime := isPrime(i) | |
if prime { | |
// fmt.Printf("%+v: %+v\n", i, prime) | |
} | |
} | |
} | |
func main() { | |
st := time.Now() | |
do(10_000_000) | |
fmt.Printf("%+v\n", time.Since(st)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment