I am trying to create a function that returns me the total count of the prime numbers, but the problem I am running into is that it is not working for few cases like 3, or 4. Can you please tell me where am I making mistake?
n = 10
func primeNumber(n int) int {
var totalNumberOfPrimes int
for i := 2; i <= n/2; i++ {
if n%i == 0 {
fmt.Printf("The value is the not prime %v \n", i)
}
totalNumberOfPrimes++
}
return totalNumberOfPrimes
}
This is wrong code: it counts x*x as prime. For example, it will return 25 as being prime. You should replace for i := 2; i < testUntil; i++ { with i <= testUntil