Created
June 19, 2012 17:35
-
-
Save doherty/2955461 to your computer and use it in GitHub Desktop.
Create a directory tree representing the recursive calls for generating a Fibonacci number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use File::pushd qw(pushd); | |
use File::Path qw(make_path); | |
=head1 NAME | |
Fib-tree.pl - Create a directory tree representing the recursive calls for generating a Fibonacci number | |
=head1 DESCRIPTION | |
This is useful to generate a directory tree representing the recursive calls for | |
generating a Fibonacci number. You might be able to use that to illustrate how | |
sub-trees are repeated, yielding inefficiency. | |
It is also useful for creating directory trees that can easily be represented in | |
plain text by the Linux program C<tree> so that when your professor asks you to | |
generate such trees, you don't have to do it by hand. | |
=head1 USAGE | |
perl Fib-tree.pl 5 | |
=cut | |
sub fib { | |
my $num = shift; | |
my $dirname = "fib($num)"; | |
make_path($dirname); | |
my $dir = pushd($dirname); | |
if ($num == 0) { | |
return 1; | |
} | |
elsif ($num == 1) { | |
return 1; | |
} | |
else { | |
return fib($num-1) + fib($num-2); | |
} | |
} | |
my $num = shift; | |
fib($num); | |
=head1 AUTHOR & COPYRIGHT | |
Mike Doherty <doherty@cs.dal.ca> | |
Licensed under the GPL, version 2. | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment