Professional Documents
Culture Documents
Untitled
Untitled
NUMBER THEORY
c Steven & Felix, NUS
// in int main()
sieve(100); // prepare list of primes [0 .. 100]
vi result = primeFactors(10000); // with that, we can factor up to 100^2 = 10000
vi::iterator last = unique(result.begin(), result.end()); // to remove duplicates
for (vi::iterator i = result.begin(); i != last; i++) // output: 2 and 5
printf("%d\n", *i);
In the worst case – when N is prime, this prime factoring algorithm with trial division requires
√ √ √ √
testing all smaller primes up to N, mathematically denoted as O(π( N )) = O( N /ln N ).
However, if given composite numbers, this algorithm is reasonably fast.
97