From 40b23f19d6906994ed1f6392f46d927123568699 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Thu, 28 Dec 2023 23:57:02 +0100 Subject: [PATCH] improved simple factorization --- numbers.cc | 8 ++++---- numbers.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/numbers.cc b/numbers.cc index ac8c217..4a829ad 100644 --- a/numbers.cc +++ b/numbers.cc @@ -22,12 +22,12 @@ namespace LA { template -N primefactor(const N &x) +N primefactor(const N &x, const N &last) { N i,t; if ( x <= 2 ) return x; if ( !(x & 1) ) return 2; - i = 3; + i = last >3 ? last : 3; for (;;) { t = x / i; if ( t < i ) break; @@ -47,7 +47,7 @@ N y=x; N last=0; while(y>1) { - N z=primefactor(y); + N z=primefactor(y,last); if(z!=last) { std::pair p; @@ -121,7 +121,7 @@ return y; //force instantization #define INSTANTIZE(N) \ -template N primefactor(const N &x); \ +template N primefactor(const N &x, const N &last); \ template FACTORIZATION factorization(const N &x); \ template N nextprime(N x); \ template std::ostream & operator<<(std::ostream &s, const FACTORIZATION &x); \ diff --git a/numbers.h b/numbers.h index da3a9d7..bc70856 100644 --- a/numbers.h +++ b/numbers.h @@ -31,7 +31,7 @@ namespace LA { template -N primefactor(const N &x); +N primefactor(const N &x, const N &last=0); template bool isprime(const N &x) {return x>1 && x==primefactor(x);}