Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/math/primenumbers.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2003 Ferdinando Ametrano
5
6
 This file is part of QuantLib, a free-software/open-source library
7
 for financial quantitative analysts and developers - http://quantlib.org/
8
9
 QuantLib is free software: you can redistribute it and/or modify it
10
 under the terms of the QuantLib license.  You should have received a
11
 copy of the license along with this program; if not, please email
12
 <quantlib-dev@lists.sf.net>. The license is also available online at
13
 <http://quantlib.org/license.shtml>.
14
15
 This program is distributed in the hope that it will be useful, but WITHOUT
16
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19
20
// ===========================================================================
21
// NOTE: The following copyright notice applies to the original code,
22
//
23
// Copyright (C) 2002 Peter Jäckel "Monte Carlo Methods in Finance".
24
// All rights reserved.
25
//
26
// Permission to use, copy, modify, and distribute this software is freely
27
// granted, provided that this notice is preserved.
28
// ===========================================================================
29
30
#include <ql/math/primenumbers.hpp>
31
#include <iterator>
32
33
namespace QuantLib {
34
35
    namespace {
36
37
        const BigNatural firstPrimes[] = {
38
            // the first two primes are mandatory for bootstrapping
39
            2,  3,
40
            // optional additional precomputed primes
41
            5,  7, 11, 13, 17, 19, 23, 29,
42
            31, 37, 41, 43, 47 };
43
    }
44
45
    std::vector<BigNatural> PrimeNumbers::primeNumbers_;
46
47
0
    BigNatural PrimeNumbers::get(Size absoluteIndex) {
48
0
        if (primeNumbers_.empty()) {
49
0
            Size n = sizeof(firstPrimes)/sizeof(firstPrimes[0]);
50
0
            primeNumbers_.insert(primeNumbers_.end(),
51
0
                                 firstPrimes, firstPrimes+n);
52
0
        }
53
0
        while (primeNumbers_.size()<=absoluteIndex)
54
0
            nextPrimeNumber();
55
0
        return primeNumbers_[absoluteIndex];
56
0
    }
57
58
0
    BigNatural PrimeNumbers::nextPrimeNumber() {
59
0
        BigNatural p, n, m = primeNumbers_.back();
60
0
        do {
61
            // skip the even numbers
62
0
            m += 2;
63
0
            n = static_cast<BigNatural>(std::sqrt(Real(m)));
64
            // i=1 since the even numbers have already been skipped
65
0
            Size i = 1;
66
0
            do {
67
0
                p = primeNumbers_[i];
68
0
                ++i;
69
0
            } while (((m % p) != 0U) && p <= n);
70
0
        } while ( p<=n );
71
0
        primeNumbers_.push_back(m);
72
0
        return m;
73
0
    }
74
75
}