Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by dragonchild (Archbishop)
on Feb 19, 2005 at 22:20 UTC ( [id://432783]=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

One way to do it, similar to talexb, would be (tested!)
use Math::Big::Factors; my $val = 2000; my @x = Math::Big::Factors::factors_wheel( $val ); print "@x\n"; my %factors; $factors{$_}++ for @x; use Data::Dumper; print Dumper \%factors; my $sqrt = 1; while (my ($k,$v) = each %factors) { while ($v > 1) { $sqrt *= $k; $v -= 2; } if ($v) { $sqrt *= sqrt( $k ); } } print "$sqrt => ", sqrt( $val ), $/;
This is going to be slower that just taking the sqrt of the value. Just to let you know ...

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^4: OT: Finding Factor Closest To Square Root
by QM (Parson) on Feb 20, 2005 at 05:08 UTC
    This would also give me a practical answer, though it could be off quite a bit for some cases.

    For instance, for N=1000, the factorization is 2**3 * 5**3. Your method gives 2**2 * 5**2 = 100, while the closest factor to the square root is 5**2 = 25 (paired with 40). I'd be happy with either 25 or 40.

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

      My method actually gives 2**1.5 * 5 ** 1.5, which is the actual square root of 1000. Coming up with 25 or 40 is going to be ... interesting. I can do further research into the mathematics behind it, if you want. (Number Theory is a pet project of mine.)

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        I can do further research into the mathematics behind it, if you want. (Number Theory is a pet project of mine.)
        I'd be interested in any insights you have. I keep poking at number theory ideas, but I never get anywhere.

        -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://432783]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found