Math-Fibonacci-1.5/0040755000076600007660000000000007272625372013237 5ustar vipulvipulMath-Fibonacci-1.5/Makefile.PL0100644000076600007660000000103507272624747015212 0ustar vipulvipul#!/usr/bin/perl -sw ## ## Makefile.PL for Math::Fibonacci ## ## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: Makefile.PL,v 1.2 2001/04/28 20:36:55 vipul Exp $ use ExtUtils::MakeMaker; WriteMakefile( 'NAME' => 'Math::Fibonacci', 'AUTHOR' => 'Vipul Ved Prakash ', 'VERSION_FROM' => 'lib/Math/Fibonacci.pm', 'PREREQ_PM' => { } ); Math-Fibonacci-1.5/Changes0100644000076600007660000000030407272613540014516 0ustar vipulvipul CHANGELOG for Math::Fibonacci $Id: Changes,v 1.1.1.1 2001/04/28 19:17:52 vipul Exp $ -------------------------------------------------------------------------- Math-Fibonacci-1.5/ARTISTIC0100644000076600007660000001220207272613540014370 0ustar vipulvipul The "Artistic License" Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. "Copyright Holder" is whoever is named in the copyright or copyrights for the package. "You" is you, if you're thinking about copying or distributing this Package. "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Math-Fibonacci-1.5/MANIFEST0100644000076600007660000000030107272625114014351 0ustar vipulvipul # Files in Math-Fibonacci bundle. MANIFEST ARTISTIC TODO Changes Makefile.PL lib/Math/Fibonacci.pm t/01-basic.t t/02-decompose.t # $Id: MANIFEST,v 1.2 2001/04/28 20:38:36 vipul Exp $ Math-Fibonacci-1.5/t/0040755000076600007660000000000007272625372013502 5ustar vipulvipulMath-Fibonacci-1.5/t/01-basic.t0100755000076600007660000000135007272613540015157 0ustar vipulvipul#!/usr/bin/perl -sw ## ## 01-basic.t ## ## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: 01-basic.t,v 1.1.1.1 2001/04/28 19:17:52 vipul Exp $ use Test; use lib '../lib'; use Math::Fibonacci qw(term series decompose isfibonacci); BEGIN { plan tests => 10 }; ok(term(2),1); ok(term(10),55); ok(term(42),267914296); ok(term(31),1346269); ok("@{[series(20)]}", "1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765"); ok(isfibonacci(3),4); ok(isfibonacci(233),13); ok(isfibonacci(267914296),42); ok(isfibonacci(15),0); ok(isfibonacci(65535),0); # my @y = decompose (4200); # print "@y\n"; Math-Fibonacci-1.5/t/02-decompose.t0100755000076600007660000000071307272613540016057 0ustar vipulvipul#!/usr/bin/perl -sw ## ## 01-basic.t ## ## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: 02-decompose.t,v 1.1.1.1 2001/04/28 19:17:52 vipul Exp $ use Test; use lib '../lib'; use Math::Fibonacci qw(decompose isfibonacci); BEGIN { plan tests => 9 }; my @d = decompose (9372883); for (@d) { ok(isfibonacci($_)); }; Math-Fibonacci-1.5/TODO0100644000076600007660000000137107272613540013720 0ustar vipulvipul TODO for Math::Fibonacci $Id: TODO,v 1.1.1.1 2001/04/28 19:17:52 vipul Exp $ ------------------------------------------------------------------------------- P No Task Status - -- ------------------------------------------------ ------------------------ * 1 Should be able to decompose Math::Pari integers * 2 Should return Math::Pari integers if the return value is too big to fit in a perl int. ------------------------------------------------------------------------------- LEGEND - Date in the Status field indicates the task was completed on that date. - `*' in P field distinguishes pending tasks. - `+' before the task description indicates a sub-task. Math-Fibonacci-1.5/lib/0040755000076600007660000000000007272625372014005 5ustar vipulvipulMath-Fibonacci-1.5/lib/Math/0040755000076600007660000000000007272625372014676 5ustar vipulvipulMath-Fibonacci-1.5/lib/Math/Fibonacci.pm0100644000076600007660000000624707272625353017116 0ustar vipulvipul#!/usr/bin/perl # # Computes the Fibonacci sequence using the fast algorithm: F(n) ~ g^n/sqrt(5), # where g is the golden ratio and ~ stands for "take the nearest integer." # # Copyright (c) 1999-2000, Vipul Ved Prakash # This code is free software distributed under the same license as Perl itself. # $Id: Fibonacci.pm,v 1.5 2001/04/28 20:41:15 vipul Exp $ package Math::Fibonacci; use strict; use vars qw($VERSION @ISA @EXPORT_OK); use POSIX qw(log10 ceil floor); require Exporter; @ISA = qw(Exporter); ( $VERSION ) = '$Revision: 1.5 $' =~ /\s(\d+\.\d+)\s/; @EXPORT_OK = qw(term series decompose isfibonacci); sub g () { "1.61803398874989" } # golden ratio sub term { nearestint ((g ** shift) / sqrt(5)) } # nth term of the seq sub series { return map(term($_), 1..shift) } # n terms of the seq sub decompose { # decomposes any integer into the sum of # members of the fibonacci sequence. my ($int) = @_; my $sum = decomp ($int); return @$sum; } sub decomp { my ($a, $sum) = @_; my $n = nearestint ((log10($a) + log10(sqrt(5)))/log10(g)); my $fibn = term($n); if ( $fibn == $a ) { push @$sum, $a; return $sum } elsif ( $fibn < $a ) { push @$sum, $fibn; decomp( $a-$fibn, $sum ) } elsif ( $a < $fibn ) { my $fibn1 = term($n-1); push @$sum, $fibn1; decomp( $a - $fibn1, $sum ) } }; sub isfibonacci { my $a = shift; my $n = nearestint ((log10($a) + log10(sqrt(5)))/log10(g)); return $a == term($n) ? $n : 0; } sub nearestint { my $v = shift; my $f = floor($v); my $c = ceil($v); ($v-$f) < ($c-$v) ? $f : $c; } # routines to implement term and series with the familiar additive algorithm. sub a_term { return $_[0] < 3 ? 1 : a_term($_[0]-1) + a_term ($_[0]-2) } sub a_series { my @series = map(a_term($_), 1..shift); \@series; } 1; =head1 NAME Math::Fibonacci - Fibonacci numbers. =head1 VERSION $Revision: 1.5 $ =head1 SYNOPSIS use Math::Fibonacci qw(term series decompose); my $term = term ( 42 ); my @series = series ( 42 ); my @sum = decompose ( 65535 ); =head1 DESCRIPTION This module provides a few functions related to Fibonacci numbers. =head1 EXPORTS ON REQUEST term(), series() decompose(), isfibonacci() =head1 FUNCTIONS =over 4 =item B Returns the $n-th term of the Fibonacci sequence. The term is computed using the fast algorithm: C, where g is the golden ratio and ~ means "take the nearest integer". =item B Computes and returns the first $n Fibonacci numbers. =item B Decomposes $int into the sum of Fibonacci numbers. Returns the list of Fibonacci numbers. =item B Returns the sequence number of $int if it is a Fibonacci number or a non-true value if it is not. =head1 AUTHOR Vipul Ved Prakash, Email@vipul.netE =head1 LICENSE Copyright (c) 1999-2001, Vipul Ved Prakash. This code is free software; you can redistribute it and/or modify it under the ARTISTIC license (a copy is included in the distribution) or under the same terms as Perl itself. =cut