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 <cmath>
#define VMAX  1000000
using namespace std;

int main(){
  int k, v;
  char prime[VMAX+1];
  for (int i=0;i<=VMAX;i++) prime[i] = false;
  for (int i=3;i<=VMAX;i+=2) prime[i] = true;
  prime[2] = true;
  int limit = (int)sqrt((double)VMAX) + 1;
  for (int i=3;i<=limit;i+=2){
    if (!prime[i]) continue;
    for (int j=i+i;j<=VMAX;j+=i)prime[j]=false;
  }

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

  }
}

C++: #include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>

using namespace std;

int main(){
	bool sosuu[1000011];
	bool dp[1000011];
	
		for(int i=0;i<=1000000;i++){
			sosuu[i]=0;
			dp[i]=0;
		}
		for(int i=2;i<=500000;i++){
			if(sosuu[i]==0){
				for(int j=i;j<=1000000;j+=i){
					sosuu[j]=1;
				}
				sosuu[i]=0;
			}
		}
		sosuu[0]=1;
		sosuu[1]=1;
	while(1){
		
		int item[30];
		
		int n,m;
		scanf("%d%d",&n,&m);
		
		if(n==0 && m==0){
			break;
		}
		for(int i=0;i<n;i++){
			scanf("%d",&item[i]);
		}
		dp[0]=1;
		for(int i=0;i<=m;i++){
			for(int j=0;j<n;j++){
				if(i<item[j]){
					continue;
				}
				if(dp[i-item[j]]==1){
					dp[i]=1;
				}
			}
		}
		int num=0;
		for(int i=m;i>=0;i--){
			if(dp[i]==1&&sosuu[i]==0){
				num=i;
				break;
			}
		}
		if(num==0){
			puts("NA");
		}else{
			printf("%d\n",num);
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <cstdio>	// require printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen

using namespace std;

const int MAX_N = 1000000;
bool prime[MAX_N+1];

void make_prime (void )
{
	fill (prime, prime + MAX_N, true );

	prime[0] = false;
	prime[1] = false;

	for (int i = 2; i < MAX_N + 1; ++i ){
		if (prime[i] ){
			for (int j = 2*i; j < MAX_N + 1; j += i ){
				prime[j] = false;
			} // end for
		} // end if
	} // end for
}

bool dp[31][MAX_N+1];

int main()
{
//	cut here before submit 
//	freopen ("testcase.be", "r", stdin );
	int n, x;

	make_prime ();

	while (cin >> n >> x && n && x ){
		vector <int> cost (n, 0 );
		for (int i = 0; i < n; ++i ){
			cin >> cost[i];
		} // end for
		memset (dp, false , sizeof (dp ) );

		for (int j = cost[0]; j <= x; j += cost[0] )
			dp[0][j] = true;

		for (int i = 0; i < n; ++i ){
			for (int j = 0; j <= x; ++j ){
				if (j < cost[i] ){
					dp[i+1][j] = dp[i][j];
				}else{
					dp[i+1][j] = dp[i][j] | dp[i+1][j - cost[i]];
				} // end if
			} // end for
		} // end for 

		int res = 0;
		for (int j = MAX_N; j >= 0; --j ){
			if (dp[n][j] && prime[j] ){
				res = j;
				break;
			} // end if
		} // end for

		if (res == 0 || res == x ){
			cout << "NA" << endl;
		}else{
			cout << res << endl;
		} // end if
	} // end loop
		
	return 0;
}

C++: #include<iostream>
using namespace std;
bool prime(int);
int main()
{
while (true){
	bool dp[1000001] = { false };
	int a[31];
	int n, v;
	cin >> n >> v;
	if (n == 0){
		break;
	}
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	dp[0] = true;
	for (int i = 0; i < n; i++){
		for (int i2 = 0; i2 <= v - a[i]; i2++){
			if (dp[i2]){
				dp[i2 + a[i]] = true;
			}
		}
	}

	bool flag = true;
	for (int i = 1000000; i > 0; i--){
		if (dp[i] && !prime(i)){
			flag = false;
			cout << i << endl;
			break;
		}
	}
	if (flag)
		cout << "NA" << endl;
}

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

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[31][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(1){
    cin >> 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[k][m] != -1)return memo[k][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[k][m] = max(solve(k,m+v[k]),solve(k+1,m));
}