-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMath.cs
37 lines (31 loc) · 927 Bytes
/
Math.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
namespace interviewPractice
{
public class Math
{
public int CountPrimes(int n)
{
var primes = new bool[n];
var primeCount = 0;
// to help cut down on the number of computations
// we use the fact the largest factor needed to determine the rest of the factors
//is the integer of sqrt(n). so our upper limit is i-squared
for (int i = 2; i * i < n; i++)
{
if (!primes[i])
{
//Cycle through all the factors of i
for (int j = i; i * j < n; j++)
{
primes[j * i] = true;
}
}
}
for (int i = 2; i < n; i++)
{
if (!primes[i]) primeCount++;
}
return primeCount;
}
}
}