// Prints out all unique magic hexagons of order 3,
// ie  hexagon made of hexagons, 3 to a side
// there is in fact only one such hexagon.
// The program excludes rotations and reflections,
// and executes quickly.
//
// with acknowledgement to xptaylor@hotmail.com & crux@qnet.com
// whose programs provided the foundation for this
//
// March 04

char used[20]; 
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s;
int rowsum;

void printout()
{
  printf( "\n"
    "    %3i %3i %3i\n"
    "  %3i %3i %3i %3i\n"
    "%3i %3i %3i %3i %3i\n"
    "  %3i %3i %3i %3i\n"
    "    %3i %3i %3i\n",
          
         a,  l,  k,
       b,  m,  r,  j,
     c,  n,  s,  q,  i, 
       d,  o,  p,  h,
         e,  f, g);
}


main()
{

rowsum = 38;			// the magic constant, has to be 38

for (a=1; a<=14; a++) {	// a is lowest corner; must be 5 larger corners	
 used[a]++;			// mark this number as used
 for (b=1; b<=19; b++)
  if (!used[b]) {		// 'for' loop consists of this single 'if' statement; no {} needed
   used[b]++;
   c = rowsum-a-b;
   if (c>a && c<=19 && !used[c]) {
    used[c]++;
    for (n=1; n<=19; n++)
     if (!used[n]) {
      used[n]++;
      for (o=1; o<=19; o++)
       if (!used[o]) {
        used[o]++;
         f = rowsum-b-n-o;
         if (f>0 && f<=19 && !used[f]) {
          used[f]++;
          for (m=1; m<=19; m++)
           if (!used[m]) {
            used[m]++;
            for (s=1; s<=19; s++)
             if (!used[s]) {
              used[s]++;
              for (p=1; p<=19; p++)
               if (!used[p]) {
                used[p]++;
                g = rowsum-a-m-s-p;
                if (g>a && g<=19 && !used[g]) {
                 used[g]++;
                 e = rowsum-g-f;
                 if (e>a && e<=19 && !used[e]) {
                  used[e]++;
                  d = rowsum-c-e;
                  if (d>0 && d<=19 && !used[d]) {
                   used[d]++;
                   h = rowsum-d-o-p;
                   if (h>0 && h<=19 && !used[h]) {
                    used[h]++;
                    i = rowsum-h-g;
                    if (i>a && i<=19 && !used[i]) {
                     used[i]++;
                     q = rowsum-i-s-n-c;
                     if (q>0 && q<=19 && !used[q]) {
                      used[q]++;
                      j = rowsum-q-p-f;
                      if (j>0 && j<=19 && !used[j]) {
                       used[j]++;
                       k = rowsum-j-i;
                       if (k>a && k<=19 && !used[k]) {
                        used[k]++;
                        l = rowsum-a-k;
                        if (l>b && l<=19 && !used[l]) { // l>b avoids reflections
                         used[l]++;
                         r = rowsum-k-s-o-e;
                         if (r>0 && r<=19 && rowsum==l+m+n+d && rowsum == l+r+q+h && rowsum == b+m+r+j && !used[r])
                          printout();
                         used[l]--;
                        }
                        used[k]--;
                       }
                       used[j]--;
                      }
                      used[q]--;
                     }
                     used[i]--;
                    }
                    used[h]--;
                   }
                   used[d]--;
                  }
                  used[e]--;
                 }
                 used[g]--;
                }
                used[p]--;
               }
              used[s]--;
             }
            used[m]--;
           }
          used[f]--;
         }
        used[o]--;
       }
      used[n]--;
     }
    used[c]--;
   }
   used[b]--;
  }	// if !used[b]
 used[a]--;
}	// for a

}	// main()
