I was stumbling through some online message boards and I came across three problems that piqued my curiosity. Each problem asks you to write code which will generate the following output:

No 1.

1 25

2 8 20 26

3 9 13 17 21 27

4 10 14 16 18 22 28

5 11 15 19 23 29

6 12 24 30

7 31

*edit* I didn't know the board automatically deleted multiple spaces. The above is supposed to look like a ribbon or butterfly.

No 2.

1 3 6 10 15

2 5 9 14 19

4 8 13 18 22

7 12 17 21 24

11 16 20 23 25

No. 3

1 3 5 7 9 11 13

47 49 51 53 55 57 15

45 79 81 83 85 59 17

43 77 95 97 87 61 19

41 75 93 91 89 63 21

39 73 71 69 67 65 23

37 35 33 31 29 27 25

It took me five hours to do it (and I made some improvements) but I eventually did complete it.

Unfortunately, the message board did not indicate whether there was a terse, near-genius solution to these problems. My code nears some 160 lines of code but I know there is a solution out there written by some really smart guy that can do it in about 10 (maybe I exaggerate). If anybody knows where it is, or has the relevant bit of code, please feel free to give me a hand.

Anyway, here's my code:

Code:#include <stdio.h> void butterfly(int, int); void diagonal(int, int); void twine(int, int); int main() { int i, j, k; while(1) { restart: printf("Select figure to draw: (1. butterfly 2. diagonal stripes 3. ball 4. exit) "); scanf("%d", &i); if(i == 4) break; else if(i > 4) goto restart; printf("Define dimensions:"); scanf("%d", &j); printf("Define common difference: "); scanf("%d", &k); switch(i) { case 1: butterfly(j, k); break; case 2: diagonal(j, k); break; case 3: twine(j, k); break; default: break; } } return 0; } void butterfly(int nD, int nStep) { int** nArray; int i, j, k = 1, nHalf; nHalf = nD >> 1; nArray = new int* [nD]; for(i = 0; i < nD; i++) nArray[i] = new int [nD]; for(i = 0; i < nD; i++) for(j = 0; j < nD; j++) nArray[i][j] = 0; for(i = 0; i < nD; i++) { if(i < nHalf) for(j = i; j < nD - i; j++, k+=nStep) nArray[i][j] = k; else for(j = nD - i - 1; j < i + 1; j++, k+=nStep) nArray[i][j] = k; } for(i = 0; i < nD; i++) { for(j = 0; j < nD; j++) { if(nArray[j][i] != 0) printf("%3d", nArray[j][i]); else printf(" "); } printf("\n"); } for(i = 0; i < nD; i++) delete [] nArray[i]; delete [] nArray; } void diagonal(int nD, int nStep) { int** nArray; int i, j, k = 1, l = 0; nArray = new int* [nD]; for(i = 0; i < nD; i++) nArray[i] = new int [nD]; for(i = 0; i < nD; i++) for(j = 0; j < nD; j++) nArray[i][j] = 0; for(i = 0; i < nD * 2 - 1; i++) { if(i < nD) for(j = 0; j <= i; j++, k+=nStep) nArray[i-j][j] = k; else { l++; for(j = l;j < nD; j++, k+=nStep) nArray[i-j][j] = k; } } for(i = 0; i < nD; i++) { for(j = 0; j < nD; j++) printf("%3d", nArray[i][j]); printf("\n"); } for(i = 0; i < nD; i++) delete [] nArray[i]; delete [] nArray; } void twine(int nD, int nStep) { int** nArray; int i, j, k = 1, row = 0, column = 0, nt_D = nD, c_init = 0, r_init = 0; bool horizontal = true, vertical = true; nArray = new int* [nD]; for(i = 0; i < nD; i++) nArray[i] = new int [nD]; while(k <= nD * nD * nStep) { if(horizontal && vertical) { for(i = column; i < nt_D; i++, k+=nStep) { nArray[row][i] = k; } c_init = column - 1; vertical = !vertical; row++; column = i - 1; //nt_D--; } else if(horizontal && !vertical) { for(i = row; i < nt_D; i++, k+=nStep) { nArray[i][column] = k; } r_init = row - 1; horizontal = !horizontal; column--; row = i - 1; } else if(!horizontal && !vertical) { for(i = column; i > c_init; i--, k+=nStep) nArray[row][i] = k; vertical = !vertical; row--; column = i + 1; nt_D--; } else if(!horizontal && vertical) { for(i = row; i > r_init; i--, k+=nStep) nArray[i][column] = k; horizontal = !horizontal; row = i + 1; column++; } } for(i = 0; i < nD; i++) { for(j = 0; j < nD; j++) printf("%3d", nArray[i][j]); printf("\n"); } for(i = 0; i < nD; i++) delete [] nArray[i]; delete [] nArray; }