[Golang] Naive Method for Primality Test


Naive method for primality test in Go: Given a natural number n, if n is divisible by any number from 2 to square root of n, then n is composite. Otherwise n is prime.

Run Code on Go Playground

func IsPrime(n int) bool {
      if n < 2 {
              return false
      }

      for i := 2; i*i <= n; i++ {
              if n%i == 0 {
                      return false
              }
      }
      return true
}

Tested on: Go Playground


References:

[1][Golang] Primality Test - Optimized School Method
[2]Lemoine's Conjecture - GeeksforGeeks
[3]Lemoine’s Conjecture
[4][Golang] Sieve of Eratosthenes
[5]