14 April 2006

the Golden Ratio and the Fibonacci series (Perl program)

I've been re-reading Dan Brown's The Da Vinci Code lately, and I was intrigued by the Golden Ratio and how it relates to the Fibonacci series. The Fibonacci series is such that each term is the sum of the previous two terms:
1, 1, 2, 3, 5, 8, 13, ...

The ratio of consecutive terms in the Fibonacci series converges on the Golden Ratio, which has a value of approximately 1.618. It also shows up in a geometric analysis of the pentacle. These properties are discussed in mathematical detail on the following Web pages:

the Golden Ratio
pentacle geometry

I wanted to see this from a computational point of view, so I wrote a quick Perl program to compute the Fibonacci numbers and ratios out to around 100 terms. The program prints out deviations from the Golden Ratio, and the output shows a near-zero deviation after less than 40 terms.


#!/usr/bin/perl -w

use strict;
use diagnostics;

my $NUM_ITERATIONS = 100;
my $GOLDEN_RATIO = 0.5 * (1.0 + sqrt(5));

my ($previous_term, $current_term) = (1, 1);
my $iteration_number = 1;
while ( $iteration_number <= $NUM_ITERATIONS ) {
my $new_term = $previous_term + $current_term;
my $ratio = $new_term / $current_term;
my $deviation = abs($GOLDEN_RATIO-$ratio) / $GOLDEN_RATIO;
printf "% 3d: %e\n", $iteration_number, $deviation;
$previous_term = $current_term;
$current_term = $new_term;
$iteration_number++;
}

06 April 2006

seeing 'svn diff' output while editing the commit log in joe

I use subversion and I use the text editor joe (http://sourceforge.net/projects/joe-editor/). I've got the following in my ~/.bashrc file, so that joe is the editor that's launched when I type 'svn commit':


export VISUAL="/usr/bin/joe"


(export SVN_EDITOR="/usr/bin/joe" also works, if you've already got VISUAL set to something else.)

I like being able to look at the output of 'svn diff' while I edit the commit log entry, and I've found two ways of doing this (I'm still figuring out which I like better).

The first way is to redirect 'svn diff' to a file, edit the file (putting the log entry in the file), remove the 'svn diff' output from the file, and then use that file as the log entry when committing:


$ svn diff > ~/commit.txt
$ joe ~/commit.txt
(compose the log entry at the top of the file,
and delete the 'svn diff' output)
$ svn commit --file ~/commit.txt
$ rm ~/commit.txt


The second way is to pull the 'svn diff' output into the editor below the 'This line, and those below, will be ignored' line. Then I just compose the log entry at the top of the file and then quit-and-save. In fact, this can be done with a joe macro. Try adding this to the 'Macros' section of ~/.joerc:


:def svndiff eof,insf,"!svn diff",rtn,bof
svndiff ^K 4


Then, next time you want to commit, just type 'svn commit' and do a '^K 4' in joe. That'll add the 'svn diff' output at the bottom of the file and return the cursor to the top of the file.

The second approach requires fewer keystrokes. The only drawback I've noticed is that if you decide you want to abort the commit and you've already saved your edits, you need to be sure to delete the svn-commit.tmp file before exiting joe, or the commit will proceed with whatever's saved in the svn-commit.tmp file.