blib/lib/Math/Fibonacci.pm

filter: Statementstmt 92.5% 37 / 40 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 50.0% 4 / 8 totaltotal 83.1% 59 / 71 CC 7 SCAR 9.5
CC
7
Cov
88%
a_term
6.0
decomp
4.0
isfibonacci
2.0
CRAP 2.6
↑ Summary
1 #!/usr/bin/perl
2 #
3 # Computes the Fibonacci sequence using the fast algorithm: F(n) ~ g^n/sqrt(5),
4 # where g is the golden ratio and ~ stands for "take the nearest integer."
5 #
6 # Copyright (c) 1999-2000, Vipul Ved Prakash <mail@vipul.net>
7 # This code is free software distributed under the same license as Perl itself.
8 # $Id: Fibonacci.pm,v 1.5 2001/04/28 20:41:15 vipul Exp $
9  
10 package Math::Fibonacci;
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POSIX qw(log10 ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 require Exporter;
15 2 @ISA = qw(Exporter);
16 2 ( $VERSION ) = '$Revision: 1.5 $' =~ /\s(\d+\.\d+)\s/;
17  
18 2 @EXPORT_OK = qw(term series decompose isfibonacci);
19  
20 sub g () { "1.61803398874989" } # golden ratio
21  
22 51 ▶sub term { nearestint ((g ** shift) / sqrt(5)) } # nth term of the seq
Subroutine term: called CC=1 SCAR=0.0
Pod documented
23  
24 1 ▶sub series { return map(term($_), 1..shift) } # n terms of the seq
Subroutine series: called CC=1 SCAR=0.0
Pod documented
25  
26  
27 1 ▶sub decompose { # decomposes any integer into the sum of
Subroutine decompose: called CC=1 SCAR=0.0
Pod documented
28 # members of the fibonacci sequence.
29 1 my ($int) = @_;
30 1 my $sum = decomp ($int);
31 1 return @$sum;
32  
33 }
34  
35 9 ▶sub decomp {
Subroutine decomp: called CC=4 SCAR=13.9
Pod undocumented
36 9 my ($a, $sum) = @_;
37 9 my $n = nearestint ((log10($a) + log10(sqrt(5)))/log10(g));
38 9 my $fibn = term($n);
39 9 ▶ if ( $fibn == $a ) { push @$sum, $a; return $sum }
Branch: 3 branches
BranchTrueFalseTotal
if ($fibn == $a) { } 1 8 9
elsif ($fibn < $a) { } 4 4 8
elsif ($a < $fibn) { } 4 0 4
40 4 elsif ( $fibn < $a ) { push @$sum, $fibn; decomp( $a-$fibn, $sum ) }
41 4 elsif ( $a < $fibn ) { my $fibn1 = term($n-1); push @$sum, $fibn1;
42 4 decomp( $a - $fibn1, $sum ) }
43 };
44  
45  
46 14 ▶sub isfibonacci {
Subroutine isfibonacci: called CC=2 SCAR=6.9
Pod documented
47
48 14 my $a = shift;
49 14 my $n = nearestint ((log10($a) + log10(sqrt(5)))/log10(g));
50 14 ▶ return $a == term($n) ? $n : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a == term($n) ? : 12 2 14
51  
52 }
53  
54 74 ▶sub nearestint {
Subroutine nearestint: called CC=2 SCAR=6.9
Pod undocumented
55 74 my $v = shift;
56 74 my $f = floor($v); my $c = ceil($v);
57 74 ▶ ($v-$f) < ($c-$v) ? $f : $c;
Branch: 1 branch
BranchTrueFalseTotal
$v - $f < $c - $v ? : 36 38 74
58 }
59  
60  
61 # routines to implement term and series with the familiar additive algorithm.
62  
63 0 ▶sub a_term { return $_[0] < 3 ? 1 : a_term($_[0]-1) + a_term ($_[0]-2) }
Subroutine a_term: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 3 ? : 0 0 0
64  
65 0 ▶sub a_series {
Subroutine a_series: not called CC=1 SCAR=6.9
Pod undocumented
66 0 my @series = map(a_term($_), 1..shift);
67 0 \@series;
68 }
69  
70  
71 2 1;
72  
73  
74 =head1 NAME
75  
76 Math::Fibonacci - Fibonacci numbers.
77  
78 =head1 VERSION
79  
80 $Revision: 1.5 $
81  
82 =head1 SYNOPSIS
83  
84 use Math::Fibonacci qw(term series decompose);
85  
86 my $term = term ( 42 );
87 my @series = series ( 42 );
88 my @sum = decompose ( 65535 );
89  
90 =head1 DESCRIPTION
91  
92 This module provides a few functions related to Fibonacci numbers.
93  
94 =head1 EXPORTS ON REQUEST
95  
96 term(), series() decompose(), isfibonacci()
97  
98 =head1 FUNCTIONS
99  
100 =over 4
101  
102 =item B<term($n)>
103  
104 Returns the $n-th term of the Fibonacci sequence. The term is computed
105 using the fast algorithm: C<F(n) ~ g^n/sqrt(5)>, where g is the golden
106 ratio and ~ means "take the nearest integer".
107  
108 =item B<series($n)>
109  
110 Computes and returns the first $n Fibonacci numbers.
111  
112 =item B<decompose($int)>
113  
114 Decomposes $int into the sum of Fibonacci numbers. Returns the list of
115 Fibonacci numbers.
116  
117 =item B<isfibonacci($int)>
118  
119 Returns the sequence number of $int if it is a Fibonacci number or a
120 non-true value if it is not.
121  
122 =head1 AUTHOR
123  
124 Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>
125  
126 =head1 LICENSE
127  
128 Copyright (c) 1999-2001, Vipul Ved Prakash.
129  
130 This code is free software; you can redistribute it and/or modify it under
131 the ARTISTIC license (a copy is included in the distribution) or under the
132 same terms as Perl itself.
133  
134 =cut
135  
↑ Summary