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 <vector>
#include <set>
#include <algorithm>
#include <cmath>

using namespace std;
const int MAX = 1000000;
vector<int> Primes;
bool isPrime[MAX] = {0};

void sieve(){
    bool find[MAX] = {0};
    int i;
    for(i=2;i*i < MAX;i++){
        if(find[i]) continue;
        Primes.push_back(i);
        for(int j=i;j<MAX;j++){
            if(j%i == 0) find[j] = true;
        }
    }
    for(;i<MAX;i++){
        if(!find[i]) Primes.push_back(i);
    }
    for(int i=0;i<Primes.size();i++){
        isPrime[Primes[i]] = true;
    }
    Primes.clear();
}

int main(){
    sieve();

    while(true){
        int n,x;
        cin >> n >> x;
        if(n==0 && x==0) return 0;
        vector<int> R(n);
        for(int i=0;i<n;i++){
            cin >> R[i];
        }

        set<int> S;
        S.insert(0);
        for(int i=0;i<R.size();i++){
            for(set<int>::iterator it=S.begin();it!=S.end();++it){
                int newe = *it + R[i];
                if(newe > x) continue;
                S.insert(newe);
            }
        }
        int maxn = -1;
        for(set<int>::iterator it=S.begin();it!=S.end();++it){
            if(isPrime[*it] && *it > maxn) maxn = *it;
        }
        if(maxn == -1) cout << "NA" << endl;
        else cout << maxn << endl;
    }
    return 0;
}

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

constexpr int MAX_N = 31, MAX_C = 1000001;
int n, max;
int cost[MAX_N];
bool dp[MAX_C];
bool prime_num[MAX_C];

int main() {
	while (1) {
		std::cin >> n >> max;
		if (n + max == 0)
			break;
		std::memset(prime_num, false, sizeof(prime_num));
		std::memset(dp, false, sizeof(dp));
		for (int i = 0; i < n; ++i)
			std::cin >> cost[i];
		for (int i = 2; i * i <= max; ++i) {
			if (!prime_num[i]) {
				for (int j = 2; i*j <= max; ++j)
					prime_num[i*j] = true;
			}
		}
		int ans = 0;
		for (int i = 0; i < n; ++i)
			dp[cost[i]] = true;

		for (int i = 1; i <= MAX_C; ++i) {
			for (int j = 0; j < n; ++j) {
				if (dp[i])
					dp[i + cost[j]] = true;
			}
		}

		for (int i = max; i >= 0; --i) {
			if (dp[i] && !prime_num[i]) {
				ans = i; 
				break;
			}
		}
		if (!ans)
			std::cout << "NA" <<std::endl;
		else
			std::cout << ans << std::endl;
	}
	return 0;
}

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

using namespace std;
vector<int> prime;
bool isPrime[1000001];
bool dp[1000001];
vector<int> price;

void PrimeCalc()
{
	memset(isPrime, 1, sizeof(isPrime));
	isPrime[0] = isPrime[1] = false;
	for(int i = 2; i < 1000001; ++i)
	{
		if(isPrime[i])
		{
			for(int j = 2 * i; j < 1000001; j += i)
			{
				isPrime[j] = false;
			}
		}
	}
}

void dpCalc(int sum, int& x)
{
	if(dp[sum] || sum > x)
	{
		return;
	}
	else
	{
		dp[sum] = true;
	}
	for(int i = 0; i < price.size(); ++i)
	{
		dpCalc(sum + price[i], x);
	}
}

void solve()
{
	PrimeCalc();
	int n, x;
	while(cin >> n >> x, n || x)
	{
		price.clear();
		price.resize(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> price[i];
		}
		memset(dp, 0, sizeof(dp));
		dpCalc(0, x);
		bool ok_flag = false;
		for(int i = x; i >= 1; --i)
		{
			if(dp[i] == true && isPrime[i] == true)
			{
				cout << i << endl;
				ok_flag = true;
				break;
			}
		}
		if(!ok_flag)
		{
			cout << "NA" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

const int MAX_SQRT_B = 1000001;
const int MAX_RANGE = 1000001;

typedef unsigned long long U;

vector<U> primes;

void elatos(U a, U b){
  if(a < 2) a = 2;
  if(a >= b) return;

  bool isp_r[MAX_RANGE];
  bool isp_sqrtb[MAX_SQRT_B];
  memset(isp_r , 1 , sizeof(isp_r));
  memset(isp_sqrtb , 1 , sizeof(isp_sqrtb));

  for(U i=2; (U)i*i < b; i++)
    if(isp_sqrtb[i]){
      for(U j=2*i; (U)j*j < b; j+=i) isp_r[j] = 0;
      for(U j = max((U)2,(a+i-1)/i)*i; j<b; j+=i) isp_r[j-a] = 0;
    }
  for(U i=a; i<b; i++) if(isp_r[i-a]) primes.push_back(i);
}

bool isp(U n){
  vector<U>::iterator i = lower_bound(primes.begin(), primes.end(), n);
  return *i == n;
}

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

int p[31];
int m[31];
int dp[1000001];

int main(){
  elatos(0,1000001);
  int n;
  int bu;
  while(cin >> n >> bu , n + bu){
    rep(i,n){
      cin >> p[i];
      m[i] = bu/p[i];
    }
    memset(dp,-1,sizeof(dp));
    dp[0] = 0;
    rep(i,n){
      rep(j,bu+1){
	if(dp[j] >= 0){
	  dp[j] = m[i];
	}
	else if(j < p[i] || dp[j-p[i]] <= 0){
	  dp[j] = -1;
	}
	else {
	  dp[j] = dp[j - p[i]] - 1;
	}
      }
    }
    int ans = -1;
    for(int i=bu+(bu+1)%2; i>=1; i-=2){
      if(dp[i] >= 0 && isp(i)){
	ans = i;
	break;
      }
    }
    if(ans > 0){
      cout << ans << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int n,x;
int m[50];
bool dp[1000001];

bool isprime(int num){
  if(num<=1) return 0;
  for(int i=2;i*i<=num;i++){
    if(num%i==0) return 0;
  }
  return 1;
}


int main(){
  while(cin>>n>>x && (n||x)){
    for(int i=0;i<n;i++) cin>>m[i];
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=0;i<n;i++){
      for(int j=0;j<=x;j++){
	if(dp[j] && j+m[i]<=x) dp[j+m[i]]=1;
      }
    }
    int ans=0;
    for(int i=x;i>=0;i--) {
      if(dp[i] && isprime(i) ){
	ans=i;
	break;
      }
    }
    if(ans==0) cout<<"NA"<<endl;
    else cout<<ans<<endl;
	
  }
  
  return 0;
}

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

const int N = 1000000;
int dp[N + 1], primes[N + 1];
int main(){
    fill(primes, primes + N + 1, 1);
    primes[0] = primes[1] = 0;
    for(int i = 2;i <= sqrt(N);i += 2){
        if(primes[i]){
            for(int j = i + i;j <= N;j += i){
                primes[j] = 0;
            }
        }
        if(i == 2) --i;
    }
    int n, x, a, ans;
    int ps[30];
    while(cin >> n >> x, n && x){
        fill(dp, dp + N + 1, 0);
        ans = 0;
        for(int i = 0;i < n;++i){
            cin >> a;
            ps[i] = a;
            dp[a] = 1;
        }
        for(int i = 0;i <= x;++i){
            if(dp[i]){
                for(int j = 0;j < n;++j){
                    if(i + ps[j] <= N && !dp[i + ps[j]]){
                        dp[i + ps[j]] = 1;
                    }
                }
            }
        }
        for(int i = x;i > 0;--i){
            if(dp[i] && primes[i]){
                ans = i;
                break;
            }
        }
        if(ans) cout << ans << endl;
        else cout << "NA" << endl;
    }
}