Skip to content

Instantly share code, notes, and snippets.

@kirugan
Created April 9, 2024 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirugan/b3fe871f5b2d83f8394415f05a3f8f03 to your computer and use it in GitHub Desktop.
Save kirugan/b3fe871f5b2d83f8394415f05a3f8f03 to your computer and use it in GitHub Desktop.
Detecting 10001 prime number in golang
func TestMe(t *testing.T) {
var primes int
for i := 2; i < 1e7; i++ {
if isPrime(i) {
primes++
// fmt.Printf("%d is prime. Congrats!\n", i)
if primes == 10_001 {
fmt.Printf("%d th prime is %d\n", primes, i)
break
}
}
}
fmt.Printf("Total number of primes is %d\n", primes)
}
func isPrime(n int) bool {
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment