for (i in2..Math.sqrt(max.toDouble()).toInt()) { if (isPrime[i]) { for (j in i * i..max step i) { isPrime[j] = false } } }
// 统计数组中的素数个数 var count = 0 for (num in arr) { if (num >= 2 && isPrime[num]) { count++ } }
return count }
funmain() { val arr = intArrayOf(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) println("Count of prime numbers in the array with sieve: ${countPrimesInArrayWithSieve(arr)}") }