Skip to content

Instantly share code, notes, and snippets.

@yaasita
Created September 29, 2019 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaasita/fde662593ce9187adfb8de3d64cc4b23 to your computer and use it in GitHub Desktop.
Save yaasita/fde662593ce9187adfb8de3d64cc4b23 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(:5.10);
use utf8;
my ($n1, $n2);
{
my $line = <STDIN>;
($n1, $n2) = split(/\s+/, $line);
}
my $gcd = gcd($n1,$n2);
my @divisor = divisor($gcd);
my @uniq;
{
my %hash;
$hash{$_}++ for @divisor;
@uniq = keys %hash;
}
say @uniq+1;
sub divisor {
my $gcd = shift;
return if $gcd == 1;
my $limit = sqrt($gcd);
for (my $i=2;$i <= $limit;$i++){
my $remainder = $gcd % $i;
if ($remainder == 0){
return $i, divisor($gcd/$i);
}
}
return $gcd;
}
sub gcd {
my @n = sort {$a <=> $b} @_;
my $remainder = $n[1] % $n[0];
return $n[0] if $remainder == 0;
return gcd($n[0], $remainder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment