Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 13, 2019 00:54
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 tomcha/df1e478bb7d6111cf84d06c98ece4d3f to your computer and use it in GitHub Desktop.
Save tomcha/df1e478bb7d6111cf84d06c98ece4d3f to your computer and use it in GitHub Desktop.
最大公約数をユークリッドの互除法を使って再帰で実装する
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use DDP { deparse => 1 };
print "整数2つを入力して下さい i,j >>";
chomp(my $input = <STDIN>);
my ($i, $j) = split(/,/, $input);
say get_gcd($i, $j);
sub get_gcd{
my $x = shift;
my $y = shift;
my $mod = $x % $y;
my $result;
if ($mod == 0){
return $y;
} else {
return get_gcd($y, $mod);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment