Created
April 9, 2024 17:47
-
-
Save kirugan/b3fe871f5b2d83f8394415f05a3f8f03 to your computer and use it in GitHub Desktop.
Detecting 10001 prime number in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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