Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.79 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math"
  6.     "time"
  7. )
  8.  
  9. var list = make([]int, 0, 1)
  10.  
  11. func checkPrime(numero int) {
  12.     var lim = (int)(math.Floor(math.Sqrt(float64(numero))) + 1)
  13.  
  14.     for j := 0; j < len(list); j++ {
  15.         if numero%list[j] == 0 {
  16.             return
  17.         }
  18.         if list[j] > lim {
  19.             break
  20.         }
  21.     }
  22.         list = append(list,numero)
  23. }
  24.  
  25. func getPrimes(upperLimit int) {
  26.     for i := 3; i < upperLimit; i += 2 {
  27.         checkPrime(i)
  28.     }
  29. }
  30.  
  31. func main() {
  32.     benchmakFunction(func() {
  33.         getPrimes(67108864)
  34.     })
  35.     fmt.Println(len(list),list[len(list)-1])
  36. }
  37.  
  38. func benchmakFunction(functionToBench func()) {
  39.     list = append(list,2)
  40.     fmt.Println(time.Now())
  41.     functionToBench()
  42.     fmt.Println(time.Now())
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement