ceilingPrime

pure nothrow @safe @nogc
size_t
ceilingPrime
(
in size_t value
,)

Return Value

Type: size_t

first prime in primeConstants >= value, where the linear search in primeConstants starts at primeIndex.

Increases primeIndex so that primeConstants[primeIndex] equals returned value.

Examples

verify for small modulos

1 size_t value = 0;		   // to be adjusted to nearest prime in `primeConstants`
2 auto i = PrimeIndex(0);
3 
4 value = 0;
5 value = ceilingPrime(value, i);
6 assert(primeConstants[i] == 0);
7 
8 value = 1;
9 value = ceilingPrime(value, i);
10 assert(primeConstants[i] == 2);
11 assert(value == 2);
12 
13 value = 2;
14 value = ceilingPrime(value, i);
15 assert(primeConstants[i] == 2);
16 assert(value == 2);
17 
18 value = 3;
19 value = ceilingPrime(value, i);
20 assert(primeConstants[i] == 3);
21 assert(value == 3);
22 
23 value = 4;
24 value = ceilingPrime(value, i);
25 assert(primeConstants[i] == 5);
26 assert(value == 5);
27 
28 value = 5;
29 value = ceilingPrime(value, i);
30 assert(primeConstants[i] == 5);
31 assert(value == 5);
32 
33 value = 6;
34 value = ceilingPrime(value, i);
35 assert(primeConstants[i] == 7);
36 assert(value == 7);
37 
38 value = 7;
39 value = ceilingPrime(value, i);
40 assert(primeConstants[i] == 7);
41 
42 foreach (const ix; 8 .. 11 + 1)
43 {
44 	value = ix;
45 	value = ceilingPrime(value, i);
46 	assert(value == 11);
47 	assert(primeConstants[i] == 11);
48 }
49 
50 foreach (const ix; 12 .. 13 + 1)
51 {
52 	value = ix;
53 	value = ceilingPrime(value, i);
54 	assert(value == 13);
55 	assert(primeConstants[i] == 13);
56 }
57 
58 foreach (const ix; 14 .. 17 + 1)
59 {
60 	value = ix;
61 	value = ceilingPrime(value, i);
62 	assert(value == 17);
63 	assert(primeConstants[i] == 17);
64 }
65 
66 foreach (const ix; 18 .. 23 + 1)
67 {
68 	value = ix;
69 	value = ceilingPrime(value, i);
70 	assert(value == 23);
71 	assert(primeConstants[i] == 23);
72 }

remaining modulos

foreach (const prime; primeConstants[3 .. $])
{
	size_t value = prime - 1;
	PrimeIndex primeIndex;
	value = ceilingPrime(value, primeIndex);
	assert(value == prime);
	assert(moduloPrimeIndex(value, primeIndex) == 0);
}

Meta