#!/usr/bin/newlisp

# fannkuch.newlisp - Fannkuch benchmark
# v. 9.0
# by Lutz Mueller 2004-12-11

(define MaxCount 0)

(define (permute left right)
    (let (j (length right))
        (if (< j 2) 
            (permCount (append left right))
            (dotimes (i j)
                (permute (append left (slice right i 1))
                         (append (slice right 0 i) (slice right (+ i 1))))))))

(define (permCount perm , myCount perm0)
        (set 'myCount 0)
        (while (!= (set 'perm0 (first perm)) 1)
            (inc 'myCount)
            (set 'perm (append (reverse (slice perm 0 perm0)) (slice perm perm0))))
        (if (> myCount MaxCount) (set 'MaxCount myCount)))

(define (fannkuch n) 
    (permute '() (sequence 1 n)) 
    MaxCount)

(define (main)
    (set 'n (integer (main-args 2)))
    (println (format "Pfannkuchen(%d) = %d" n (fannkuch n))))

(main)
(exit)


syntax highlighting with newLISP and syntax.cgi