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>

using namespace std;

bool flag[1000001];
int add[30], n, a = 0, x, m, price[30];

bool sosuu(int k) {
	if (k == 2) return true;
	else if (k < 2) return false;
	else if (k % 2 == 0) return false;
	for (int i = 3; i*i<=k; i += 2)
		if (k % i == 0) return false;
	return true;
}
void next(int k) {
	if (flag[k]) return;
	flag[k] = true;
	if (k > x) return;
	if (k > m) if (sosuu(k)) m = k;
	for (int i = 0; i < n; ++i)
		next(k + price[i]);
}

int main() {
	int pattern[30];
	while (cin >> n >> x, n || x) {
		for (int i = 0; i < 1000001; ++i) flag[i] = false;
		for (int i = 0; i < n; ++i)
			cin >> price[i];
		int p = 0, a = 0;
		m = 0;
		next(0);
		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
}

C++: #include<stdio.h>
int hideo[1000001] = { 0 };
int main(void)
{
	int  n, x, temp = 1,noyori=0,debu=0 , ruio[31] = { 0 };
	while (scanf("%d %d", &n, &x)) {
		if (n == 0 && x == 0) {
			return 0;
		}
		for (int i = 1; i <= n; i++) {
			scanf("%d", &ruio[i]);
			hideo[ruio[i]] = 1;
		}
		while (temp != x) {
			if (hideo[temp] == 1) {
				for (int j = 1; j <= n; j++) {
					if (hideo[temp + ruio[j]] == 0) {
						hideo[temp + ruio[j]] = 1;
					}
				}
			}
			temp++;
		}
		for (int i = x; i >= 1; i--) {
			if (hideo[i] == 1) {
				for (int j = 2; j < i - 1; j++) {
					if (i%j == 0) {
						debu=1;
					}
				}
				if (debu == 0) {
					noyori = i;
					break;
				}
			}
			debu = 0;
		}
		if (noyori > 1 ||noyori==x) {
			printf("%d\n", noyori);
		}
		else {
			printf("NA\n");
		}
		temp = 1;
		noyori = 0;
		debu = 0;
		for (int i = 1; i <= n; i++) {
			ruio[i] = 0;
		}
		for (int i = 1; i <= x; i++) {
			hideo[i] = 0;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool sosuu[1111111];
vector<int> v;
int a[33],x,N;
//bool dp[33][1111111];
//bool used[33][1111111];
bool solve(int n,int m){
  if(m == 0) return true;
  if(n == N) return false;
  //if(used[n][m]) return dp[n][m];
  //used[n][m] = true;

  bool res = solve(n+1,m);
  if(!res && m >= a[n]) res |= solve(n,m-a[n]);
  //return dp[n][m] = res;
  return res;
}
int main(void){
  sosuu[0] = sosuu[1] = true;
  for(int i = 0; i*i <= 1000000; i++){
    if(sosuu[i]) continue;
    for(int j = i+i; j <= 1000000; j += i){
      sosuu[j] = true;
    }
  }
  for(int i = 0; i <= 1000000; i++){
    if(!sosuu[i]){
      v.push_back(i);
    }
  }

  while(cin >> N >> x){
    if(!N)break;
    //memset(used,false,sizeof(used));
    for(int i = 0; i < N; i++){
      cin >> a[i];
    }
    int i;
    for(i = (int)v.size()-1;i >= 0; i--){
      if(x < v[i]) continue;
      if(solve(0,v[i]))break;
    }
    if(i+1){
      cout << v[i] << endl;
    }else{
      cout << "NA" << endl;
    }
  }
}

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

bool prime[1000001];
void is_prime(int n){
	for(int i = 0; i <= n; ++i)
		prime[i] = true;
	prime[0] = prime[1] = false;
	
	for(int i = 2; i <= n; ++i){
		if(prime[i]){
			for(int j = i * 2; j <= n; j += i)
				prime[j] = false;
		}
	}
}

bool DP[1000001];
int N, X;

int main() {
	is_prime(1000000);
	while(cin >> N >> X && (N || X)){
		int cook[50], ans = 0;
		memset(DP, 0, sizeof(DP));
		
		for(int i = 0; i < N; ++i){
			cin >> cook[i];
			DP[cook[i]] = true;
		}
		
		for(int i = 0; i < N; ++i){
			for(int j = 0; j <= X; ++j){
				if(DP[j] && j + cook[i] <= X)
					DP[j + cook[i]] = true;
			}
		}
			
		for(int i = 0; i <= X; ++i)
			if(DP[i] && prime[i])
				ans = i;
		if(ans == 0)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>

#define pb push_back
#define MAX 1000000

int dp[MAX];

using namespace std;

int main(void)
{
	int n,x;
	int i1,i2;

	while(1){
		cin>>n>>x;
		if(n+x==0) break;

		vector<int> v(n);
		memset(dp,0,sizeof(dp));

		for(i1=0;i1<n;i1++){
			cin>>v[i1];
			dp[v[i1]]=1;
		}

		for(i1=0;i1<=x;i1++){
			if(dp[i1]==1) {
				for(i2=0;i2<n;i2++){
					dp[i1+v[i2]]=1;
				}
			}
		}

		for(i1=x;i1>0;i1--){
			if(dp[i1]==1) {

				for(i2=2;i2*i2<=i1;i2++){
					if(i1%i2==0) break;
				}
				if(i2*i2>i1) break;
			}
		}

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


	return 0;

}

C++: #include<stdio.h>
#include <string.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 p,cnt,cnt2,food;
int n,d=0,g,kakaku[31],flag,youso[1000002]={0};
int main()
{
	scanf("%d %d",&n,&p);
	while(1){
		//memset(youso, 0, sizeof(youso));
		
		if(n==0&&p==0){
			break;
		}
		for(cnt=1;cnt<=n;cnt++){
			scanf("%d",&food);
			youso[food]=1;
			kakaku[cnt]=food;
		}
		for(cnt=0;cnt<=p;cnt++){
			if(youso[cnt]==1){
				for(cnt2=1;cnt2<=n;cnt2++){
					if((cnt+kakaku[cnt2])<=p){
						youso[cnt+kakaku[cnt2]]=1;
					}
				}
			}
		}
		flag=0;
		for(cnt=p;cnt>=0;cnt--){
			if(youso[cnt]==1){
				g=cnt;
				d=isPrime(g);
				if(d==1){
					flag=1;
					d=cnt;
					printf("%d\n",d);
					break;
				}
			}
		}
		if(flag==0){
			printf("NA\n");
		}
		d=0;
		flag=0;
		for(int i=0;i<p+1;i++){
			youso[i]=0;
		}
		scanf("%d %d",&n,&p);
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 1000000

bool prime[MAX];
bool order[MAX+1];
int menu[30];
int n,money;

void init(){
	fill(prime,prime+MAX,true);
	prime[0]=prime[1]=false;
	for(int i=2;i<MAX;i++){
		if(prime[i]){
			for(int j=2;j*i<MAX;j++)
				prime[j*i]=false;
		}
	}
}
void search(){
	queue<int> que;
	int m,l;
	que.push(0);
	while(!que.empty()){
		m=que.front();
		que.pop();
		for(int i=0;i<n;i++){
			l = m+menu[i];
			if(l<=money && !order[l]){
				order[l]=true;			
				que.push(l);
			}
		}
	}
}
int main(){
	init();
	while(cin>>n>>money,n|money){
		for(int i=0;i<n;i++)cin>>menu[i];
		fill(order,order+(MAX+1),false);
		search();
		int ans=-1;
		for(int i=money;i>1;i--){
			if(prime[i] && order[i]){
				ans=i;
				break;
			}
		}
		if(ans!=-1)cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
}