Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

McCarthy's 91 Function - Non Recursive

By: a guest on Jan 7th, 2014  |  syntax: Perl  |  size: 0.53 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }