good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: OT: Finding Factor Closest To Square Rootby Limbic~Region (Chancellor) |
on Feb 27, 2005 at 21:15 UTC ( [id://434927]=note: print w/replies, xml ) | Need Help?? |
QM,
I have spent far too many cycles on this. Though it may have been your intention, your original question was not: Given a number N, find the nearest factor of N less than or equal to the square root of N. For that question, tall_man's solution is quite concise, efficient, and elegant. Even though I used Math::Pari instead of Math::Big::Factors to get the prime factorization, I interpreted the question to be more about what came after to be important. My first, solution was straight forward and only had a %seen cache optimization, though I knew other optimizations were possible. Next, I came up with a Inline::C implementation of the first solution (minus the cache). It has turned out to be the fastest by far even without the cache. Since I was really interested in coming up with a Perl solution that had optimizations, I came up with a second solution. Actually, I came up with about 5 variations on tye's 3 simple rules for generating a powerset in progressive order. The problem in finding the fastest implementation was a strange bug that BrowserUk helped me work around. With that - here is my last attempt (ver_3.pl)
Cheers - L~R
In Section
Seekers of Perl Wisdom
|
|