improved simple factorization

This commit is contained in:
Jiri Pittner 2023-12-28 23:57:02 +01:00
parent c6a9a8e456
commit 40b23f19d6
2 changed files with 5 additions and 5 deletions

View File

@ -22,12 +22,12 @@
namespace LA { namespace LA {
template<typename N> template<typename N>
N primefactor(const N &x) N primefactor(const N &x, const N &last)
{ {
N i,t; N i,t;
if ( x <= 2 ) return x; if ( x <= 2 ) return x;
if ( !(x & 1) ) return 2; if ( !(x & 1) ) return 2;
i = 3; i = last >3 ? last : 3;
for (;;) { for (;;) {
t = x / i; t = x / i;
if ( t < i ) break; if ( t < i ) break;
@ -47,7 +47,7 @@ N y=x;
N last=0; N last=0;
while(y>1) while(y>1)
{ {
N z=primefactor(y); N z=primefactor(y,last);
if(z!=last) if(z!=last)
{ {
std::pair<N,N> p; std::pair<N,N> p;
@ -121,7 +121,7 @@ return y;
//force instantization //force instantization
#define INSTANTIZE(N) \ #define INSTANTIZE(N) \
template N primefactor(const N &x); \ template N primefactor(const N &x, const N &last); \
template FACTORIZATION<N> factorization(const N &x); \ template FACTORIZATION<N> factorization(const N &x); \
template N nextprime(N x); \ template N nextprime(N x); \
template std::ostream & operator<<(std::ostream &s, const FACTORIZATION<N> &x); \ template std::ostream & operator<<(std::ostream &s, const FACTORIZATION<N> &x); \

View File

@ -31,7 +31,7 @@
namespace LA { namespace LA {
template<typename N> template<typename N>
N primefactor(const N &x); N primefactor(const N &x, const N &last=0);
template<typename N> template<typename N>
bool isprime(const N &x) {return x>1 && x==primefactor(x);} bool isprime(const N &x) {return x>1 && x==primefactor(x);}