In response to this challenge McNugget numbers  I wrote C code based on a different approach than exhibited in the LISP solution and obviously the Haskell solution. I tried to make it shorter, but instead only succeeded to obfuscate the code – normally my source files are much more readable.

Please leave a comment if you need my explanation how it works, or a more readable Python equivalent.

#include <stdio.h>
#define M(x) ((x) % 20)
int main()
{
  char a[20]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  int k=0,c=0;
  while(++k){
    int i=M(k);
    if(!(a[M(i+14)]||a[M(i+11)]||a[i])){a[i]=c=0;printf("%d\n",k);continue;}
    if((a[i]=1)&&++c==6)break;
  }
  return 0;
}
About these ads