Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

const (
	MAX  = 1000000
	SQRT = 1000
)

func main() {
	comp := make([]int, MAX+2)
	sieve := func() {
		for i := 3; i < SQRT; i += 2 {
			if comp[i] == 0 {
				for j := i * i; j < MAX; j += i {
					comp[j] = 1
				}
			}
		}
	}
	sieve()
	tbl := make([]int, MAX+2)
	tbl[2] = 1
	k := 1
	for i := 3; i < MAX; i += 2 {
		if comp[i] == 0 {
			k++
		}
		tbl[i+1], tbl[i] = k, k
	}
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		ans := 0
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			p, _ := strconv.Atoi(buf[0])
			m, _ := strconv.Atoi(buf[1])
			a, b := p-m, p+m
			if a < 2 {
				a = 2
			}
			if b > MAX {
				b = MAX
			}
			ans += tbl[b] - tbl[a-1] - 1
		}
		if ans < 0 {
			ans = 0
		}
		fmt.Println(ans)
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, m, p, imos[1000009]; bool prime[1000009];
int main() {
	for (int i = 2; i < 1000000; i++) prime[i] = true;
	for (int i = 2; i < 1000; i++) {
		if (!prime[i]) continue;
		for (int j = i * i; j < 1000000; j += i) prime[j] = false;
	}
	while (cin >> n, n) {
		fill(imos, imos + 1000000, 0);
		for (int i = 0; i < n; i++) {
			cin >> m >> p;
			imos[max(m - p, 0)]++;
			imos[min(m + p + 1, 1000000)]--;
		}
		int s = 0, ret = 0;
		for (int i = 0; i < 1000000; i++) {
			s += imos[i];
			if (prime[i] && s) ret++;
		}
		cout << ret - 1 << endl;
	}
}

C++: #include<iostream>
#include <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 999983+100
#define MP 999983

int main(){
	bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	for(int i=MP+1;i<MAX_LIST;i++){
		prime[i]=0;
	}
	int prime_num[MAX_LIST];
	prime_num[0]=1;
	for(int i=1;i<MAX_LIST;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}
	
	
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		for(int i=0;i<pair_num;i++){
			int p,m;
			cin>>p;
			cin>>m;
			int x=0;
			int a,b;
			if(p+m>MP){
				a=MP;
			}else{
				a=p+m;
			}
			if(p-m<0){
				b=0;
			}else{
				b=p-m;
			}
			x=prime_num[a]-prime_num[b]+prime[b];
			
			if(x==0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
			
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

const int PRIME_SIZE = 1000000;

int main()
{
	bool prime[ PRIME_SIZE ] = { 0 };
	prime[ 0 ] = prime[ 1 ] = true;
	for( int i = 2; i < PRIME_SIZE; ++i ){
		if( prime[ i ] ) continue;
		for( int j = i + i; j < PRIME_SIZE; j += i ){
			prime[ j ] = true;
		}
	}

	int n;
	while( cin >> n && n ){
		int pay = -n;
		int* hit = new int[ PRIME_SIZE + 1 ]();
		for( int i = 0; i < n; ++i ){
			int p, m;
			cin >> p >> m;
			int low = max( 0, p - m );
			int high = min( PRIME_SIZE - 1, p + m );
			++hit[ low ];
			--hit[ high + 1 ];
		}
		int count = 0;
		for( int i = 0; i < PRIME_SIZE; ++i ){
			count += hit[ i ];
			if( !prime[ i ] ){
				pay += count;
			}
		}

		cout << ( pay < 0 ? 0 : pay ) << endl;

		delete[] hit;
	}
	return 0;
}

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

const int PRIME_SIZE = 1000000;

int main()
{
	bool prime[ PRIME_SIZE ] = { 0 };
	prime[ 0 ] = prime[ 1 ] = true;
	for( int i = 2; i < PRIME_SIZE; ++i ){
		if( prime[ i ] ) continue;
		for( int j = i + i; j < PRIME_SIZE; j += i ){
			prime[ j ] = true;
		}
	}

	int n;
	while( cin >> n && n ){
		int pay = 0;
		for( int i = 0; i < n; ++i ){
			int p, m;
			cin >> p >> m;
			int low = max( 0, p - m );
			int high = min( PRIME_SIZE - 1, p + m );
			int count = 0;
			for( int j = low; j <= high; ++j ){
				if( !prime[ j ] ){
					++count;
				}
			}
			if( count == 0 ){
				pay -= 1;
			}else{
				pay += ( count - 1 );
			}
		}

		cout << ( pay < 0 ? 0 : pay ) << endl;
	}
	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

const int PrimeMax = 999984;
int is_prime[PrimeMax],sum[PrimeMax];
void Eratosthenes(int N){
  for(int i = 0; i < N; i++){
    is_prime[i] = 1;
  }
  is_prime[0] = 0;
  is_prime[1] = 0;
  for(int i = 2; i*i < N ; i++){
    if(is_prime[i]){
      for(int j = 0; i * (j + 2) < N; j++){
        is_prime[i *(j + 2)] = 0;
      }
    }
  }
  REP(i,1,N){
    sum[i] += sum[i-1] + is_prime[i];
  }
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  Eratosthenes(PrimeMax);
  while(cin>>n && n){
    ll p,m,ans = 0;
    rep(i,n){
      ll f,s;
      cin >> p>>m;
      if(p - m-1 < 0){
        s = 0;
      }
      else{
        s = p -m-1;
      }
      f = (p+m > PrimeMax) ? PrimeMax-1: p + m;
      if(s == f){
        if(!is_prime[s])
          ans -= 1;
      }
      else if(sum[s]- sum[f] == 0){
        ans -= 1;
      }
      else{
        ans += (sum[f]-sum[s])-1;
      }
    }
    if(ans)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

const int MAX = 1000100;
bool prime[MAX];

void sieve(){
    fill(prime, prime+MAX, true);
    prime[0] = prime[1] = false;

    for(int i=0; i < MAX; i++){
            if(!prime[i]) continue;
            for(int j=i+i; j < MAX; j += i){
                        prime[j] = false;
                    }
        }
}

int main(){
    sieve();
    int n;
    while(cin >> n, n){
        int ans = 0;
        for(int i=0; i< n; i++){
            int a, b;
            cin >> a >> b;
            for(int j = max(2, a-b); j <= min(a+b, 999983); j++){
                if(prime[j]) ans++;
            }
            ans--;
        }
        cout << ans << endl;
    }
}

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

#define mx 1000001

using namespace std;

bool prm[mx];
int cnt[mx];

void mkPrm()
{
  fill(prm, prm+mx, true);
  prm[0] = prm[1] = false;

  for(int i = 2; i < mx; ++i)
    if(prm[i])
      for(int j = i+i; j < mx; j+=i) prm[j] = false;

  int t = 0;
  for(int i = 0; i < mx; ++i){
    if(prm[i]) ++t;
    cnt[i] = t;
  }

  return ;
}


int main()
{
  int n, s, p, m, c;
  mkPrm();

  while(cin>>n && n){
    s = 0;
    while(n--){
      cin >> p >> m;
      c = ((p+m >= mx)? cnt[mx-1]: cnt[p+m]);
      c -= (p-m < 0)? cnt[0] : cnt[p-m];
      s--;
      if(c > 0) s += c;
    }
    cout << ((s < 0)? 0 : s) << endl;
  }

  return 0;
}