Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
is there any fastest algorithm to find factors of given number?
in the range of 10^9...
less than O(n)
for(i=2;i<=n/2;i++)
{
if(n%i==0)
printf(i);
}
Posted
Comments
PIEBALDconsult 5-Oct-13 13:24pm    
You only need to go as far as the square root. Also, other than the square root, when you find one you've found two, so print both.
Vivek_Maruxxxx 16-Oct-13 7:17am    
its not the right way to do that.

 
Share this answer
 
You could, for instance, iterate over a sequence of pre-computed prime numbers.
 
Share this answer
 
Comments
PIEBALDconsult 5-Oct-13 13:52pm    
I assume he wants all factors; not just the prime factors.
CPallini 5-Oct-13 15:09pm    
Well, all factors are prime numbers...
PIEBALDconsult 5-Oct-13 18:06pm    
Are they?
Andreas Gieriet 5-Oct-13 20:35pm    
When talking about factorization you usually assume prime factorization, since this is the hard problem. Once you have all prime factors, you can produce all the other (not prime) factors by do all permutations of the prime factors.
Cheers
Andi
PIEBALDconsult 6-Oct-13 1:00am    
Yet the code he presented finds all.


" you usually assume prime factorization"

No I don't.

How to know what "fastest" mean?
(a) fastest to execute in production mode
(b) fastest to prepare data (e.g. calculate a lookup table)
(c) fastest to develop

Assuming you aim for (a) and that you can spend any amount of time to develop it (i.e. (b) and (c)) and assuming you have sufficient storage space with adequate access speed, I would go for
1) during development, produce a lookup table for all 10^9 numbers ;-)
1.1) calculate all prime factors for a given number
1.2) produce all combinations of the prime factors to produce all factors
2) in the production, use that lookup table

E.g. for n = 63
1) prime factors: 3 x 3 x 7
2) combinations:
(3) x (3) x (7) = 3 x 3 x 7
(3  x  3) x (7) = 9 x 7
(3) x (3  x  7) = 3 x 21

Now you can think of space optimizations, but this will always go on the cost of execution speed. E.g. move 1.2 to production mode, or compress the table (arrange the table as a tree or partition and compress and only deflate the needed part, etc.), ...

Cheers
Andi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900