[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]
All Source For php
Ackermann's Function
```<?php
/*
\$Id: ackermann.php,v 1.1 2001/04/16 18:45:12 doug Exp \$
http://www.bagley.org/~doug/shootout/
from Alexander Klimov
*/
function Ack(\$m, \$n){
if(\$m == 0) return \$n+1;
if(\$n == 0) return Ack(\$m-1, 1);
return Ack(\$m - 1, Ack(\$m, (\$n - 1)));
}
\$n = (\$argc == 2) ? \$argv[1] : 1;
\$r = Ack(3,\$n);
print "Ack(3,\$n): \$r\n";
?>
```
Array Access
```<?php
/*
\$Id: ary3.php,v 1.1 2001/05/31 02:27:48 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$n = (\$argc == 2) ? \$argv[1] : 1;
for (\$i=0; \$i<\$n; \$i++) {
\$X[\$i] = \$i + 1;
}
for (\$k=0; \$k<1000; \$k++) {
for (\$i=\$n-1; \$i>=0; \$i--) {
\$Y[\$i] += \$X[\$i];
}
}
\$last = \$n-1;
print "\$Y[0] \$Y[\$last]\n";
?>
```
Count Lines/Words/Chars
```<?php
/*
\$Id: wc.php,v 1.4 2001/06/26 05:10:01 doug Exp \$
http://www.bagley.org/~doug/shootout/

TBD - this program should not assume lines are less than 10000 characters long
*/

\$fd = fopen("php://stdin", "r");
\$nl = \$nw = \$nc = 0;
while (!feof (\$fd)) {
if (\$line = fgets(\$fd, 10000)) {
++\$nl;
\$nc += strlen(\$line);
\$nw += count(preg_split("/\s+/", \$line, -1, PREG_SPLIT_NO_EMPTY));
}
}
fclose(\$fd);
print "\$nl \$nw \$nc\n";
?>
```
Fibonacci Numbers
```<?php
/*
\$Id: fibo.php,v 1.1 2001/05/06 03:07:33 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
function fibo(\$n){
return((\$n < 2) ? 1 : fibo(\$n - 2) + fibo(\$n - 1));
}
\$n = (\$argc == 2) ? \$argv[1] : 1;
\$r = fibo(\$n);
print "\$r\n";
?>
```
Hash (Associative Array) Access
```<?php
/*
\$Id: hash.php,v 1.2 2001/05/06 05:25:08 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$n = (\$argc == 2) ? \$argv[1] : 1;
for (\$i = 1; \$i <= \$n; \$i++) {
\$X[dechex(\$i)] = \$i;
}
for (\$i = \$n; \$i > 0; \$i--) {
if (\$X[\$i]) { \$c++; }
}
print "\$c\n";
?>
```
Hashes, Part II
```<?php
/*
\$Id: hash2.php,v 1.1 2001/05/06 05:29:12 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$n = (\$argc == 2) ? \$argv[1] : 1;
for (\$i = 0; \$i < 10000; \$i++) {
\$hash1["foo_\$i"] = \$i;
}
for (\$i = \$n; \$i > 0; \$i--) {
foreach(\$hash1 as \$key => \$value) \$hash2[\$key] += \$value;
}
print "\$hash1[foo_1] \$hash1[foo_9999] \$hash2[foo_1] \$hash2[foo_9999]\n";
?>
```
Heapsort
```<?php
/*
\$Id: heapsort.php,v 1.1 2001/05/08 03:25:55 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
define("IM", 139968);
define("IA", 3877);
define("IC", 29573);

\$LAST = 42;
function gen_random (\$n) {
global \$LAST;
return( (\$n * (\$LAST = (\$LAST * IA + IC) % IM)) / IM );
}

function heapsort (\$n, &\$ra) {
\$l = (\$n >> 1) + 1;
\$ir = \$n;

while (1) {
if (\$l > 1) {
\$rra = \$ra[--\$l];
} else {
\$rra = \$ra[\$ir];
\$ra[\$ir] = \$ra[1];
if (--\$ir == 1) {
\$ra[1] = \$rra;
return;
}
}
\$i = \$l;
\$j = \$l << 1;
while (\$j <= \$ir) {
if ((\$j < \$ir) && (\$ra[\$j] < \$ra[\$j+1])) {
\$j++;
}
if (\$rra < \$ra[\$j]) {
\$ra[\$i] = \$ra[\$j];
\$j += (\$i = \$j);
} else {
\$j = \$ir + 1;
}
}
\$ra[\$i] = \$rra;
}
}

\$N = (\$argc == 2) ? \$argv[1] : 1;

for (\$i=1; \$i<=\$N; \$i++) {
\$ary[\$i] = gen_random(1);
}

/*
for (\$i=0; \$i<\$N; \$i++) {
printf("%4d %.15f\n", \$i, \$ary[\$i]);
}
*/

heapsort(\$N, \$ary);

printf("%.10f\n", \$ary[\$N]);
?>
```
Hello World
```#!/usr/local/bin/php -f<?php
/*
\$Id: hello.php,v 1.1 2001/06/17 22:00:34 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
print "hello world\n";
?>
```
Matrix Multiplication
```<?php
/*
\$Id: matrix.php,v 1.1 2001/05/14 03:37:18 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/

set_time_limit(0);

\$SIZE = 30;

function mkmatrix (\$rows, \$cols) {
\$count = 1;
\$mx = array();
for (\$i=0; \$i<\$rows; \$i++) {
for (\$j=0; \$j<\$cols; \$j++) {
\$mx[\$i][\$j] = \$count++;
}
}
return(\$mx);
}

function mmult (\$rows, \$cols, \$m1, \$m2) {
\$m3 = array();
for (\$i=0; \$i<\$rows; \$i++) {
for (\$j=0; \$j<\$cols; \$j++) {
\$x = 0;
for (\$k=0; \$k<\$cols; \$k++) {
\$x += \$m1[\$i][\$k] * \$m2[\$k][\$j];
}
\$m3[\$i][\$j] = \$x;
}
}
return(\$m3);
}

\$n = (\$argc == 2) ? \$argv[1] : 1;
\$m1 = mkmatrix(\$SIZE, \$SIZE);
\$m2 = mkmatrix(\$SIZE, \$SIZE);
while (\$n--) {
\$mm = mmult(\$SIZE, \$SIZE, \$m1, \$m2);
}
print "{\$mm[0][0]} {\$mm[2][3]} {\$mm[3][2]} {\$mm[4][4]}\n";
?>
```
Nested Loops
```<?php
/*
\$Id: nestedloop.php,v 1.1 2001/05/06 06:13:21 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$n = (\$argc == 2) ? \$argv[1] : 1;
\$x = 0;
for (\$a=0; \$a<\$n; \$a++)
for (\$b=0; \$b<\$n; \$b++)
for (\$c=0; \$c<\$n; \$c++)
for (\$d=0; \$d<\$n; \$d++)
for (\$e=0; \$e<\$n; \$e++)
for (\$f=0; \$f<\$n; \$f++)
\$x++;
print "\$x\n";
?>
```
Random Number Generator
```<?php
/*
\$Id: random.php,v 1.4 2001/05/16 05:30:59 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
define("IM", 139968);
define("IA", 3877);
define("IC", 29573);

\$LAST = 42;
function gen_random (\$max) {
global \$LAST;
return( (\$max * (\$LAST = (\$LAST * IA + IC) % IM)) / IM );
}

\$result = 0;
\$N = (\$argc == 2) ? \$argv[1] : 1;
while (\$N--) {
\$result = gen_random(100);
}

printf("%.9f\n", \$result);
?>
```
Reverse a File
```#!/usr/local/bin/php -f<?php
/*
\$Id: reversefile.php,v 1.1 2001/05/08 04:18:33 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$fd = fopen("php://stdin", "r");
\$lines = array();
while (!feof (\$fd)) { array_push(\$lines, fgets(\$fd, 4096)); }
fclose(\$fd);
foreach (array_reverse(\$lines) as \$line) print \$line;
?>
```
Sieve of Erathostenes
```<?php
/*
\$Id: sieve.php,v 1.1 2001/05/06 04:37:37 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$n = (\$argc == 2) ? \$argv[1] : 1;
\$count = 0;
while (\$n-- > 0) {
\$count = 0;
\$flags = range (0,8192);
for (\$i=2; \$i<8193; \$i++) {
if (\$flags[\$i] > 0) {
for (\$k=\$i+\$i; \$k <= 8192; \$k+=\$i) {
\$flags[\$k] = 0;
}
\$count++;
}
}
}
print "Count: \$count\n";
?>
```
Spell Checker
```#!/usr/local/bin/php -f<?php
/*
\$Id: spellcheck.php,v 1.1 2001/05/16 01:43:37 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$dict = array();
\$fd = fopen("Usr.Dict.Words", "r");
while (!feof (\$fd)) { \$dict[chop(fgets(\$fd, 1024))] = 1; }
fclose(\$fd);

\$fd = fopen("php://stdin", "r");
while (!feof (\$fd)) {
\$word = chop(fgets(\$fd, 1024));
if (! \$dict[\$word]) {
print "\$word\n";
}
}
fclose(\$fd);
?>
```
String Concatenation
```#!/usr/local/bin/php -f<?php
/*
\$Id: strcat.php,v 1.1 2001/05/06 06:00:46 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$n = (\$argc == 2) ? \$argv[1] : 1;
\$str = "";
while (\$n-- > 0) {
\$str .= "hello\n";
}
\$len = strlen(\$str);
print "\$len\n";
?>
```
Sum a Column of Integers
```#!/usr/local/bin/php -f<?php
/*
\$Id: sumcol.php,v 1.1 2001/05/13 04:19:17 doug Exp \$
http://www.bagley.org/~doug/shootout/
*/
\$fd = fopen("php://stdin", "r");
\$sum = 0;
while (!feof (\$fd)) { \$sum += fgets(\$fd, 1024); }
fclose(\$fd);
print "\$sum\n";
?>
```