SUNphi
1.0
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
Factorize.hpp
Go to the documentation of this file.
1
#
ifndef
_FACTORIZE_HPP
2
#
define
_FACTORIZE_HPP
3
4
/// \file Factorize.hpp
5
///
6
/// \brief Factorizes a number with a simple algorithm
7
8
#
include
<
ios
/
Logger
.
hpp
>
9
10
#
include
<
vector
>
11
12
namespace
SUNphi
13
{
14
/// Factorizes a number with a simple algorithm
15
template
<
typename
I>
16
constexpr Vector<I>
factorize
(
I
n
)
///< Number to factorize
17
{
18
// Simple case with 0: must crash
19
if
(n<=0)
20
return
21
{};
22
23
// Simple case with 1 or 2: returns the number itself
24
if
(n<=2)
25
return
26
{n};
27
28
/// Result of the factorization
29
Vector<I> out;
30
31
/// List of all known primes before 100
32
constexpr std::array<I,25> primesList=
33
{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
34
35
/// Iterator to the initial value of the divisor
36
auto
dPtr=
37
primesList.begin();
38
39
/// Divisor
40
I d=
41
*dPtr;
42
43
// Loops until n is 1
44
while
(n!=1)
45
{
46
/// Dividend
47
const
I t=
48
n/d;
49
50
/// Remainder
51
const
I r=
52
n-t*d;
53
54
// If no remainder
55
if
(r==0)
56
{
57
// Store
58
out.push_back(d);
59
// Replace n with the dividend
60
n=
61
t;
62
}
63
else
64
// Increment the divisor
65
if
(d<primesList.back())
66
d=
67
(*(++dPtr));
68
else
69
d+=
70
2;
71
}
72
73
return
74
out;
75
}
76
77
/// Factorize grouping number
78
///
79
/// Example:
80
/// \code
81
/// factorizeGrouping(12); // {{2,2},{3,1}}
82
/// \endcode
83
template
<
typename
I>
84
std
::
map
<
I
,
I
>
factorizeGrouping
(
const
I
&
i
)
85
{
86
/// Ungrouped factors
87
const
Vector
<I> factors=
88
factorize(i);
89
90
return
91
factors.group();
92
}
93
}
94
95
#
endif
SUNphi::Vector::divWithMod
void divWithMod(Vector< TOut > "ient, Vector< TOut > &remainder, const Vector &divisor) const
Returns the result and remainder of the division.
Definition:
Vector.hpp:310
SUNphi::factorizeGrouping
std::map< I, I > factorizeGrouping(const I &i)
Definition:
Factorize.hpp:84
SUNphi::factorize
constexpr Vector< I > factorize(I n)
Factorizes a number with a simple algorithm.
Definition:
Factorize.hpp:16
include
math
Factorize.hpp
Generated by
1.8.11