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<cstring>
using namespace std;

bool dp[1000010];
int n,x;
int cook[33];
int solve();
bool prime(int now);

int main(){
	while(1){
		cin >> n >> x;
		if(n == 0 && x == 0) break;
		memset(dp,false,x+2);
		dp[0] = true;
		for(int i=0;i<n;i++){
			cin >> cook[i];
			for(int k=cook[i];k<=x;k++){
				if(dp[k-cook[i]]){
					dp[k] = true;
				}
			}
		}
		int ans = solve();
		if(ans) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

int solve (){
	int now = x;
	while(1){
		if(dp[now] && !(prime(now))) return now;
		now--;
	}
}

bool prime(int now) {
	for(int i=2;i*i<=now;i++){
		if(now%i == 0) return true;
	}
	return false;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
bool dp[1000001]={true};
bool prime(int& x){
  for(int i = 2 ; i * i <= x ; i++ ) if( x % i == 0) return true;
  return x == 1;
}
int solve(int &i){
  while(i--) if(dp[i] && !prime(i)) return i;
}
int main(){
  int n,x,data[30],ans;
  while(cin >> n >> x , n){
    for(int i = 1 ; i <= x ; i++ ) dp[i] = false;
    for(int i = 0 ; i < n ; i++ ) cin >> data[i];
    for(int i = 0 ; i <= x ; i++ ){
      if(dp[i]) for(int j = 0 ; j < n ; j++ )
        if(i+data[j] <= x) dp[i+data[j]] = true;
    }
    if(1 < (ans = solve(x))) cout << ans << endl;
    else cout << "NA" << endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#define rep(i, a, n) for(int i = a;i < n;i++)
using namespace std;


int n, x;
int m[40];
int dp[40][1000010];


bool judge(int v){
	if(v <= 1) return false;
	if(v == 2) return true;
	for(int i = 2;i*i <= x+1;i++){
		if(v % i == 0) return false;
	}
	return true;
}


void makeDP(){
	rep(i, 1, n+1){
		rep(j, 0, x+1){
			if(j - m[i] >= 0) dp[i][j] = max(dp[i-1][j], dp[i][j - m[i]] + m[i]);
			else dp[i][j] = dp[i-1][j];
		}
	}
}


int solve(){
	for(int i = x;i >= 0;i--){
		if(judge(dp[n][i])) return dp[n][i];
	}
	return 0;
}


int main(){
	while(cin >> n >> x, n || x){
		rep(i, 1, n+1) cin >> m[i];

		makeDP();

		int ans = solve();

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

	return 0;
}

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

const int MAX = 1000001;
char p[MAX] = {0};
int a[31],n,x;

void solve(int dp[MAX]){
	
	for(int i=0 ; i < x ; i++ ){
		if( dp[i] ){
			for(int j=0 ; j < n ; j++ ){
				if( i+a[j] <= x ){
					dp[ i + a[j] ] = 1;
				}
			}
		}
	}
}

int main(){
	for(int i=2 ; i < MAX ; i++ ) p[i] = 1;
	for(int i=2 ; i < sqrt(MAX)+1 ; i++ ){
		if( p[i] ){
			for(int j=i+i ; j < MAX ; j += i ){
				p[j] = 0;
			}
		}
	}

	while( cin >> n >> x , n||x ){
		for(int i=0 ; i < n ; i++ ){
			cin >> a[i];
		}

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

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

C++: #include <iostream>

using namespace std;

bool budget[1000001];
bool P[1000001];

void eratos()
{
	for( int i = 0;i < 1000001;i++ ) P[i] = true;
	P[0] = P[1] = false;
	for( int i = 0;i < 1000001;i++ ){
		if( P[i] ) for( int j = 2;j * i < 1000001;j++ ) P[i * j] = false;
	}
}

void prim( int n, int x, int *v )
{
	for( int i = 1;i <= n;i++ ) budget[v[i]] = true;

	for( int i = 1;i <= x;i++ ) if( budget[i] ) {
		for( int j = 1;j <= n;j++ ) if( i + v[j] < 1000001 )budget[i + v[j]] = true;
	}
	int i;
	for( i = x;i >= 2;i-- ) if( budget[i] && P[i] ){ cout << i << endl; break;}
	if( i == 1 ) cout << "NA" << endl;
}

int main(int argc, char const* argv[])
{
	int n,x;
	int v[31];

	eratos();

	while( cin >> n >> x && x != 0 ){
		for( int i = 1;i <= n;i++ ) cin >> v[i];
		for( int i = 0;i < 1000001;i++ ) budget[i] = false;

		prim( n, x, v );
	}

	return 0;
}

C++: #include<iostream>
#include<math.h>

#define VMAX 1000000
#define KMAX 30

using namespace std;

	int K[KMAX];
	int T[KMAX][VMAX];

void eratos(int n,bool prime[]){
	for(int i=0;i<=n;i++)prime[i]=false;
	for(int i=3;i<=n;i+=2)prime[i]=true;
	prime[2]=true;
	int lim=(int)sqrt((double)n)+1;
	for(int i=3;i<=lim;i+=2){
		if(!prime[i])continue;
		for(int j=i+i;j<=n;j+=i)prime[j]=false;
	}
}	

int main(){
	bool prime[VMAX+1];
	eratos(VMAX,prime);
	
	int k,v;

	while(cin>>k>>v && !(k==0 && v==0)){
		for(int i=0;i<k;i++)cin>>K[i];
		
		for(int j=0;j<=v;j++)T[0][j]=0;
		for(int j=K[0];j<=v;j+=K[0])T[0][j]=1;
		for(int i=0;i<k;i++)T[i][0]=1;
		
		for(int i=1;i<k;i++){
			for(int j=1;j<K[i];j++)T[i][j]=T[i-1][j];
			for(int j=K[i];j<=v;j++){
				T[i][j]=(T[i-1][j] | T[i][j-K[i]]);
			}
		}
		int ans=-1;
		for(int i=v;i>=2;i--){
			if(prime[i] && T[k-1][i]){
				ans=i;
				break;
			}
		}
		if(ans==-1)cout<<"NA"<<endl;
		else cout<<ans<<endl;
	}

}

C++: #include<stdio.h>
int n,x;
int kin;
int i,j;
int dp[1000050],flg[1000050];
int main(void){
	scanf("%d %d",&n,&x);
	for(i=1;i<=1000000;i++){
		flg[i]=1;
	}
	for(i=2;i*i<=1000000;i++){
		if(flg[i]==1){
			for(j=2;j*i<=1000000;j++){
				flg[i*j]=0;
			}
		}
	}
	while(n!=0 && x!=0){
		for(j=1;j<=x;j++){
			dp[j]=-1;
		}
		dp[0]=0;
		for(i=0;i<n;i++){
			scanf("%d",&kin);
			for(j=0;j<=x;j++){
				if(dp[j]!=-1 && j+kin<=x){
					dp[j+kin]=1;
				}
			}
		}
		int max=0;
		for(i=0;i<=x;i++){
			if(dp[i]==1 && flg[i]==1){
				max=i;
			}
		}
		if(max==0){
			printf("NA\n");
		}
		else {
			printf("%d\n",max);
		}
		scanf("%d %d",&n,&x);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a,b) (abs((a)-(b))<EPS)
#define rep(i,j) for(int i=0;i<j;i++)
#define repi(i,j,k) for(int i=j;i<k;i++)
#define Max_N 1000010
bool prime[Max_N];
int menu[30];
bool dp[Max_N];
void eratos()
{
	repi(i,2,Max_N)prime[i]=true;
	prime[0]=false;
	prime[1]=false;
	for(int i=2;i*i<=Max_N;i++)
	{
		if(prime[i])
		{
			for(int j=2*i;j<Max_N;j+=i)
			{
				prime[j]=false;
			}
		}
	}
	return;
}
int main()
{
	rep(i,Max_N)dp[i]=false;
	eratos();
	while(1)
	{
		int n,x;
		cin >> n >> x;
		if(n==0&&x==0)break;
		for(int i=0;i<n;i++)
		{
			cin >> menu[i];
		}
		dp[0]=true;
		int ans=0;
		for(int i=1;i<=x;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(i-menu[j]>=0&&dp[i-menu[j]])
				{
					dp[i]=true;
				}
			}
			if(prime[i]&&dp[i])
			{
				ans=i;
			}	
		}
		if(ans!=0)
		{
			cout << ans << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
	return 0;
}