Want more features on Pastebin? Sign Up, it's FREE!
Guest

McCarthy's 91 Function - Recursive

By: a guest on Jan 7th, 2014  |  syntax: Perl  |  size: 0.49 KB  |  views: 27  |  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.     print tostring($n, $level)."\n";
  16.     return if $level == 0;
  17.  
  18.     mprint($n+11, $level+1) if $n <= 100;
  19.     mprint($n-10, $level-1) if $n > 100;
  20. }
  21.  
  22. sub tostring {
  23.     my $n = shift;
  24.     my $level = shift;
  25.  
  26.     return "$n" if $level <= 0;
  27.     return "M($n)" if $level == 1;
  28.     return "M(".tostring($n, $level-1).")";
  29. }
clone this paste RAW Paste Data