/src/aspell/modules/speller/default/primes.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2000 |
2 | | // Kevin Atkinson |
3 | | // |
4 | | // Permission to use, copy, modify, distribute and sell this software |
5 | | // and its documentation for any purpose is hereby granted without |
6 | | // fee, provided that the above copyright notice appear in all copies |
7 | | // and that both that copyright notice and this permission notice |
8 | | // appear in supporting documentation. Kevin Atkinson makes no |
9 | | // representations about the suitability of this software for any |
10 | | // purpose. It is provided "as is" without express or implied |
11 | | // warranty. |
12 | | |
13 | | #include "primes.hpp" |
14 | | #include <cmath> |
15 | | #include <cassert> |
16 | | |
17 | | namespace aspeller { |
18 | | |
19 | | using namespace std; |
20 | | |
21 | 0 | void Primes::resize(size_type s) { |
22 | 0 | size_type i, j = 2; |
23 | 0 | data.resize(s); |
24 | 0 | for(i = 0; i < s; ++i) |
25 | 0 | data[i] = true; |
26 | 0 | if (s > 0) |
27 | 0 | data[0] = false; |
28 | 0 | if (s > 1) |
29 | 0 | data[1] = false; |
30 | 0 | size_type sqrt_s = static_cast<size_type>(sqrt(static_cast<double>(s))); |
31 | 0 | while (j < sqrt_s) { |
32 | 0 | for (i = 2*j; i < s; i += j) { |
33 | 0 | data[i] = false; |
34 | 0 | } |
35 | 0 | ++j; |
36 | 0 | for (;j < sqrt_s && !data[j]; ++j); |
37 | 0 | } |
38 | 0 | } |
39 | | |
40 | 0 | bool Primes::is_prime(size_type n) const { |
41 | 0 | if (n < size()) { |
42 | 0 | return data[n]; |
43 | 0 | } else { |
44 | 0 | size_type e = static_cast<size_type>(sqrt(static_cast<double>(n))); |
45 | 0 | assert(e < size()); |
46 | 0 | for (const_iterator i = begin(); *i <= e; ++i) |
47 | 0 | if (!(n % *i)) return false; |
48 | 0 | return true; |
49 | 0 | } |
50 | 0 | } |
51 | | } |