forked from ThomasMertes/seed7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfannkuch.sd7
130 lines (114 loc) · 3.83 KB
/
fannkuch.sd7
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(********************************************************************)
(* *)
(* fannkuch.sd7 Fannkuch-redux benchmark program *)
(* Copyright (C) 2011 Bart C *)
(* *)
(* This program is free software; you can redistribute it and/or *)
(* modify it under the terms of the GNU General Public License as *)
(* published by the Free Software Foundation; either version 2 of *)
(* the License, or (at your option) any later version. *)
(* *)
(* This program is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
(* GNU General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU General Public *)
(* License along with this program; if not, write to the *)
(* Free Software Foundation, Inc., 51 Franklin Street, *)
(* Fifth Floor, Boston, MA 02110-1301, USA. *)
(* *)
(* For maximum performance compile this program with: *)
(* s7c -O2 -r fannkuch *)
(* *)
(********************************************************************)
$ include "seed7_05.s7i";
include "stdio.s7i";
const proc: fannkuch (in integer:n, inout integer:res, inout integer:res2) is func
local
var integer: signx is 1;
var integer: maxflips is 0;
var integer: flips is 0;
var integer: sum is 0;
var integer: i is 0;
var integer: j is 0;
var integer: t is 0;
var integer: q1 is 0;
var integer: qq is 0;
var integer: sx is 0;
var boolean: finished is FALSE;
var array integer: p is 0 times 0;
var array integer: q is 0 times 0;
var array integer: s is 0 times 0;
begin
p := n times 0;
q := n times 0;
s := n times 0;
for i range 1 to n do
p[i]:=i;
q[i]:=i;
s[i]:=i;
end for;
repeat
q1:=p[1];
if q1<>1 then
for i range 2 to n do q[i]:=p[i] end for;
flips:=1;
qq:=q[q1];
while qq<>1 do
q[q1]:=q1;
if q1>=4 then
i:=2; j:=pred(q1);
repeat
t:=q[i]; q[i]:=q[j]; q[j]:=t;
incr(i);
decr(j);
until i>=j
end if;
q1:=qq;
incr(flips);
qq:=q[q1];
end while;
sum+:=signx*flips;
if flips>maxflips then maxflips:=flips end if;
end if;
if signx=1 then
t:=p[1]; p[1]:=p[2]; p[2]:=t;
signx:=(-1);
else
t:=p[2]; p[2]:=p[3]; p[3]:=t;
signx:=1;
for i range 3 to n do
sx:=s[i];
if sx<>1 then
s[i]:=pred(sx);
i:=succ(n);
else
if i=n then
res:=sum;
res2:=maxflips;
finished:=TRUE;
else
s[i]:=i;
t:=p[1];
for j range 1 to i do
p[j]:=p[succ(j)];
end for;
p[succ(i)]:=t;
end if;
end if;
end for;
end if;
until finished;
end func;
const proc: main is func
local
var integer: n is 0;
var integer: sum is 0;
var integer: flips is 0;
begin
n:=10;
fannkuch(n,sum,flips);
writeln(sum);
writeln("Fannkuch(" <& n <& ") =" <& flips);
end func;