Advertisement
spytheman123

Primes Go

Oct 16th, 2024 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.62 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "os"
  6.     "strconv"
  7.     "time"
  8. )
  9.  
  10. func main() {
  11.     var max_n = int32(20_000)
  12.     if len(os.Args) > 1 {
  13.         if n, err := strconv.Atoi(os.Args[1]); err == nil {
  14.             max_n = int32(n)
  15.         }
  16.     }
  17.     t := time.Now()
  18.     primes := make([]int32, 1, max_n)
  19.     primes[0] = 2
  20.     var check int32 = 3
  21.     var index int32 = 1
  22.     for index < max_n {
  23.         is_prime := true
  24.         for _, prime := range primes {
  25.             if (check % prime) == 0 {
  26.                 is_prime = false
  27.                 break
  28.             }
  29.         }
  30.         if is_prime {
  31.             primes = append(primes, check)
  32.             index += 1
  33.         }
  34.         check += 2
  35.     }
  36.     fmt.Println(len(primes))
  37.     fmt.Println(time.Since(t))
  38. }
Tags: primes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement