Whoever suggested using the sieve of eratosthenes was being extremely dumb. You just need to factor your number; there's no reason to generate a long list of primes.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
I was the original person who suggested using a sieve of eratosthenes. Now does this still hold? I believe so and here is why.
You were required to find the factors. The only factors of importance are prime factors, all other factors will be bigger since they are a combination of two or more prime factors. Hence you only need to test prime factors.
You now need a way to generate prime factors upto (at a maximum) the sqrt of the number. Then test them.
The SIMPLEST TO CODE method of finding prime factors is the sieve of eratosthenes.
In addition, the SoE is progressive, so after finding a new prime, it is tested and if a factor, the target number is divided and if the new target number is less than the square of the newly found factor, then the new target number is prime and the problem is finished.
I did then suggest that testing if a number is prime is easier than factoring and it is a good idea to test the target number first (because if it is prime then that is the end of the problem). [but this is a refinement of the original problem solution]
If you look a typical Pollard Lenstra number sieves, code for finding prime factors you are at >1000+ lines, that is serious code.
So Rashakil Fol please explain to us WHAT else you would do? you have said that I am dumb now back it up with a little maths/code.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
So Rashakil Fol please explain to us WHAT else you would do? you have said that I am dumb now back it up with a little maths/code.
Just use successive trial division. There's no need to explicitly skip all the non-prime numbers.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
His original problem was that he was doing just THAT
use successive trial division. There's no need to explicitly skip all the non-prime numbers.
Obviously, if you can do that quicly with a number like 600851475143, you have a seriously quick computer. That is just and only just in the standard PC / half-hour range, for older hardware and 32bit hardward it is in the 2-3hour range.
The reason for the orginal post (2 week ago ?) was that arun_lisieux was running on a slowish machine, and wasn't getting any output after a couple of minutes and thought that his program was broken.
So I stand by my original statement. I feel that I haven't been shown to be extremely dumb in this instance. [Although I accept that I am often :) ]
p.s. If I had set this as an assignment and someone handed in a simple loop, I would accept it, then add an extra assignment next week to factor say 15241580725499173 and that would turn 30min into a year or so -- and make the lazy student do a lot of work in a short space of time, while the others just changed the constant in their code.
p.p.s A good factor program takes 0.078seconds on my PC to do the 15...3 number above, a SoE based program takes 1.8seconds.
Algorithm is everything.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
The sieve of eratosthenes is slower than trial division. It takes Omega(n log n) operations when implemented naively, and Omega(n) operations with the best algorithm, which is just as good as trial division-based factorization.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
I wish to appologise. I think I have been very dumb. The S.o.E method does have a fundermental draw back. The only advantage is that it keeps the inital search under the machine limit for a longer time. This is not sufficient to offset the cost.
A naive search without dividing the target number down is obviously wrong route.
so returning to the original posters problem:
Algorithm should be:
i loop over 2->sqrt(N);
while (!(N % i ))
{
store i;
N/=i;
}
Then store N.
Note the while you need to check that the number doesn't have two or more identical factors.
hmmm I learnt something useful..... (as well as use brain before posting.. although I keep forgetting that!)
Thanks Rashakil.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
Well, I was being a bit inaccurate when I said you were being "extremely dumb". Everybody is dumb from time to time.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177