Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by tall_man (Parson)
on Feb 22, 2005 at 20:44 UTC ( [id://433487]=note: print w/replies, xml ) Need Help??


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

Here is a simple version of the code. It just enumerates the divisors and binary-searches them:
#! /usr/bin/perl use strict; use Math::Pari qw(:int factorint sqrtint divisors); for (@ARGV) { my $N = $_; my $a = factorint($N); my $sqrt = sqrtint($N); my $div = divisors($a); my $len = Math::Pari::matsize($div)->[1]; my $low = 0; my $high = $len - 1; my $mid; while (1) { $mid = ($low + $high) >> 1; last unless $low < $high; if ($div->[$mid] > $sqrt) { $high = $mid - 1; } else { last if $low == $mid; $low = $mid; } } print "Result for $N is ",$div->[$mid],"\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-24 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found