TobyInkster.co.uk

17/07/2007: PHP vs Perl

Here’s a simple program which calculates, to eight decimal places, the value of the Golden Ratio φ implemented in both PHP and Perl, to demonstrate their similarities.

Perl

#!/usr/bin/perl
 
$a = 1;
$b = 1;
$c = undef;
$psi_old = undef;
 
print "Approximating psi...\n";
while (1)
{
    $psi = sprintf('%.08f', $b/$a);
    last if ($psi_old eq $psi);
    $psi_old = $psi;
    print "$psi\n";
 
    $c = $a + $b;
    $a = $b;
    $b = $c;
}

PHP

<?php

$a 1;
$b 1;
$c NULL;
$psi_old NULL;

print "Approximating psi...\n";
while (1)
{
    $psi sprintf('%.08f'$b/$a);
    if ($psi_old == $psi) break;
    $psi_old $psi;
    print "$psi\n";

    $c $a $b;
    $a $b;
    $b $c;
}

?>

Google Search

Article Details

Author:Toby Inkster
Licence:All rights reserved
Created:Tue, 17 Jul 2007
Status:Standard
Permalink:Permalink

July 2007

M T W T F S S
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Blogroll

Here are some other sites. Some might be good; some might be rubbish. You decide.