Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: OT: Finding Factor Closest To Square Root

by talexb (Chancellor)
on Feb 19, 2005 at 00:58 UTC ( [id://432591]=note: print w/replies, xml ) Need Help??


in reply to Re^2: OT: Finding Factor Closest To Square Root
in thread OT: Finding Factor Closest To Square Root

If you get a list of prime factors in ascending order, then I would take every other member of the list, multiply them together and start with that.

In your example, that would give you 20, which isn't too far from the correct result of 31.

Hmm .. Actually, an even better answer would be

  • for a list with an odd number of elements, take all of the odd position numbers and multiply them together;
  • for a list with an even number of elements, take the odd numbered elements from 1 to (n/2)-1, and the even numbered elements from n down to (n/2)+2, and multiply them together with the harmonic mean of the (n-1)/2 and n/2 elements.

For your example, that would be 2 * sqrt(2*5) * 5, which turns out to be exactly the correct answer, 31.622..

To try out the odd number, we'll try out 2000, which gives us a list of (2, 2, 2, 2, 5, 5, 5) and a result of 2 * 2 * 5 * 5 or 100. Hmm, a little high.

Well, that's a fascinating question, and good luck with that.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re^4: OT: Finding Factor Closest To Square Root
by QM (Parson) on Feb 20, 2005 at 04:53 UTC
    For your example, that would be 2 * sqrt(2*5) * 5, which turns out to be exactly the correct answer, 31.622..
    I don't need the exact square root, but the largest (possibly composite) integer factor less than or equal to the square root.

    Cheers,

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Needless to say, but I'll say it anyway, a fascinating problem. Exactly the kind of thing that my Dad eats for breakfast (retired actuary and all that).

      After some thought, it seems clear that this is a tough nut to crack. If you start with a sorted list of factors, largest to smallest, and multiply values together, the solution is 25. If you drop the first value, you end up with 20. I can't prove it (not at 0630, anyway), but I expect there are cases where the solution is the product of the first, second and last factors.

      In the end, I think the best way to find out the answer is to figure out the integer that's closest to the square root, then go backwards to find the largest number made up of the factors. Assuming you don't want to actually calculate the suqare root, the first part of that can be a straightforward binary search. The second part is where it gets interesting .. you have to find the combination of factors whose product is closest to the approximate square root value you've determined.

      And that sounds an awful lot like re-starting the original problem. Ugh.

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        This discussion and my own (preliminary) benchmarking results have led me to rethink a few things about my real problem.

        For instance, when computing F(n), we can make use of the addition formula to break n into 2 pieces:

        F(n) = F(i+j) = F(i-1)*F(j) + F(i)*F(j+1)
        Then:
        j = k**2 = int(sqrt(n)) i = n - j F(n) = F(i+j) = F(i+k**2)
        which we can compute using the first formula above and
        F(k**2) = (F(k-1)+F(k+1))*F(k*(k-1)) - ((-1)**k)*F(k*(k-2))
        It remains to be seen whether all of this extra work buys anything.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://432591]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found