4 # test recursive functions.
17 return gcd($_[0] - $_[1], $_[1]) if ($_[0] > $_[1]);
18 return gcd($_[0], $_[1] - $_[0]) if ($_[0] < $_[1]);
23 $_[0] < 2 ? 1 : $_[0] * factorial($_[0] - 1);
27 $_[0] < 2 ? 1 : fibonacci($_[0] - 2) + fibonacci($_[0] - 1);
30 # Highly recursive, highly aggressive.
31 # Kids, don't try this at home.
33 # For example ackermann(4,1) will take quite a long time.
34 # It will simply eat away your memory. Trust me.
37 return $_[1] + 1 if ($_[0] == 0);
38 return ackermann($_[0] - 1, 1) if ($_[1] == 0);
39 ackermann($_[0] - 1, ackermann($_[0], $_[1] - 1));
42 # Highly recursive, highly boring.
46 takeuchi(takeuchi($_[0] - 1, $_[1], $_[2]),
47 takeuchi($_[1] - 1, $_[2], $_[0]),
48 takeuchi($_[2] - 1, $_[0], $_[1]))
52 is(gcd(1147, 1271), 31, "gcd(1147, 1271) == 31");
54 is(gcd(1908, 2016), 36, "gcd(1908, 2016) == 36");
56 is(factorial(10), 3628800, "factorial(10) == 3628800");
58 is(factorial(factorial(3)), 720, "factorial(factorial(3)) == 720");
60 is(fibonacci(10), 89, "fibonacci(10) == 89");
62 is(fibonacci(fibonacci(7)), 17711, "fibonacci(fibonacci(7)) == 17711");
64 my @ack = qw(1 2 3 4 2 3 4 5 3 5 7 9 5 13 29 61);
68 my $a = ackermann($x, $y);
69 is($a, shift(@ack), "ackermann($x, $y) == $a");
73 my ($x, $y, $z) = (18, 12, 6);
75 is(takeuchi($x, $y, $z), $z + 1, "takeuchi($x, $y, $z) == $z + 1");
83 return [curr_test] unless $_[0];
84 my $u = get_first1(0);
87 my $x = get_first1(1);
88 ok($x, "premature FREETMPS (change 5699)");
93 return get_list2(@_)->[0];
97 return [curr_test] unless $_[0];
98 my $u = get_first2(0);
101 my $x = get_first2(1);
102 ok($x, "premature FREETMPS (change 5699)");
106 local $^W = 0; # We do not need recursion depth warning.
109 return $_[0] + ($_[0] > 0 ? sillysum($_[0] - 1) : 0);
112 is(sillysum(1000), 1000*1001/2, "recursive sum of 1..1000");
115 # check ok for recursion depth > 65536
122 prog => q{$d=0; $e=1; sub c { ++$d; if ($d > 66000) { $e=0 } else { c(); c() unless $d % 32768 } --$d } c(); exit $e});
125 skip("Out of memory -- increase your data/heap?", 2)
126 if $r =~ /Out of memory/i;
127 is($r, '', "64K deep recursion - no output expected");
128 is($?, 0, "64K deep recursion - no coredump expected");