Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		x, _ := strconv.Atoi(buf[1])
		if n == 0 && x == 0 {
			break
		}
		prime_table := make([]int, 1000001)
		prime_table[0] = -1
		prime_table[1] = -1
		for i := 2; i < 1000001; i++ {
			if prime_table[i] != -1 {
				prime_table[i] = 1
				for j := 2*i; j < 1000001; j += i {
					prime_table[j] = -1 // -1 means "not prime"
				}
			}
		}
		dp := make([]int, x+1)
		dp[0] = 1
		menu := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			menu[i], _ = strconv.Atoi(scanner.Text())
		}
		for i := 0; i < n; i++ {
			for j := 0; j <= x; j++ {
				if menu[i] <= j && dp[j-menu[i]] != 0 {
					// log.Println(dp)
					dp[j] = 1
				}
			}
		}
		flag := true
		for j := x; j >= 0; j-- {
			if prime_table[j] == 1 && dp[j] == 1 {
				flag = false
				fmt.Println(j)
				break
			}
		}
		if flag {
			fmt.Println("NA")
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
#define MAX_X 1000100
using namespace std;

int n;
int x;
int d[100];
bool p[MAX_X];
bool dp[MAX_X];

int main(){
  
  memset(p,true,sizeof(p));
  p[0] = false;
  for(int i=2;i*i<MAX_X;i++){
    if(p[i]) for(int j=i+i;j<MAX_X;j+=i) p[j] = false;
  }
  
  while(true){
    cin >> n >> x;
    if(!n && !x) break;
    
    memset(dp,false,sizeof(dp));
    dp[0] = true;
    for(int i=0;i<n;i++) cin >> d[i];
    
     for(int i=0;i<MAX_X;i++){
      if(dp[i]){
	for(int j=0;j<n;j++){
	  if(i+d[j] <= x) dp[i+d[j]] = true;
	}
      }
    }
    
    int ans = -1;
    for(int i=0;i<=x;i++){
      if(dp[i] && p[i]) ans = i;
    }

    if(ans < 0) cout << "NA" << endl;
    else cout << ans << endl;
    
  }
}

C++: #include<stdio.h>
int isPrime(int x){
	int i;
	if(x<2) return 0;
	else if(x==2) return 1;
	if(x%2==0) return 0;
	for(i=3;i*i<=x;i+=2){
		if(x%i==0) return 0;
	}
	return 1;
}
int main()
{
	long x,cnt,cnt2,food;
	int n,d=0,g,kakaku[31],youso[1000001]={0};
	scanf("%d %d",&n,&x);
	while(n!=0&&x!=0){
		for(cnt=1;cnt<=n;cnt++){
			scanf("%ld",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=1;cnt<=x;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					youso[cnt+kakaku[cnt2]]=1;
					if(cnt+kakaku[cnt2]>x){
						break;
					}
				}
			}
		}
		for(cnt=x;cnt>0;cnt--){
			if(youso[cnt]==1){
				g=cnt;
				d=isPrime(g);
				if(d==1){
					d=cnt;
					flag=1;
					printf("%d\n",d);
					break;
				}
			}
		}
		if(flag==0){
			printf("NA\n");
		}
		flag=0;
		d=0;
		for(cnt=1;cnt<=x;cnt++){
			youso[cnt]=0;
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

int dt[40];
bool prime[100000];
bool okapon[1000000];
/*
long long dfs(int i,int lm){
	long long res=0;
	if(lm < 0)return 0;
	
}
*/
int main(void){
	int n, x,lm;
	rep(i,100000){
		prime[i]=true;
	}
	for(int i=2;i<100000;i++){
		for(int j=i*2;j<100000;j+=i){
			prime[j]=false;
		}
	}

	while(cin>>n>>lm,n||lm){
		int ma = 1;
		bool find = false;
		rep(i,100000){
			okapon[i]=false;
		}
		okapon[1] = 1;
		loop(i,0,n){
			cin>>dt[i];
			rep(k,ma)
			if(okapon[k])
			for(int j = k ; j < lm ; j += dt[i]){
				okapon[k+j] = true;
			}
			ma = max(ma,dt[i]);
		}
		//rep(i,sizeof(okapon))if(okapon[i])cout<<i<<endl;
		for(int i = lm ; i >= 0 ; i --){
			if(i==2){cout<<"NA"<<endl;break;}
			if(okapon[i] && prime[i]){cout<<i<<endl;break;}
		}
	}
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int n, m;
    while(cin >> n >> m && (n || m)) {
        int cnt=0;vector<int> primes; bool num[m];
        memset(num, false, sizeof(num));
        num[0] = true;

        for(int i=0;i < n;++i) {int hoge; cin>>hoge; if(!(hoge%2)){++cnt;}}
        if (cnt == n) {puts("NA"); continue;}

        for(int i=2,len=m/2;i<=len;++i) {
            if (num[i-1]) continue;
            for(int j=i+i;j <= m;j+=i) {
                num[j-1] = true;
            }
        }

        for(int i=0;i < m;++i) {
            if (!num[i]) {
                primes.push_back(i+1);
            }
        }

        cout << primes.back() << endl;
    }

    return 0;
}

C++: #include <stdio.h>
#include <bitset>
#include <set>
using namespace std;
const int N=1000000;
int main(){
    int n,x,temp;
    bitset<N>b;
    set<int> prime;
    bool f;
    b.reset();
    prime.insert(2);
    for(long long int i=3;i<1000000;i+=2)if(!b[i]){
        prime.insert(static_cast<int>(i));
        for(long long int j=i*i;j<1000000;j+=i*2)b[j]=true;
    }
    while(true){
        scanf("%d%d",&n,&x);
        if(n==0&&x==0)return 0;
        b.reset();
        b[0]=true;
        while(n--){
            scanf("%d",&temp);
            while(temp<=x){
                b|=b<<temp;
                temp<<=1;
            }
        }
        f=true;
        for(set<int>::iterator u=prime.upper_bound(x);f&&u!=prime.begin();){
            u--;
            if(b[*u]){
                f=false;
                printf("%d\n",*u);
            }
        }
        if(f)printf("NA\n");
    }
}

C++: #include <iostream>
using namespace std;

int n, x;
int a[30];
bool b[1000001];
bool p[1000001];

bool input() {
  cin >> n >> x;
  if (!n && !x) {
    return false;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  return true;
}

void solve() {
  for (int i = 0; i <= x; ++i) {
    b[i] = false;
    p[i] = true;
  }

  b[0] = true;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= x - a[i]; ++j) {
      if (b[j]) {
        b[j + a[i]] = true;
      }
    }
  }

  p[0] = p[1] = false;
  for (int i = 2; i * i <= x; ++i) {
    if (!p[i]) {
      continue;
    }
    for (int j = i + i; j <= x; j += i) {
      p[j] = false;
    }
  }

  int ans = -1;
  for (int i = x; i >= 0; --i) {
    if (b[i] && p[i]) {
      ans = i;
      break;
    }
  }

  if (ans >= 0) {
    cout << ans << endl;
  } else {
    cout << "NA" << endl;
  }
}

int main() {
  while (input()) {
    solve();
  }
  return 0;
}

C++: #include <stdio.h>

int n,m[30];
/*
bool prime[1000000];
*/
bool c[1000000];

void saiki(int total){
	int i;
	if(total<=0){
		return;
	}
	
	for( i=0 ; i<n ; i++){
		total-=m[i];
		if(c[total]==1)continue;
		if(total>=0){
			c[total]=1;
		}
		saiki(total);
	}
}

int check(int prime){
	int flg=0,i;
	if(prime%2==0)return 0;
	for(i=3;i<prime;i+=2){
		if(prime%i==0){
			flg=1;
		}
	}
	if(flg==1)return 0;
	return 1;
}

int main(void)
{
	int i,j;
	//f
	/*
	prime[0]=1;
	prime[1]=1;
	for(i=3;i<1000000;i++){
		if(i%2==0){
			prime[i]=1;
		}
	}
	for(i=3;i<1000000;i++){
		if(prime[i]==0){
			for(j=(i+i);j<1000000;j+=i){
				prime[j]=1;
			}
		}
	}
	*/
	//
	while(1){
		for(int m;m<100;i++)
		int x;
		int flg=0;
		scanf("%d%d",&n,&x);
		if(n==0)break;
		
		for(i=0;i<n;i++){
			scanf("%d",&m[i]);
		}
		saiki(x);
		//
		for(i=0;i<x;i++){
			
			if(c[i]==1 && check(x-i)){
				flg=1;
				break;
			}
		}
		if(flg==1){
			printf("%d\n",x-i);
		}
		else puts("NA");
		for(i=0;i<1000000;i++){
			c[i]=0;
		}
		//
	}
	
	
	return 0;
}