This one is really trivial, but I'm not complaining.
=head1 NAME

examples/shootout/harmonic.pir - Partial sum of Harmonic series

=head1 SYNOPSIS

    % ./parrot examples/shootout/harmonic.pir 10000000

=head1 DESCRIPTION

Translated from C code by Greg Buchholz into PIR
by Peter Baylies <[EMAIL PROTECTED]>.

The C code is:
/* The Great Computer Language Shootout
   http://shootout.alioth.debian.org/
   contributed by Greg Buchholz
   Optimized by Paul Hsieh
   compile:  gcc -O2 -o harmonic harmonic.c
*/
#include<stdio.h>
#include<stdlib.h>

int main (int argc, char **argv)
{
    double i=1, sum=0;
    int n;

    for(n = atoi(argv[1]); n > 0; n--, i++)
        sum += 1/i;

    printf("%.9f\n", sum);
    return 0;
}

=cut

.sub 'main' :main
	.param pmc argv
	.local int argc
	.local int n
	.local num i, sum
	i = 1
	sum = 0
	argc = argv
	if argc <= 1 goto NREDO
	$S0 = argv[1]
	n = $S0
NREDO:	$N1 = 1 / i
	sum += $N1
	inc i
	dec n
	if n goto NREDO

PRINT:	$P0 = new .FixedFloatArray
	$P0 = 1
	$P0[0] = sum
	$S0 = sprintf "%.9f\n", $P0
        print $S0
	end
.end

Reply via email to