patterncppMinor
Optimization - Prime Factorizations of Numbers <= 10^7
Viewed 0 times
factorizationsoptimizationnumbersprime
Problem
I am using Trail Division Method with a pre-calculated list of primes to calculate the prime factorization of all numbers less than M (M
My code
Timing
My Code
Igor ostrovsky Code
- For my approach what other optimizations can i do?
My code
#include
#include
#include
#include
#include "math.h"
#include "stdio.h"
using namespace std;
const int lim=45000;
const int Max=10000000;
char prime[lim];
vector > PF[Max];
void prep()
{
//Calculation of Prime Numbers
for(int i=1;i0)
PF[i].push_back( make_pair(2,pq) );
int pan=num;
//Loop for all primes j such that j*j0)
PF[i].push_back( make_pair(j,pq) );
}
}
if(num>1)
PF[i].push_back( make_pair(num,1) );
}
}
main()
{
prep();
}Timing
My Code
real 0m36.841s
user 0m36.624s
sys 0m0.265sIgor ostrovsky Code
real 0m41.628s
user 0m41.390s
sys 0m0.265sSolution
I did this a while ago, in the interest of reducing ratios of factorials
I believe it will be much faster than yours, because I was able to avoid ever using division (
(It's about 6x faster with g++ than the code in the question, and the actual factor generation part is marginally faster, about 10%, than lol4t0's. Returning factors in a
Test program: http://ideone.com/pt9nu
Original application:
#include
#include
std::vector > factor_table;
void fill_sieve( int n )
{
factor_table.resize(n+1);
for( int i = 1; i (i, 1);
else
factor_table[i] = std::pair(2, i>>1);
}
for( int j = 3, j2 = 9; j2 (j, i);
++i;
ij += j;
}
}
j2 += (j + 1) factor( int num )
{
std::vector factors;
factors.reserve(30);
do {
factors.push_back(factor_table[num].first);
num = factor_table[num].second;
} while (num != 1);
return factors;
}I believe it will be much faster than yours, because I was able to avoid ever using division (
/ and %). As a matter of fact, I don't use any multiplies either.(It's about 6x faster with g++ than the code in the question, and the actual factor generation part is marginally faster, about 10%, than lol4t0's. Returning factors in a
std::vector takes 5x as long as the factor generation.)Test program: http://ideone.com/pt9nu
Original application:
- http://ideone.com/Weeg6
Code Snippets
#include <utility>
#include <vector>
std::vector< std::pair<int, int> > factor_table;
void fill_sieve( int n )
{
factor_table.resize(n+1);
for( int i = 1; i <= n; ++i ) {
if (i & 1)
factor_table[i] = std::pair<int, int>(i, 1);
else
factor_table[i] = std::pair<int, int>(2, i>>1);
}
for( int j = 3, j2 = 9; j2 <= n; ) {
if (factor_table[j].second == 1) {
int i = j;
int ij = j2;
while (ij <= n) {
factor_table[ij] = std::pair<int, int>(j, i);
++i;
ij += j;
}
}
j2 += (j + 1) << 2;
j += 2;
}
}
std::vector<int> factor( int num )
{
std::vector<int> factors;
factors.reserve(30);
do {
factors.push_back(factor_table[num].first);
num = factor_table[num].second;
} while (num != 1);
return factors;
}Context
StackExchange Code Review Q#9052, answer score: 4
Revisions (0)
No revisions yet.