Could someone help me with code optimization?
I've made a program that show number as a product of it's prime numbers. Unfortunately it's too slow with big numbers. Could you help me optimize it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
using namespace std;
int main()
{
int liczba;
while (cin >> liczba){
int l,b;
l=b;
for (int i=2; i <= liczba; i++)
{
while(liczba % i == 0)
{
liczba /= i;
l=l/i;
cout << i;
if (liczba>1)
cout <<"*";
}
}
cout <<endl;
}
}
|
>
for (int i=2; i <= liczba; i++)
Consider to test only the primes number that are less or equal than sqrt(liczba)
1 2 3 4
|
for(auto p: prime_list){
if(p*p > liczba) break;
//factorize
}
|
Topic archived. No new replies allowed.