forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfannkuchredux2.fs
62 lines (59 loc) · 1.65 KB
/
fannkuchredux2.fs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/// The Computer Language Benchmarks Game
/// http://shootout.alioth.debian.org/
///
/// from Scala version by Otto Bommer, August 2010
/// Modified by Faisal Waris by removing ref's and using mutable variables, April 25, 2011
module Fannkuchredux
let fannkuch n =
(let perm1 = Array.create n 0
for i = 0 to (n - 1) do
perm1.[i] <- i
let perm = Array.create n 0
let count = Array.create n 0
let mutable flips = 0
let mutable maxflips = 0
let mutable checksum = 0
let mutable nperm = 0
let mutable r = n
while r > 0 do
for i = 0 to n - 1 do
perm.[i] <- perm1.[i]
while r <> 1 do
count.[r - 1] <- r
r <- r - 1
flips <- 0
let mutable k = perm.[0]
while k <> 0 do
let mutable t = 0
for i = 0 to k / 2 do
t <- perm.[i]
perm.[i] <- perm.[k - i]
perm.[k - i] <- t
k <- perm.[0]
flips <- flips + 1
maxflips <- max maxflips flips
if nperm &&& 1 = 0 then checksum <- checksum + flips
else checksum <- checksum - flips
let mutable go = true
let mutable t = 0
while go do
if r = n then
(go <- false
r <- 0)
else
(t <- perm1.[0]
for i = 0 to r - 1 do
perm1.[i] <- perm1.[i + 1]
perm1.[r] <- t
count.[r] <- count.[r] - 1
if count.[r] > 0 then go <- false
else r <- r + 1)
nperm <- nperm + 1
(maxflips, checksum))
let _ =
let n =
try
int ((System.Environment.GetCommandLineArgs()).[1])
with _ -> 7
let (maxflips, checksum) = fannkuch n
Printf.printf "%d\nPfannkuchen(%d) = %d\n" checksum n maxflips