-
Notifications
You must be signed in to change notification settings - Fork 1
/
fannkuchredux.c
100 lines (84 loc) · 2.1 KB
/
fannkuchredux.c
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
/*
* The Computer Language Benchmarks Game
* http://shootout.alioth.debian.org/
*
* converted to C by Joseph Piché
* from Java version by Oleg Mazurov and Isaac Gouy
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "bench-framework.h"
#include "nvmalloc_wrap.h"
inline static int max(int a, int b)
{
return a > b ? a : b;
}
//n = 10, pf[0] = 73196, pf[1] = 38
//n = 11, pf[0] = 556355, pf[1] = 51
int ref_checksum = 73196;
int ref_flips = 38;
int fannkuchredux(int n)
{
int *perm;
int perm1[n];
int count[n];
int maxFlipsCount = 0;
int permCount = 0;
int checksum = 0;
int i;
perm = (int *)allocate_mem(n);
for (i=0; i<n; i+=1)
perm1[i] = i;
int r = n;
while (1) {
while (r != 1) {
count[r-1] = r;
r -= 1;
}
for (i=0; i<n; i+=1)
perm[i] = perm1[i];
int flipsCount = 0;
int k;
while ( !((k = perm[0]) == 0) ) {
int k2 = (k+1) >> 1;
for (i=0; i<k2; i++) {
int temp = perm[i]; perm[i] = perm[k-i]; perm[k-i] = temp;
}
flipsCount += 1;
}
maxFlipsCount = max(maxFlipsCount, flipsCount);
checksum += permCount % 2 == 0 ? flipsCount : -flipsCount;
/* Use incremental change to generate another permutation */
while (1) {
if (r == n) {
if (n == 10 && checksum != ref_checksum) return -1;
if (n == 10 && maxFlipsCount != ref_flips) return -1;
return 0;
}
int perm0 = perm1[0];
i = 0;
while (i < r) {
int j = i + 1;
perm1[i] = perm1[j];
i = j;
}
perm1[r] = perm0;
count[r] = count[r] - 1;
if (count[r] > 0) break;
r++;
}
permCount++;
}
}
/*
int main(int argc, char *argv[])
{
int n = argc > 1 ? atoi(argv[1]) : 7;
printf("Pfannkuchen(%d) = %d\n", n, fannkuchredux(n));
return 0;
}
*/
int run_fannkuch(int n) {
return fannkuchredux(n);
}