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])
		prime_table := make([]int, 1000001)
		prime_table[0] = -1
		prime_table[1] = -1
		for i := 2; i < 1000001; i++ {
			if ^prime_table[i] != 0 {
				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>
#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;
    }
}

C++: #include <iostream>

using namespace std;

bool flag[1000001];
int add[30], n, a = 0, x, m, price[30];

bool sosuu(int k) {
	if (k == 2) return true;
	else if (k < 2) return false;
	else if (k % 2 == 0) return false;
	for (int i = 3; i*i<=k; i += 2)
		if (k % i == 0) return false;
	return true;
}
void next(int k) {
	if (flag[k]) return;
	flag[k] = true;
	if (k > x) return;
	if (k > m) if (sosuu(k)) m = k;
	for (int i = 0; i < n; ++i)
		next(k + price[i]);
}

int main() {
	int pattern[30];
	while (cin >> n >> x, n || x) {
		for (int i = 0; i < 1000001; ++i) flag[i] = false;
		for (int i = 0; i < n; ++i)
			cin >> price[i];
		int p = 0, a = 0;
		m = 0;
		next(0);
		if (m) cout << m << endl;
		else cout << "NA" << endl;
	}
}

C++: #include<stdio.h>
int hideo[1000001] = { 0 };
int main(void)
{
	int  n, x, temp = 1,noyori=0,debu=0 , ruio[31] = { 0 };
	while (scanf("%d %d", &n, &x)) {
		if (n == 0 && x == 0) {
			return 0;
		}
		for (int i = 1; i <= n; i++) {
			scanf("%d", &ruio[i]);
			hideo[ruio[i]] = 1;
		}
		while (temp != x) {
			if (hideo[temp] == 1) {
				for (int j = 1; j <= n; j++) {
					if (hideo[temp + ruio[j]] == 0) {
						hideo[temp + ruio[j]] = 1;
					}
				}
			}
			temp++;
		}
		for (int i = x; i >= 1; i--) {
			if (hideo[i] == 1) {
				for (int j = 2; j < i - 1; j++) {
					if (i%j == 0) {
						debu=1;
					}
				}
				if (debu == 0) {
					noyori = i;
					break;
				}
			}
			debu = 0;
		}
		if (noyori > 1 ||noyori==x) {
			printf("%d\n", noyori);
		}
		else {
			printf("NA\n");
		}
		temp = 1;
		noyori = 0;
		debu = 0;
		for (int i = 1; i <= n; i++) {
			ruio[i] = 0;
		}
		for (int i = 1; i <= x; i++) {
			hideo[i] = 0;
		}
	}
	return 0;
}