Factorization Perl Script Update

Advertisements

I cleaned up the code for cf_exam.pl just now. Making the script better also made it a little shorter, but I doubt that it is any faster. Anyway, have at it!

#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw/ceil/;

my $m = $ARGV[0];
my ( $r_cand, $s_cand, $rc_ceil, $n_iters, $factor );

$n_iters = 1;
$s_cand = ceil( sqrt( $m ) );
$r_cand = $s_cand * $s_cand - $m;
# print "$s_cand,$r_cand\n";
$rc_ceil = ceil( sqrt( $r_cand ) );
while ( ( $rc_ceil * $rc_ceil ) > $r_cand ) {
  $s_cand++;
  $r_cand = $s_cand * $s_cand - $m;
  $rc_ceil = ceil( sqrt( $r_cand ) );
  $n_iters++;
}
$factor = $s_cand - $rc_ceil;
print "\n$m = $factor * ", $m/$factor, ".\n";;
print "Factorization found in $n_iters iterations.\n";

Leave a ReplyCancel reply

Discover more from JCSBimp's Hexagonal Orthogonal

Subscribe now to keep reading and get access to the full archive.

Continue reading