Nested Loops Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For Nested Loops
nestedloop.awka
# $Id: nestedloop.gawk,v 1.2 2001/05/20 06:13:00 doug Exp $
# http://www.bagley.org/~doug/shootout/

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];

    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 += 1
    print x
}
nestedloop.bcc
/* -*- mode: c -*-
 * $Id: nestedloop.gcc,v 1.1 2000/12/30 21:42:57 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int a, b, c, d, e, f, 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++;

    printf("%d\n", x);
    return(0);
}
nestedloop.bigforth
\ $Id: nestedloop.bigforth,v 1.1 2001/06/19 16:20:46 doug Exp $
\ http://www.bagley.org/~doug/shootout/

decimal

\ read NUM from last command line argument
0. argc @ 1- arg >number 2drop drop constant NUM

: nestedloops 
  NUM 0 do
    NUM 0 do
      NUM 0 do
        NUM 0 do
          NUM 0 do
            NUM 0 do
              1+
            loop
          loop
        loop
      loop
    loop
  loop ;

\ run test and print result
0 nestedloops 1 u.r cr

bye \ th-th-that's all folks!
nestedloop.cim
% $Id: nestedloop.cim,v 1.0 2002/11/07 15:47:00 dada Exp $
external class UNIX;
begin

    integer n, a, b, c, d, e, f, x;
    text t;
    t :- Blanks(80);
    t := Arg(1);
    n := t.getInt;
    if n < 1 then n := 1;
    For a := 1 step 1 until n do
    For b := 1 step 1 until n do
    For c := 1 step 1 until n do
    For d := 1 step 1 until n do
    For e := 1 step 1 until n do
    For f := 1 step 1 until n do
    x := x + 1;
    OutInt(x,0);
    OutImage;
end
nestedloop.csharp
// $Id: nestedloop.csharp,v 1.0 2002/02/14 11:17:00 dada Exp $
// http://dada.perl.it/shootout/

using System;

class App {

    public static int Main(String[] args) {
        int n;
        int a, b, c, d, e, f, x=0;
        n = System.Convert.ToInt32(args[0]);
        if(n < 1) n = 1;

        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++;
    
        Console.WriteLine(x.ToString() + "\n");
        return(0);
    }
}
nestedloop.cygperl
#!/usr/local/bin/perl
# $Id: nestedloop.perl,v 1.2 2000/12/30 21:42:57 doug Exp $
# http://www.bagley.org/~doug/shootout/

use strict;

my $n = ($ARGV[0] > 0) ? $ARGV[0] : 1;
my $x = 0;
my $a = $n;
while ($a--) {
    my $b = $n;
    while ($b--) {
    my $c = $n;
    while ($c--) {
        my $d = $n;
        while ($d--) {
        my $e = $n;
        while ($e--) {
            my $f = $n;
            while ($f--) {
            $x++;
            }
        }
        }
    }
    }
}
print "$x\n";
nestedloop.delphi
program nestedloop;


var NUM, a, b, c, d, e, f, x : Cardinal;
    code: integer;
begin
  NUM :=1;
  if (ParamCount=1) then Val(ParamStr(1),NUM,code);

  x := 0;
  for a:=1 to NUM do
    for b:=1 to NUM do
      for c:=1 to NUM do
        for d:=1 to NUM do
          for e:=1 to NUM do
            for f:=1 to NUM do
              inc(x);
  WriteLn(x);
end.

nestedloop.elastic
// $Id: nestedloop.elastic,v 1.0 2002/04/30 10:56:00 dada Exp $
package nestedloop;

import basic;
import sys;
import array;

private n = 1;
if(array.length(sys.args) > 0) {
    n = basic.int(sys.args[0]);
} else {
    n = 1;
}
private a, b, c, d, e, f, 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++;
basic.print(x);

nestedloop.erlang
%%% -*- mode: erlang -*-
%%% $Id: nestedloop.erlang,v 1.1 2001/05/15 07:00:27 doug Exp $
%%% http://www.bagley.org/~doug/shootout/

-module(nestedloop).
-export([main/1]).

main([Arg]) ->
    Num = list_to_integer(atom_to_list(Arg)),
    io:fwrite("~w\n", [loopA(Num, Num, 0)]),
    halt(0).


loopA(0, M, N) -> N;
loopA(I, M, N) -> loopA(I - 1, M, loopB(M, M, N)).

loopB(0, M, N) -> N;
loopB(I, M, N) -> loopB(I - 1, M, loopC(M, M, N)).

loopC(0, M, N) -> N;
loopC(I, M, N) -> loopC(I - 1, M, loopD(M, M, N)).

loopD(0, M, N) -> N;
loopD(I, M, N) -> loopD(I - 1, M, loopE(M, M, N)).

loopE(0, M, N) -> N;
loopE(I, M, N) -> loopE(I - 1, M, loopF(M, N)).

loopF(0, N) -> N;
loopF(I, N) -> loopF(I - 1, 1 + N).
nestedloop.fpascal
program nestedloop;
uses SysUtils;

var n, a, b, c, d, e, f : integer;
var x : longint;

begin
    if ParamCount = 0 then
        n := 1
    else
        n := StrToInt(ParamStr(1));
    if n < 1 then n := 1;
    x := 0;
    For a := 1 to n Do
    For b := 1 to n Do
    For c := 1 to n Do
    For d := 1 to n Do
    For e := 1 to n Do
    For f := 1 to n Do
    Inc(x);
    WriteLn( IntToStr(x) );
end.
nestedloop.gawk
# $Id: nestedloop.gawk,v 1.2 2001/05/20 06:13:00 doug Exp $
# http://www.bagley.org/~doug/shootout/

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];

    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 += 1
    print x
}
nestedloop.gcc
/* -*- mode: c -*-
 * $Id: nestedloop.gcc,v 1.1 2000/12/30 21:42:57 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int a, b, c, d, e, f, 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++;

    printf("%d\n", x);
    return(0);
}
nestedloop.gforth
\ $Id: nestedloop.gforth,v 1.7 2001/05/25 16:45:53 doug Exp $
\ http://www.bagley.org/~doug/shootout/

decimal

\ read NUM from last command line argument
0. argc @ 1- arg >number 2drop drop constant NUM

: nestedloops 
  NUM 0 do
    NUM 0 do
      NUM 0 do
        NUM 0 do
          NUM 0 do
            NUM 0 do
              1+
            loop
          loop
        loop
      loop
    loop
  loop ;

\ run test and print result
0 nestedloops 1 u.r cr

bye \ th-th-that's all folks!
nestedloop.ghc
-- $Id: nestedloop.ghc,v 1.1 2001/02/22 23:22:11 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Brian Gregor

module Main where

import System(getArgs, exitWith, ExitCode(..))
import Numeric(readDec)

main = do
        arg <- getArgs
        case arg of
         [number] -> let n = fst (head (readDec number)) in
                     putStrLn (show (loopA n n 0))
         _      -> exitWith (ExitFailure 1)

loopA :: Int -> Int -> Int -> Int
loopA n m x 
   | n > 0         = loopA (n-1) m (loopB m m x)
   | otherwise     = x

loopB :: Int -> Int -> Int -> Int
loopB n m x
   | n > 0         = loopB (n-1) m (loopC m m x)
   | otherwise     = x

loopC :: Int -> Int -> Int -> Int
loopC n m x
   | n > 0         = loopC (n-1) m (loopD m m x)
   | otherwise     = x

loopD :: Int -> Int -> Int -> Int
loopD n m x
   | n > 0         = loopD (n-1) m (loopE m m x)
   | otherwise     = x

loopE :: Int -> Int -> Int -> Int
loopE n m x
   | n > 0         = loopE (n-1) m (loopF m x)
   | otherwise     = x

loopF :: Int -> Int -> Int 
loopF n x
   | n > 0         = loopF (n-1) (x+1)
   | otherwise     = x
nestedloop.gnat
-- $Id: nestedloop.gnat,v 1.0 2003/06/11 12:04:00 dada Exp $
-- http://dada.perl.it/shootout/
-- Ada 95 code by C.C.

with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Command_Line;

procedure NestedLoop is
   Count, M : Natural := 0;
   N        : Positive := 1;
begin
   begin
      N := Positive'Value (Ada.Command_Line.Argument (1));
   exception
      when Constraint_Error => null;
   end;
   M := N - 1;
   for A in 0 .. M loop
      for B in 0 .. M loop
         for C in 0 .. M loop
            for D in 0 .. M loop
               for E in 0 .. M loop
                  for F in 0 .. M loop
                     Count := Count + 1;
                  end loop;
               end loop;
            end loop;
         end loop;
      end loop;
   end loop;
   Ada.Integer_Text_IO.Put (Item => Count, Width => 0);
   Ada.Text_IO.New_Line;
end NestedLoop;

nestedloop.guile
#!/usr/local/bin/guile \
-e main -s
!#

;;; $Id: nestedloop.guile,v 1.2 2001/06/29 23:12:37 doug Exp $
;;; http://www.bagley.org/~doug/shootout/

(define (main args)
  (let* ((n (or (and (= (length args) 2) (string->;number (cadr args))) 1))
     (x 0))
    (do ((a 0 (+ a 1)))
    ((= a n))
      (do ((b 0 (+ b 1)))
      ((= b n))
    (do ((c 0 (+ c 1)))
        ((= c n))
      (do ((d 0 (+ d 1)))
          ((= d n))
        (do ((e 0 (+ e 1)))
        ((= e n))
          (do ((f 0 (+ f 1)))
          ((= f n))
        (set! x (+ x 1))))))))
    (display x) (newline)))
nestedloop.ici
// $Id: nestedloop.ici,v 1.0 2003/01/03 11:26:00 dada Exp $
// http://dada.perl.it/shootout
//
// contributed by Tim Long

n := argv[1] ? int(argv[1]) : 1;
x := 0;

z := array();
for (i = 0; i < n; ++i)
    z[i] = i;
forall (a in z)
    forall (b in z)
        forall (c in z)
            forall (d in z)
                forall (e in z)
                    forall (f in z)
                        ++x;

/*
 * These simple nested for loops are perhaps a more natural construct.
 * But other languages use the above contruct in their implementations,
 * and it is slightly faster.
 */
/*
for (a = n; a--; )
    for (b = n; b--; )
        for (c = n; c--; )
            for (d = n; d--; )
                for (e = n; e--; )
                    for (f = n; f--; )
                        ++x;
*/

printf("%d\n", x);
nestedloop.icon
# -*- mode: icon -*-
# $Id: nestedloop.icon,v 1.1 2000/12/30 21:42:57 doug Exp $
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
    local n, a, b, c, d, e, f, x
    n := argv[1] | 1;
    x := 0
    every a := 1 to n do
    every b := 1 to n do
        every c := 1 to n do
        every d := 1 to n do
            every e := 1 to n do
            every f := 1 to n do
                x +:= 1
    write(x)
end
nestedloop.java
// $Id: nestedloop.java,v 1.1 2000/12/30 21:42:57 doug Exp $
// http://www.bagley.org/~doug/shootout/

import java.io.*;
import java.util.*;

public class nestedloop {
    public static void main(String args[]) throws IOException {
    int n = Integer.parseInt(args[0]);
    int x = 0;
    for (int a=0; a<n; a++)
        for (int b=0; b<n; b++)
        for (int c=0; c<n; c++)
            for (int d=0; d<n; d++)
            for (int e=0; e<n; e++)
                for (int f=0; f<n; f++)
                x++;
    System.out.println(x);
    }
}
nestedloop.jscript
// -*- mode: java -*-
// $Id: nestedloop.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// From David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

var n, x=0;

ARGS = WScript.Arguments;

if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {
  n = 1;
}
var a=n;
// Using while() is faster than for()
while(a--) {
  var b=n; while(b--) {
    var c=n; while(c--) {
      var d=n; while(d--) {
    var e=n; while(e--) {
      var f=n; while(f--) {
        x++;
      }
    }
      }
    }
  }
}
WScript.Echo(x);
nestedloop.lcc
/* -*- mode: c -*-
 * $Id: nestedloop.gcc,v 1.1 2000/12/30 21:42:57 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int a, b, c, d, e, f, 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++;

    printf("%d\n", x);
    return(0);
}
nestedloop.lua
-- $Id: nestedloop.lua,v 1.2 2001/01/12 01:45:42 doug Exp $
-- http://www.bagley.org/~doug/shootout/

local n = tonumber((arg and arg[1]) or 1)
local x = 0
for a=1,n do
    for b=1,n do
    for c=1,n do
        for d=1,n do
        for e=1,n do
            for f=1,n do
            x = x + 1
            end
        end
        end
    end
    end
end
write(x,"\n")
nestedloop.lua5
-- $Id: nestedloop.lua,v 1.2 2001/01/12 01:45:42 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- contributed by Roberto Ierusalimschy

local n = tonumber((arg and arg[1]) or 1)
local x = 0
for a=1,n do
  for b=1,n do
    for c=1,n do
      for d=1,n do
        for e=1,n do
          for f=1,n do
            x = x + 1
          end
        end
      end
    end
  end
end
print(x)

nestedloop.mawk
# $Id: nestedloop.mawk,v 1.2 2001/05/20 06:13:00 doug Exp $
# http://www.bagley.org/~doug/shootout/

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];

    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 += 1
    print x
}
nestedloop.mercury
% ---------------------------------------------------------------------------- %
% nestedloop.m
% Ralph Becket <rbeck@microsoft.com>
% Tue Jan  9 13:36:26 GMT 2001
% vim: ts=4 sw=4 et tw=0 wm=0 ff=unix
%
% ---------------------------------------------------------------------------- %

:- module mytest.

:- interface.

:- import_module io.



:- pred main(io__state, io__state).
:- mode main(di, uo) is det.



:- implementation.

:- import_module list, int, std_util, string, require.



main -->
    io__command_line_arguments(ArgV),
    (   { ArgV = [],        N = 1 }
    ;   { ArgV = [Arg],     N = string__det_to_int(Arg) }
    ;   { ArgV = [_,_|_],   error("usage: nestedloop [N]") }
    ),
    io__write_int(nested_loop(N)),
    io__nl.



:- func nested_loop(int) = int.

nested_loop(N) =
    loop(N, loop(N, loop(N, loop(N, loop(N, loop(N, plus(1)))))), 0).



:- func loop(int, func(int) = int, int) = int.

loop(I, Fn, X) = ( if I > 0 then loop(I - 1, Fn, Fn(X)) else X ).
nestedloop.mingw32
/* -*- mode: c -*-
 * $Id: nestedloop.gcc,v 1.1 2000/12/30 21:42:57 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int a, b, c, d, e, f, 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++;

    printf("%d\n", x);
    return(0);
}
nestedloop.modula2
(* The Great Win32 Language Shootout http://dada.perl.it/shootout/

   contributed by Isaac Gouy (Modula2 novice)

   To build: xc =m nestedloop
   To run: nestedloop 16
*)

MODULE NestedLoop;
<* coverflow - *>

(* Prefer unqualified procedures *)
FROM LanguageShootout IMPORT N;

FROM STextIO IMPORT WriteLn;
FROM SWholeIO IMPORT WriteCard;


VAR
   count: CARDINAL;
   n, g, h, i, j, k, l: CARDINAL;

BEGIN
   n := N();
   count := 0;

   FOR g := 1 TO n DO
      FOR h := 1 TO n DO
         FOR i := 1 TO n DO
            FOR j := 1 TO n DO
               FOR k := 1 TO n DO
	          FOR l := 1 TO n DO
		     INC(count);
		  END;
               END;
            END;
	 END;
      END;
   END;  	
		
   WriteCard(count,1); WriteLn;
END NestedLoop.
nestedloop.modula3

MODULE Main;
IMPORT Fmt, Scan, Params, Wr, Stdio;

VAR N, a, b, c, d, e, f, x: INTEGER;
BEGIN
    IF Params.Count > 0 THEN
        N := Scan.Int(Params.Get(1));
    ELSE
        N := 1;
    END;
    x := 0;
    FOR a := 1 TO N DO
        FOR b := 1 TO N DO
            FOR c := 1 TO N DO
                FOR d := 1 TO N DO
                    FOR e := 1 TO N DO
                        FOR f := 1 TO N DO
                            INC(x);
                        END;
                    END;
                END;
            END;
        END;
    END;
    Wr.PutText (Stdio.stdout, Fmt.Int(x));
    Wr.Close (Stdio.stdout);
END Main.
nestedloop.nice
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a nestedloop.jar nestedloop

To run:
   java -jar nestedloop.jar 16
*/


import ackermann; // reuse toSingleInt


void main(String[] args){
   let n = toSingleInt(args);
   var count = 0;

   for(int a=0; a
nestedloop.ocaml
(*
 * $Id: nestedloop.ocaml,v 1.7 2001/06/27 11:37:53 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * based on code from Florian Hars and Markus Mottl
 *)

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let rec loopF x = function 0 -> x | i -> loopF (x+1) (i-1) in
  let rec loopE x = function 0 -> x | i -> loopE (loopF x n) (i-1) in
  let rec loopD x = function 0 -> x | i -> loopD (loopE x n) (i-1) in
  let rec loopC x = function 0 -> x | i -> loopC (loopD x n) (i-1) in
  let rec loopB x = function 0 -> x | i -> loopB (loopC x n) (i-1) in
  let rec loopA x = function 0 -> x | i -> loopA (loopB x n) (i-1) in
  Printf.printf "%d\n" (loopA 0 n)
nestedloop.ocamlb
(*
 * $Id: nestedloop.ocaml,v 1.7 2001/06/27 11:37:53 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * based on code from Florian Hars and Markus Mottl
 *)

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let rec loopF x = function 0 -> x | i -> loopF (x+1) (i-1) in
  let rec loopE x = function 0 -> x | i -> loopE (loopF x n) (i-1) in
  let rec loopD x = function 0 -> x | i -> loopD (loopE x n) (i-1) in
  let rec loopC x = function 0 -> x | i -> loopC (loopD x n) (i-1) in
  let rec loopB x = function 0 -> x | i -> loopB (loopC x n) (i-1) in
  let rec loopA x = function 0 -> x | i -> loopA (loopB x n) (i-1) in
  Printf.printf "%d\n" (loopA 0 n)
nestedloop.oz
%%% $Id: nestedloop.oz,v 1.1 2002/08/19 16:14:00 dada Exp $
%%% http://dada.perl.it/shootout/
%%% 
%%% Isaac Gouy suggested the use of a cell

functor
import System Application
define

    local P X A B C D E F N in
        [P] = {Application.getArgs plain}
        N = {String.toInt P}
        X = {NewCell 0}
        for A in 1..N do
            for B in 1..N do
                for C in 1..N do
                    for D in 1..N do
                        for E in 1..N do
                            for F in 1..N do
                                {Assign X {Access X}+1}
                            end
                        end
                    end
                end
            end
        end
        {System.printInfo {Access X}}
    end
    {Application.exit 0}
end
nestedloop.parrot
# $Id: nestedloop.parrot,v 1.0 2002/08/20 17:00:00 dada Exp $
# http://dada.perl.it/shootout/

.constant N I0
.constant A I1
.constant B I2
.constant C I3
.constant D I4
.constant E I5
.constant F I6
.constant X I7

    set .N, P0[1]
    set .X, 0
    set .A, .N
A_LOOP:
    set .B, .N
B_LOOP:
    set .C, .N
C_LOOP:
    set .D, .N
D_LOOP:
    set .E, .N
E_LOOP:
    set .F, .N
F_LOOP:
    inc .X
    dec .F
    if .F, F_LOOP
    dec .E
    if .E, E_LOOP
    dec .D
    if .D, D_LOOP
    dec .C
    if .C, C_LOOP
    dec .B
    if .B, B_LOOP
    dec .A
    if .A, A_LOOP

    print .X
    print "\n"
    end
nestedloop.perl
#!/usr/local/bin/perl
# $Id: nestedloop.perl,v 1.2 2000/12/30 21:42:57 doug Exp $
# http://www.bagley.org/~doug/shootout/

use strict;

my $n = ($ARGV[0] > 0) ? $ARGV[0] : 1;
my $x = 0;
my $a = $n;
while ($a--) {
    my $b = $n;
    while ($b--) {
    my $c = $n;
    while ($c--) {
        my $d = $n;
        while ($d--) {
        my $e = $n;
        while ($e--) {
            my $f = $n;
            while ($f--) {
            $x++;
            }
        }
        }
    }
    }
}
print "$x\n";
nestedloop.php
<?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";
?>
nestedloop.pike
#!/usr/local/bin/pike// -*- mode: pike -*-
// $Id: nestedloop.pike,v 1.1 2000/12/30 21:42:57 doug Exp $
// http://www.bagley.org/~doug/shootout/

void main(int argc, array(string) argv) {
  int n = (int)argv[-1];
  if (n < 1) n = 1;
  int x=0;

  for (int a; a<n; a++)
      for (int b; b<n; b++)
      for (int c; c<n; c++)
          for (int d; d<n; d++)
          for (int e; e<n; e++)
              for (int f; f<n; f++)
              x++;

  write("%d\n", x);
}
nestedloop.pliant
module "/pliant/language/context.pli"

function doit n
  arg Int n
  var Int x := 0
  
  for (var Int a) 0 n-1
    for (var Int b) 0 n-1
      for (var Int c) 0 n-1
        for (var Int d) 0 n-1
          for (var Int e) 0 n-1
            for (var Int f) 0 n-1
              x += 1
              
  console x eol

gvar Str s_n := cast ((pliant_script_args translate Address 1) map CStr) Str
if (s_n parse (gvar Int n))
  doit n
else
  console "usage: nestedloop.pli <number>" eol
nestedloop.poplisp
;;; -*- mode: lisp -*-
;;; $Id: nestedloop.poplisp,v 1.0 2002/05/03 12:10:00 dada Exp $

(let ((n (parse-integer (or (car pop11::poparglist) "1")))
(x 0))
(declare (fixnum n)
     (fixnum x)
     (optimize (speed 3) (debug 0) (safety 0)))
(dotimes (a n)
  (dotimes (b n)
    (dotimes (c n)
      (dotimes (d n)
        (dotimes (e n)
          (dotimes (f n)
            (incf x)))))))
(format t "~A~%" x))
nestedloop.python
#!/usr/local/bin/python
# $Id: nestedloop.python,v 1.3 2001/05/09 00:35:52 doug Exp $
# http://www.bagley.org/~doug/shootout/
# with help from Mark Baker

import sys

def main():
    x = 0
    iter = int(sys.argv[1])
    if iter < 1:
        iter = 1
    
    i_r = range(iter)
    for a in i_r:
        for b in i_r:
            for c in i_r:
                for d in i_r:
                    for e in i_r:
                        for f in i_r:
                            x += 1
    print x

main()
nestedloop.rexx
parse arg n
If n < 1 Then Do
    n = 1
End

x = 0

Do A = 1 To n
    Do B = 1 To n
        Do C = 1 To n
            Do D = 1 To n
                Do E = 1 To n
                    Do F = 1 To n
                        x = x + 1
                    End
                End
            End
        End
    End
End

say x
nestedloop.ruby
#!/usr/local/bin/ruby
# -*- mode: ruby -*-
# $Id: nestedloop.ruby,v 1.2 2001/02/15 01:09:35 doug Exp $
# http://www.bagley.org/~doug/shootout/
# from Avi Bryant

n = Integer(ARGV.shift || 1)
x = 0
n.times do
    n.times do
    n.times do
        n.times do
        n.times do
            n.times do
            x += 1
            end
        end
        end
    end
    end
end
puts x
nestedloop.se
-- -*- mode: eiffel -*-
-- $Id: nestedloop.se,v 1.1 2000/12/30 21:42:57 doug Exp $
-- http://www.bagley.org/~doug/shootout/

class NESTEDLOOP

creation make

feature

   make is

      local
     n,a,b,c,d,e,f,x: INTEGER;
      do
     if argument_count = 1 then
        n := argument(1).to_integer
     else
        n := 1
     end

     x := 0
     from
        a := 0
     until
        a = n
     loop
        from
           b := 0
        until
           b = n
        loop
           from
          c := 0
           until
          c = n
           loop
          from
             d := 0
          until
             d = n
          loop
             from
            e := 0
             until
            e = n
             loop
            from
               f := 0
            until
               f = n
            loop
               x := x + 1
               f := f + 1
            end
            e := e + 1
             end
             d := d + 1
          end
          c := c + 1
           end
           b := b + 1
        end
        a := a + 1
     end

         std_output.put_integer(x)
         std_output.put_character('%N')
      end;

end
nestedloop.slang
% $Id: nestedloop.slang,v 1.0 2002/11/29 10:43:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis

define main()
{
   variable x = 0;
   variable iter = integer (__argv[1]);
   
   loop (iter)
     loop (iter)
       loop (iter)
     loop (iter)
       loop (iter)
         loop (iter)
           x += 1;
   
   vmessage ("%d", x);
}
main();
nestedloop.smlnj
(* -*- mode: sml -*-
 * $Id: nestedloop.smlnj,v 1.3 2001/07/09 00:25:28 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *)

structure Test : sig
    val main : (string * string list) -> OS.Process.status
end = struct

fun loopF 0 x = x
  | loopF n x = loopF (n-1) (x+1)

fun loopE 0 m x = x
  | loopE n m x = loopE (n-1) m (loopF m x);

fun loopD 0 m x = x
  | loopD n m x = loopD (n-1) m (loopE m m x);

fun loopC 0 m x = x
  | loopC n m x = loopC (n-1) m (loopD m m x);

fun loopB 0 m x = x
  | loopB n m x = loopB (n-1) m (loopC m m x);

fun loopA 0 m x = x
  | loopA n m x = loopA (n-1) m (loopB m m x);


fun atoi s = case Int.fromString s of SOME num => num | NONE => 0;

fun main(name, args) = 
  let
    val arg = hd(args @ ["1"]);
    val num = atoi arg;
    val result = loopA num num 0
  in
      print (Int.toString result); print "\n";
      OS.Process.success
  end
end

val _ = SMLofNJ.exportFn("nestedloop", Test.main);
nestedloop.tcl
#!/usr/local/bin/tclsh
# $Id: nestedloop.tcl,v 1.2 2001/02/04 15:00:04 doug Exp $
# http://www.bagley.org/~doug/shootout/
# from Tom Wilkason

proc main {} {
    global argv
    set n [lindex $argv 0]
    set x 0
    incr n 1
    set a $n
    while {[incr a -1]} {
    set b $n
    while {[incr b -1]} {
        set c $n
        while {[incr c -1]} {
        set d $n
        while {[incr d -1]} {
            set e $n
            while {[incr e -1]} {
            set f $n
            while {[incr f -1]} {
                incr x
            }
            }
        }
        }
    }
    }
    puts $x
}

main
nestedloop.vbscript
n = WScript.Arguments(0)
If n < 1 Then n = 1
x = 0
a = n
While a
    b = n
    While b
        c = n
        While c
            d = n
            While d
                e = n
                While e
                    f = n
                    while f
                        x = x + 1
                        f = f - 1
                    Wend
                    e = e - 1
                Wend
                d = d - 1
            Wend
            c = c - 1
        Wend
        b = b - 1
    Wend
    a = a -1
Wend

WScript.Echo x
nestedloop.vc
/* -*- mode: c -*-
 * $Id: nestedloop.gcc,v 1.1 2000/12/30 21:42:57 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int a, b, c, d, e, f, 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++;

    printf("%d\n", x);
    return(0);
}
nestedloop.vc++
// -*- mode: c++ -*-
// $Id: nestedloop.g++,v 1.2 2001/06/20 03:20:02 doug Exp $
// http://www.bagley.org/~doug/shootout/

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int a, b, c, d, e, f, 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++;

    cout << x << endl;
    return(0);
}
nestedloop.vpascal
program nestedloop;
uses SysUtils;

var n, a, b, c, d, e, f, x : integer;

begin
    if ParamCount = 0 then
        n := 1
    else
        n := StrToInt(ParamStr(1));
    if n < 1 then n := 1;
    x := 0;
    For a := 1 to n Do
    For b := 1 to n Do
    For c := 1 to n Do
    For d := 1 to n Do
    For e := 1 to n Do
    For f := 1 to n Do
    Inc(x);
    WriteLn( IntToStr(x) );
end.