SHARE
TWEET

McCarthy's 91 Function - Non Recursive

a guest Jan 7th, 2014 30 Never
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $n = <STDIN>;
  7. chomp($n);
  8.  
  9. mprint($n, 1);
  10.  
  11. sub mprint {
  12.     my $n = shift;
  13.     my $level = shift;
  14.  
  15.     while($level > 0) {
  16.         print tostring($n, $level)."\n";
  17.         if($n <= 100) { $n += 11; $level++; }
  18.         else { $n -= 10; $level--; }
  19.     }
  20.  
  21.     print tostring($n, $level)."\n";
  22. }
  23.  
  24. sub tostring {
  25.     my $n = shift;
  26.     my $level = shift;
  27.  
  28.     return "$n" if $level <= 0;
  29.     return "M($n)" if $level == 1;
  30.     return "M(".tostring($n, $level-1).")";
  31. }
RAW Paste Data
Top