Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: OT: Finding Factor Closest To Square Root

by danaj (Friar)
on Oct 08, 2014 at 16:35 UTC ( [id://1103189]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    srand(1);  # So everything is using the same numbers
    my $s = 0;
    for (1..1000000) {
      $s += nearest_sqrt(int(rand(2**47)));
    }
    print "$s\n";
    
  2. or download this
    use ntheory qw/divisors/;
    sub closest {
      my @d = divisors(shift);
      $d[ $#d >> 1 ];
    }
    
  3. or download this
    use ntheory qw/fordivisors/;
    sub closest {
      my($n,$sqrtn,$min) = ($_[0], int(sqrt($_[0])));
      fordivisors { $min = $_ if $_ <= $sqrtn; } $n;
      $min;
    }
    
  4. or download this
    use ntheory qw/factor/;
    sub closest {
      my($n) = @_;
    ...
      # 4. Return the largest divisor <= sqrt n.
      $d[ $#d >> 1 ];
    }
    
  5. or download this
    use Math::Pari qw/divisors/;
    sub closest {
      my $d = divisors(shift);
      $d->[ $#$d >> 1 ];
    }
    
  6. or download this
    use Math::Factor::XS qw/factors/;
    sub closest {
      my @d = (1, factors($_[0]), $_[0]);
      $d[ $#d >> 1 ];
    }
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found