diff options
author | Patrick Simianer <p@simianer.de> | 2014-06-15 03:24:33 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-06-15 03:24:33 +0200 |
commit | cf3a29feb5887344b6633ead1b4b6d5657a15a4b (patch) | |
tree | f1149508f7305a48dba0226699dfafdd68d81969 /algorithms/magic_coxeter.c | |
parent | 5ddc763ab9953eebdaf78af4eb72288d7955b310 (diff) |
old stuff: algorithms
Diffstat (limited to 'algorithms/magic_coxeter.c')
-rw-r--r-- | algorithms/magic_coxeter.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/algorithms/magic_coxeter.c b/algorithms/magic_coxeter.c new file mode 100644 index 0000000..17d24f6 --- /dev/null +++ b/algorithms/magic_coxeter.c @@ -0,0 +1,63 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + + +void +magic(unsigned int n) +{ + int m[n*n]; + + unsigned int i; + for (i=0; i<n*n; i++) { + m[i] = 0; + } + + int z, pos=n/2, npos; + for (z=1; z<=n*n; z++) { + m[pos] = z; + if(pos%n == 0) { + if(pos == 0) { + npos = n*n-1; + } else { + npos = pos-1; + } + } else { + if(pos-n-1 < 0) { + npos = (n*n-1) - abs(n-pos); + } else { + npos = pos-n-1; + } + } + if(m[npos] != 0) { + npos = pos+n; + } + pos = npos; + } + + unsigned int j; + for(j=0; j<n*n; j++) { + printf("\t%d\t",m[j]); + if((j+1)%n==0 && j!=0) { + printf("\n"); + } + } +} + +int +main(int argc, char **argv) +{ + if(argc!=2) { + printf("Usage: %s <n>\n", argv[0]); + exit(1); + } + int n = atoi(argv[1]); + if(n%2==0) { + printf("n has to be odd\n"); + exit(1); + } + magic(n); + + return 0; +} + |