QuantLib
: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
ql
math
primenumbers.cpp
Go to the documentation of this file.
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
BigNatural
PrimeNumbers::get
(
Size
absoluteIndex) {
48
if
(
primeNumbers_
.empty()) {
49
Size
n
=
sizeof
(firstPrimes)/
sizeof
(firstPrimes[0]);
50
primeNumbers_
.insert(
primeNumbers_
.end(),
51
firstPrimes, firstPrimes+
n
);
52
}
53
while
(
primeNumbers_
.size()<=absoluteIndex)
54
nextPrimeNumber
();
55
return
primeNumbers_
[absoluteIndex];
56
}
57
58
BigNatural
PrimeNumbers::nextPrimeNumber
() {
59
BigNatural
p,
n
, m =
primeNumbers_
.back();
60
do
{
61
// skip the even numbers
62
m += 2;
63
n
=
static_cast<
BigNatural
>
(std::sqrt(
Real
(m)));
64
// i=1 since the even numbers have already been skipped
65
Size
i = 1;
66
do
{
67
p =
primeNumbers_
[i];
68
++i;
69
}
while
(((m % p) != 0U) && p <=
n
);
70
}
while
( p<=
n
);
71
primeNumbers_
.push_back(m);
72
return
m;
73
}
74
75
}
n
Size n
Definition:
andreasenhugevolatilityinterpl.cpp:47
QuantLib::PrimeNumbers::nextPrimeNumber
static BigNatural nextPrimeNumber()
Definition:
primenumbers.cpp:58
QuantLib::PrimeNumbers::get
static BigNatural get(Size absoluteIndex)
Get and store one after another.
Definition:
primenumbers.cpp:47
QuantLib::PrimeNumbers::primeNumbers_
static std::vector< BigNatural > primeNumbers_
Definition:
primenumbers.hpp:52
QuantLib::Real
QL_REAL Real
real number
Definition:
types.hpp:50
QuantLib::Size
std::size_t Size
size of a container
Definition:
types.hpp:58
QuantLib
Definition:
any.hpp:35
QuantLib::BigNatural
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition:
types.hpp:46
primenumbers.hpp
Prime numbers calculator.
Generated by
Doxygen
1.9.5