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>

using namespace std;

#define MAX 1000001

bool is_prime[MAX];

void Erathos() {
  fill(is_prime, is_prime + MAX, true);
  
  is_prime[0] = is_prime[1] = true;
  
  for(int i=2; i*i<=MAX; i++) {
    if(is_prime[i]) {
      for(int j=i*i; j<=MAX; j+=i) {
	is_prime[j] = false;
      }
    }
  }
}

bool dp[MAX];
int price[30];

int main() {
  
  Erathos();
  
  int n, x;
  
  while(cin >> n >> x && n+x) {
    fill(dp, dp + MAX, 0);
    
    for(int i=0; i<n; i++) {
      cin >> price[i];
    }
    
    dp[0] = 1;
    for(int i=1; i<=x; i++) {
      for(int j=0; j<n; j++) {
	if(i - price[j] >= 0) {
	  dp[i] = dp[i] | dp[i-price[j]];
	}
      }
    }
    
    int ans = 0;
    for(int i=0; i<=x; i++) {
      if(dp[i] && is_prime[i]) {
	ans = max(ans, i);
      }
    }
    
    if(ans) cout << ans << endl;
    else cout << "NA" << endl;
  }
    
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ANSWER(x) cout<<x<<endl
#define debug(x) cout<<#x<<": "<<x
const int MAX_INT = 1000000;
bool er[1000001];
int n,x,ans;
int v[31];
int solve(int k,int m);
int memo[1000001];
int main(){
  memset(er,true,sizeof(er));
  for(int i=2;i<=sqrt(MAX_INT);i++){
    if(er[i]){
      for(int j=0;i*(j+2)<MAX_INT;j++){
	er[i*(j+2)]=false;
      }
    }
  }
  while(cin >> n >> x&&n&&x){
    if(!n && !x)break;
    memset(memo,-1,sizeof(memo));
    for(int i=0;i<n;i++){
      cin >> v[i];
    }
    
    ans = solve(0,0);
    if(!ans)cout << "NA\n";
    else cout << ans << endl;
  }
}
int solve(int k,int m){
  if(memo[m] != -1)return memo[m];
  if(m > x)return 0;
  else if(k == n){
    if(m == 0)return 0;
    if(er[m])return m;
    else return 0;
  }
  return memo[m] = max(solve(k,m+v[k]),solve(k+1,m));
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

bool isp(int n){
    if(n==2)return true;
    if(n==1)return false;
    if(n==0)return false;

    for(int i=2;i<=sqrt(n);i++){
        if(n%i==0)return false;
    }
    return true;
}

int main(){
    int n,x;
    while(cin>>n>>x){
        if(n==0&&x==0)break;
        
        int data[40];
        rep(i,n){
            cin>>data[i];
        }
        
        bool dp[5][300001]={};
        rep(i,5)rep(j,300001)dp[i][j]=false;
        
        dp[0][0]=true;
        

        for(int i=1;i<=n;i++){
            for(int j=0;j<=x;j++){
                dp[i][j] = dp[i-1][j];

                dp[i][j] |= dp[i][j-data[i-1]];

            }
        }
        int ans = -1;
        for(int i=x;i>=0;i--){
            if(dp[n][i]==true){
                if(isp(i)){
                    ans=i;
                    break;
                }
            }
        }
        if(ans==-1)cout<<"NA"<<endl;
        else cout<<ans<<endl;
        
    }
}

C++: #define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
#include<map>
#include<string>
using namespace std;

bool is_prime[ (int)1e6+1 ];

int main(){
	fill_n(is_prime, 1e6+1, true);
	is_prime[0] = is_prime[1] = false;
	for(int i = 2; i <= 1e6; i++){
		if(!is_prime[i]) continue;
		for(int j = 2; i*j <= 1e6; j++) is_prime[i*j] = false;
	}

	int N, X;
	while(cin >> N >> X, N || X){
		int C[30], ans = -1;
		bool dp[ (int)1e6+1 ] = {};
		for(int i = 0; i < N; i++){
			cin >> C[i];
		}
		dp[0] = true;
		for(int j = 0; j < X; j++){
			if(!dp[j]) continue;
			for(int k = 0; k < N; k++){
				if(j + C[k] > X) continue;
				dp[j + C[k]] = true;
				if(is_prime[j + C[k]]) ans = max(ans, j + C[k]);
			}
		}
		if(ans == -1) puts("NA");
		else printf("%d\n", ans);
	}
	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){
		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;
}

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 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){
		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 && prime[x-i]==0){
				flg=1;
				break;
			}
		}
		if(flg==1){
			printf("%d\n",x-i);
		}
		else puts("NA");
		for(i=0;i<1000000;i++){
			c[i]=0;
		}
		for(;;);
		//
	}
	
	return 0;
}

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

#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)

int n,x;
int t[1000000];
bool dp[32][1000002];

bool p[1000002];

void solve(){
  memset(dp,0,sizeof(dp));
  dp[0][0] = true;

  rep(i,n){
    rep(j,x+1){
      if(j < t[i]){
        dp[i+1][j] = dp[i][j];
      }
      else{
        dp[i+1][j] = dp[i][j] || dp[i+1][j-t[i]];
      }
    }
  }

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

  if(i != 0){
    cout<<i<<endl;
  }
  else{
    cout<<"NA\n";
  }
}

int main(){
  memset(p,true,sizeof(p));
  p[0] = p[1] = false;

  for(int i=2;i*i<=1000000;i++){
    if(p[i]){
      for(int j=i+i;j<=1000000;j+=i){
        p[j] = false;
      }
    }
  }

  while(cin>>n>>x,n||x){
    rep(i,n){
      cin>>t[i];
    }
    solve();
  }
}