I need to determine the highest common factor for a series of integers.
I have scribed an initial stab included below - does anyone have a
better HCF implementation, or any improvements to suggest?

TIA
Jeff

#!/usr/local/bin/perl -w
use strict;

my ( $one, $two ) = @ARGV;
$one = 37894060279 unless $one;
$two = 18272779829 unless $two;
# answer of defaults should be 150991
print "HCF of $one, $two is " . maxFactor( $one, $two ) . "\n";


# return highest common factor for a pair of numbers
sub maxFactor {
  my ( $one, $two ) = @_;
  return $one if $one == $two;

  # private sub: never feed it zeros!
  sub _getFactors {
    my ($loNum, $hiNum) = @_; ## sort {$a,=>$b} @_;
    my $remainder = $hiNum % $loNum;
    my $loFactor  = int( $hiNum / $loNum );
    my $hiFactor  = ($hiNum - $remainder ) / $loFactor;
    return ( $loFactor, $hiFactor, $remainder );
  }

  my ( $loFactor, $hiFactor, $remainder ) = _getFactors( sort {$a<=>$b}
($one, $two) );
  while ( $remainder ) {
    ( $loFactor, $hiFactor, $remainder ) = _getFactors( $remainder,
$hiFactor );
  }
  return $hiFactor;

}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to