Advertisement
__init__

Untitled

Dec 1st, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.74 KB | None | 0 0
  1. package main
  2.  
  3.  
  4. import (
  5.     "fmt";
  6.     "math"
  7. )
  8.  
  9. func sieve(limit int) []bool {
  10.     primes := make([]bool, limit+1)    
  11.    
  12.     for x:=0; x<=limit; x++ {
  13.         if x < 2 {
  14.             primes[x] = false
  15.         } else {
  16.             primes[x] = true
  17.         }
  18.     }
  19.    
  20.     for i := 0; i <= int(math.Sqrt(float64(limit))); i++ {
  21.         if primes[i] {
  22.             for j:=i*i; j<limit; j+=i {
  23.                 primes[j] = false;
  24.             }
  25.         }
  26.     }
  27.    
  28.     return primes;
  29. }
  30.  
  31. func main() {
  32.     upto := 2000000 // 2 million
  33.     pmap := sieve(upto)
  34.     var total uint64
  35.    
  36.     for i := 0; i < upto; i++ {
  37.         if pmap[i] {
  38.             total += uint64(i)
  39.         }
  40.     }
  41.    
  42.     fmt.Println(total);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement