Perl: the Markov chain saw  
PerlMonks 
Re^2: OT: Finding Factor Closest To Square Rootby kvale (Monsignor) 
on Feb 19, 2005 at 01:21 UTC ( #432598=note: print w/ replies, xml )  Need Help?? 
You have the right of it. More precisely, take the logarithim. Then the problem becomes finding a sum of log prime factors that is closest to log(N)/2. This problem is reducible to the subset sum problem, which is NPhard. Although exact solutions are exponential, heuristics can sometimes generate good answers with minimal effort. One example of a heuristic that might work well is the greedy heuristic. To fill a box of size log(N)/2, put the largest factor into the box. Then put the next largest factor that still fits in the box and insert it. Keep doing this until no more factors fit. The above will get you the largest factor <= sqrt(N). To get the smallest factor >= sqrt(N), put all the factors in a box, and take them out again largest to smallest, until no more can be taken out. Finally, comapre the two answers to find the closest. Update: fixed a typo. Mark
In Section
Seekers of Perl Wisdom

