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 <cstring>

constexpr int MAX_X = 1000000, MAX_N = 30;

int value[MAX_N], n, x;
bool dp[MAX_X + 1];
bool is_prime_number[MAX_X + 1];

void initialize();

void solve();

int main(){
    initialize();

    while(true){
        std::cin >> n >> x;
        if(n == 0 && x == 0){
            break;
        }
        
        memset(dp, false, x + 1);

        for(int i = 0; i < n; ++i){
            std::cin >> value[i];
            dp[value[i]] = true;
        }

        solve();
    }
    return 0;
}

void solve(){
    int ans = 0;

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

                    if(is_prime_number[i]){
                        ans = std::max(ans, i);
                    }
                }
            }
        }
    }

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

void initialize(){
    memset(is_prime_number, true, MAX_X + 1);

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

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

const long N=1000000;

bool prime[N+1];
bool dp[N+1];

int main(){
	prime[0]=prime[1]=false;
	for(long i=2; i<=N; ++i) prime[i]=true;
	
	for(long i=2; i<=N; ++i){
		if(prime[i]) for(long j=2; i*j<=N; ++j) prime[i*j]=false;
	}
	
	while(1){
		int n;
		long x, c[30];
	
		scanf(" %d %ld", &n, &x);
		if(n==0) break;
		for(int i=0; i<n; ++i) scanf(" %ld", &c[i]);
		
		for(long i=0; i<=N; ++i) dp[i]=false;
		
		dp[0]=true;
		for(int i=0; i<n; ++i){
			for(long j=0; j+c[i]<=x; ++j){
				if(dp[j]) dp[j+c[i]]=true;		
			}
		}
		
		long max=0;
		for(long i=0; i<=x; ++i){
			if(dp[i] && prime[i]) max=i;
		}
			
		if(max==0) printf("NA\n");
		else printf("%ld\n", max);
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
bool dp[1000020];
bool notPrime[1000020];
int main(){
    notPrime[0] = notPrime[1] = true;
    for(int i = 2;i*i < 1000010;i++){
        for(int j = 2 * i;j*j < 1000010;j+=i){
            notPrime[j] = true;
        }
    }
    int n,x;
    while(cin>>n>>x,n){
        while(n--){
            int a;
            cin>>a;
            dp[0] = true;
            for(int i = 0;i <= x;i++){
                if(dp[i])dp[i+a] = true;
            }
        }
        bool flg = false;
        for(int i = x;i >= 2;i--){
            if(notPrime[i] & dp[i]){
                cout<<i<<endl;
                flg = true;
                break;
            }
        }
        if(!flg)cout<<"NA"<<endl;
    }
}

C++: #include<stdio.h>
int g[1000001],k[30],i,n;

int prime(int);
int goukei(int);
int saiki(int);

int saiki(int y){
	if(prime(y) && goukei(y))return y;
	if(y>2)return saiki(y-1);
	else return 0;
}

int goukei(int z){
	if(g[z]==2)return 1;
	else if(g[z]==1)return 0;
	for(i=0;i<n;i++){
		if(z-k[i]>0) goukei(z-k[i]);
		else if(z-k[i]==0){
			g[z]=2;
			return 1;
		}
	}
	return 0;
}

int prime(int n){
	int i=3;
	if(n<2)return 0;
	else if(n==2)return 1;
    if(n%2==0)return 0;
    for(;i*i<=n;i+=2)if(n%i==0)return 0;
	return 1;
}

int main(){
	int x,a;
	while(1){
		scanf("%d%d",&n,&x);
		if(!n&&!x)break;
		for(i=0;i<n;i++) scanf("%d",&k[i]);
		a=saiki(x);
		if(a)printf("%d\n",saiki(x));
		else puts("NA");
	}
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector<double> vd;
typedef std::vector<std::string> vs;
typedef std::vector<pii> vpii;
typedef std::vector<vi> vvi;
typedef std::vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { rep(i,v.size()) is >> v[i]; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

bool isPrime(const int &n) {
  if (n == 1) return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}

main {
  int n, x;
  while (cin >> n >> x, n) {
    vi v(n);
    cin >> v;
    vi dp(x + 1);
    dp[0] = true;
    rep(i, v.size()) rep(j, x + 1 - v[i]) dp[j + v[i]] |= dp[j];
    for (int i = x; i > 0; i--) {
      if (dp[i] && isPrime(i)) {
        cout << i << endl;
        goto next;
      }
    }
    cout << "NA" << endl;
  next:;
  }
}

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

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

const int MAX_N = 1000 * 1000 + 10;
bool isPrime[MAX_N];

void makePrimes() {
  REP(i, MAX_N) isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i < MAX_N; i++)
      if (isPrime[i])
          for (int j = i * i; j < MAX_N; j += i)
              isPrime[j] = false;
}

bool can[MAX_N];

int main() {
  makePrimes();

  for (int N, X; cin >> N >> X, (N || X); ) {
      memset(can, 0, sizeof(can));
      can[0] = true;
      REP(i, N) {
          int a; cin >> a;
          for (int p = 0; p + a <= X; p++)
              if (can[p]) can[p + a] = true;
      }

      int ans = -1;
      for (int i = 0; i <= X; i++)
          if (isPrime[i] && can[i])
              ans = i;
      if (ans == -1)
          cout << "NA" << endl;
      else
          cout << ans << endl;
  }
  return 0;
}