4 # test recursive functions.
10 return gcd($_[0] - $_[1], $_[1]) if ($_[0] > $_[1]);
11 return gcd($_[0], $_[1] - $_[0]) if ($_[0] < $_[1]);
16 $_[0] < 2 ? 1 : $_[0] * factorial($_[0] - 1);
20 $_[0] < 2 ? 1 : fibonacci($_[0] - 2) + fibonacci($_[0] - 1);
23 # Highly recursive, highly aggressive.
24 # Kids, don't try this at home.
26 # For example ackermann(4,1) will take quite a long time.
27 # It will simply eat away your memory. Trust me.
30 return $_[1] + 1 if ($_[0] == 0);
31 return ackermann($_[0] - 1, 1) if ($_[1] == 0);
32 ackermann($_[0] - 1, ackermann($_[0], $_[1] - 1));
35 # Highly recursive, highly boring.
39 takeuchi(takeuchi($_[0] - 1, $_[1], $_[2]),
40 takeuchi($_[1] - 1, $_[2], $_[0]),
41 takeuchi($_[2] - 1, $_[0], $_[1]))
45 print 'not ' unless (($d = gcd(1147, 1271)) == 31);
47 print "# gcd(1147, 1271) = $d\n";
49 print 'not ' unless (($d = gcd(1908, 2016)) == 36);
51 print "# gcd(1908, 2016) = $d\n";
53 print 'not ' unless (($f = factorial(10)) == 3628800);
55 print "# factorial(10) = $f\n";
57 print 'not ' unless (($f = factorial(factorial(3))) == 720);
59 print "# factorial(factorial(3)) = $f\n";
61 print 'not ' unless (($f = fibonacci(10)) == 89);
63 print "# fibonacci(10) = $f\n";
65 print 'not ' unless (($f = fibonacci(fibonacci(7))) == 17711);
67 print "# fibonacci(fibonacci(7)) = $f\n";
71 @ack = qw(1 2 3 4 2 3 4 5 3 5 7 9 5 13 29 61);
75 $a = ackermann($x, $y);
76 print 'not ' unless ($a == shift(@ack));
77 print "ok ", $i++, "\n";
78 print "# ackermann($x, $y) = $a\n";
82 ($x, $y, $z) = (18, 12, 6);
84 print 'not ' unless (($t = takeuchi($x, $y, $z)) == $z + 1);
85 print "ok ", $i++, "\n";
86 print "# takeuchi($x, $y, $z) = $t\n";
94 return [24] unless $_[0];
95 my $u = get_first1(0);
98 my $x = get_first1(1);
104 return get_list2(@_)->[0];
108 return [25] unless $_[0];
109 my $u = get_first2(0);
112 my $x = get_first2(1);