#include<stdio.h> #include<math.h> #include<string.h> int a[21],visited[21],N; int is_prime(int k) { int i; for(i=2;i*i<=k;i++) if(k%i==0) return 0; return 1; } void print(int a[]) { int i; for(i=1;i<=N-1;i++) printf("%d ",a[i]); printf("%d\n",a[N]); } void search(int m) { int i; if(m==N+1) { if(is_prime(a[1]+a[N])) print(a); else return ; } for(i=2;i<=N;i++) { if(visited[i]==0) { a[m]=i; visited[i]=1; if(is_prime(a[m]+a[m-1])) search(m+1); visited[i]=0; } } } int main() { int k=1; a[1]=1; while(scanf("%d",&N),N) { memset(visited,0,sizeof(visited)); printf("Case %d:\n",k++); if(N==1) printf("1\n"); else if(N>1&&N%2==1) printf("No Answer\n"); else search(2); } return 0; } #include<stdio.h> #include<string.h> int a[20]; bool isp[40]={1,1,0,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1}; bool visit[20]; int m; void dfs(int n) { int i; if(n==m&&!(isp[a[m-1]+1])) { for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n"); } else { for(i=2;i<=m;i++) { if(!visit[i]&&!isp[i+a[n-1]]) { a[n]=i; visit[i]=1; dfs(n+1); visit[i]=0; } } } } int main() { int count=1; while(~scanf("%d",&m)&&m) { printf("Case %d:\n",count++); if(m==1) { printf("1\n"); continue; } if(m%2) { printf("No Answer\n"); continue; } a[0]=1; dfs(1); } }