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 <string.h>
#include <math.h>

using namespace std;

const int N = 1000000;

int prime[N+1];
int dish[30];
int n, x;
int max_val;

void setPrime()
{
	memset(&prime[0], 0, sizeof(int) * (N+1));
	prime[2] = 1;
	for(int i=2; i<=N; i++)
	{
		bool is_prime = true;
		int limit = sqrt(i);
		for(int k=2; k<=limit; k++)
		{
			if(i % k == 0)
			{
				is_prime = false;
				break;
			}	
		}
		
		if(is_prime)
		{
			prime[i] = 1;
		}
	}
}

void solve(int price, int depth)
{
	if(depth == n-1)
	{
		if(prime[price] == 1 && max_val < price)
		{
			max_val = price;
		}
		return;
	}
	
	while(price <= x)
	{
		solve(price, depth+1);
		price += dish[depth];
	}
}

int main()
{
	setPrime();
	
	while( 1 )
	{
		cin >> n >> x;
		if(n==0 && x==0)
		{
			break;
		}
		
		for(int i=0; i<n; i++)
		{
			cin >> dish[i];
		}
		
		max_val = 0;
		solve(0, 0);
		
		if(max_val != 0)
		{
			printf("%d\n", max_val);
		}
		else
		{
			printf("NA\n");
		}
	}
}

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

const int MX = 1000001;
int n, x;
bool tbl[MX], sieve[MX];
int v[30];

void makeSieve() {
    memset(sieve, false, sizeof sieve);
    sieve[0] = sieve[1] = true;
    for (int i=2; i*i<MX; ++i) {
        if ( ! sieve[i]) {
            for (int j=i*i; j<MX; j+=i) {
                sieve[j] = true;
            }
        }
    }
}

int main() {
    makeSieve();
    while (cin >> n >> x, n) {
        for (int i=0; i<n; ++i) {
            cin >> v[i];
        }
        memset(tbl, false, sizeof tbl);
        tbl[0] = true;
        for (int i=0; i<n; ++i) {
            for (int j=v[i]; j<=x; ++j) {
                tbl[j] |= tbl[j-v[i]];
            }
        }
        bool ok = false;
        for (int i=x; i>=0; --i) {
            if (tbl[i] && ! sieve[i]) {
                cout << i << endl;
                ok = true;
                break;
            }
        }
        if ( ! ok) {
            cout << "NA" << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <limits>
#include <cstring>

constexpr int MAX_X = 1000001;
constexpr int MAX_N = 31;

int n, x, values[MAX_N];
bool is_prime_number[MAX_X] = {true};

void initialize();

void solve();

int main(){
   initialize();
   while(true){
       memset(values, 0, MAX_N);

       std::cin >> n >> x;
       
       if(n + x == 0) break;
       
       for(int i = 0; i < n; ++i)
           std::cin >> values[i];

       solve();
   }
   return 0;
}

void initialize(){
    memset(is_prime_number, true, MAX_X);
    is_prime_number[0] = false, is_prime_number[1] = false;
    for(int i = 2; i * i <= MAX_X; ++i){
        if(is_prime_number[i]){
            for(int j = i + 1; j < MAX_X; ++j){
                if(j % i == 0)
                    is_prime_number[j] = false;
            }
        }
    }
}

void solve(){
   int ans = 0;
   int dp[MAX_X];
   memset(dp, 0, MAX_X);

   /*
   std::cout << "x : " << x << std::endl << "n : " << n << std::endl;
   for(int i = 0; i < (x > 1000 ? 1000 : x); ++i){
       std::cout << i << " dp : " << dp[i] << std::endl;
   }
   */

   for(int i = 1; i <= x; ++i){
       int res = 0;
       for(int j = 0; j < n; ++j){
           if(values[j] <= i){
               res = std::max(dp[i - values[j]] + values[j], res);
           }
       }
       dp[i] = std::max(dp[i], res);
       if(is_prime_number[dp[i]]){
           ans = std::max(dp[i], ans);
       }
   }

   if(ans != 0){
       std::cout << ans << std::endl;
   }
   else{
       std::cout << "NA" << std::endl;
   }

}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int XMAX = 1000000;
constexpr int SQRTXMAX = sqrt(XMAX);

bool is_prime[XMAX + 1];
bool dp[XMAX + 1];
int cost[30];

int main() {
  // prime
  memset(is_prime, true, sizeof(is_prime));
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= SQRTXMAX; i++) {
    if (is_prime[i]) {
      int current = i * i;
      for (int j = i * i; j <= XMAX; j += i) {
        is_prime[j] = false;
      }
    }
  }

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

    // dp
    int r = 0;
    dp[0] = true;
    for (int i = 1; i <= x; i++) {
      dp[i] = false;
      for (int j = 0; j < n; j++) {
        int k = i - cost[j];
        if (k >= 0 && dp[k]) {
          dp[i] = true;
          if (is_prime[i]) r = i;
          break;
        }
      }
    }

    if (r == 0) {
      cout << "NA" << endl;
    } else {
      cout << r << endl;
    }
  }
  return 0;
}

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

int main(void){
	bool isprime[N+1];
	for(int i=0;i<=N;i++)
		isprime[i]=true;
	isprime[0]=isprime[1]=false;
	for(int i=2;i<=N;i++)
		if(isprime[i])
			for(int j=2*i;j<=N;j+=i)
				isprime[j]=false;

	bool budget[N+1];
	int n,m;
	while(cin >> n>>m,n|m){
		for(int i=0;i<=m;i++)
			budget[i]=false;
		int tmp;
		budget[0]=true;
		for(int i=0;i<n;i++){
			cin >> tmp;
			for(int j=0;j<=m;j++)
				if(budget[j])
					budget[j+tmp]=true;
		}
		int MAX=0;
		for(int i=m;i>0;i--){
			if(budget[i]&isprime[i]){
				MAX=i;break;
			}
		}
		if(MAX)
			cout << MAX << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
const int MAX = 1000000;
bool prime[1000001];
bool dp[1000001];

void Init(){
	memset(dp, false, sizeof(dp));
	dp[0] = true;
}

void era(){
	prime[0] = prime[1] = false;
	for(int i = 2; i <= MAX; i++){
		if(prime[i]){
			for(int j = i*2; j <= MAX; j+=i) prime[j] = false;
		}
	}
}

int main(){
	int n, limit;
	memset(prime, true, sizeof(prime));
	era();
	while(cin >> n >> limit && ((n!=0)||(limit!=0))){
		Init();
		int maxc = 0;
		for(int i = 1; i <= n; i++){
			int tmp;
			cin >> tmp;
			dp[tmp] = true;
			for(int j = 0; j+tmp <= limit; j++){
				if(dp[j]){
					dp[j+tmp] = true;
					if(prime[j+tmp]&&maxc<j+tmp) maxc = j+tmp; 
				}
			}
		}
		if(!maxc) cout << "NA" << endl;
		else cout << maxc << endl;
	}
	return 0;
}