first commit
This commit is contained in:
35
perl/Examples/Chap5/fib-11
Normal file
35
perl/Examples/Chap5/fib-11
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
|
||||
###
|
||||
### fib11
|
||||
###
|
||||
|
||||
## Chapter 5 section 4.3.1
|
||||
|
||||
sub fib {
|
||||
my $n = shift;
|
||||
my ($s1, $return);
|
||||
my $BRANCH = 0;
|
||||
my @STACK;
|
||||
while (1) {
|
||||
if ($n < 2) {
|
||||
$return = $n;
|
||||
} else {
|
||||
if ($BRANCH == 0) {
|
||||
push (@STACK, [ $BRANCH, 0, $n ]), $n -= 1 while $n >= 2;
|
||||
$return = $n;
|
||||
} elsif ($BRANCH == 1) {
|
||||
push @STACK, [ $BRANCH, $return, $n ];
|
||||
$n -= 2;
|
||||
$BRANCH = 0;
|
||||
next;
|
||||
} elsif ($BRANCH == 2) {
|
||||
$return += $s1;
|
||||
}
|
||||
}
|
||||
|
||||
return $return unless @STACK;
|
||||
($BRANCH, $s1, $n) = @{pop @STACK};
|
||||
$BRANCH++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user