Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use feature qw(:5.10);
  5. use utf8;
  6.  
  7. my ($n1, $n2);
  8. {
  9. my $line = <STDIN>;
  10. ($n1, $n2) = split(/\s+/, $line);
  11. }
  12.  
  13. my $gcd = gcd($n1,$n2);
  14. my @divisor = divisor($gcd);
  15. my @uniq;
  16. {
  17. my %hash;
  18. $hash{$_}++ for @divisor;
  19. @uniq = keys %hash;
  20. }
  21. say @uniq+1;
  22.  
  23. sub divisor {
  24. my $gcd = shift;
  25. return if $gcd == 1;
  26. my $limit = sqrt($gcd);
  27. for (my $i=2;$i <= $limit;$i++){
  28. my $remainder = $gcd % $i;
  29. if ($remainder == 0){
  30. return $i, divisor($gcd/$i);
  31. }
  32. }
  33. return $gcd;
  34. }
  35. sub gcd {
  36. my @n = sort {$a <=> $b} @_;
  37. my $remainder = $n[1] % $n[0];
  38. return $n[0] if $remainder == 0;
  39. return gcd($n[0], $remainder);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement