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 <cstring>
#include <cmath>
using namespace std;

int count[1000001];
int size;
char flg[1000001];

void mkPrime(void){
	memset(flg,1,sizeof(flg));
	flg[0] = flg[1] = 0;

	for(int i=2;i<=999983;i++){
		if(flg[i]){
			for(int j=i*2;j<=999983;j+=i){
				flg[j] = 0;
			}
		}
	}
	for(int i=999984;i<=1000000;i++){
		flg[i] = 0;
	}
}

int calX(int from,int to){
	int res = 0;
	for(int i=from;i<=to;i++){
		res += flg[i];
	}
	return res;
}

int main(void){
	int n;

	mkPrime();

	while(cin>>n && n){
		int minP = 1000000;
		int maxP = 0;

		memset(count,-1,sizeof(count));
		while(n--){
			int p,m;
			cin>>p>>m;
			if(minP > p) minP = p;
			if(maxP < p) maxP = p;
			if(count[p] == -1) count[p] = 0;
			count[p] += calX(p-m<0?0:p-m, p+m>1000000?1000000:p+m);
		}

		int ans = 0;
		for(int i=minP;i<=maxP;i++){
			if(count[i] > 0) ans += count[i] - 1;
			else if(count[i] == 0) ans--;
		}

		cout<<ans<<endl;
	}

	return 0;
}

C++: 
#include<iostream>
using namespace std;
#define MAX 1000000
int main()
{
  int b[MAX+1]={0},i,j;
  b[0]=b[1]=1;
  for(i=4;i<=MAX;i+=2)b[i]=1;
  for(i=3;i*i<=MAX;i+=2)
    if(!b[i])for(j=i+i;j<=MAX;j+=i)b[j]=1;
  int c[MAX+1]={0};
  for(i=1;i<=MAX;i++)c[i]=c[i-1]+!b[i];

  int n,p,m,X,f,l;
  while(cin>>n,n){
    X=0;
    for(i=0;i<n;i++){
      cin>>p>>m;
      f=p-m-1;if(f<0)f=0;
      l=p+m;if(l>MAX)l=MAX;
      X+=c[l]-c[f]-1;
    }
    cout<<X<<endl;
  }
}

C++: #include<iostream>
using namespace std;
int x[2000000];

void prime(int p) {
	x[2] = 1;
	for (int i = 3; i < p; i += 2) {
		for (int j = 3; j < p; j += 2) {
			if (i%j == 0) { goto E; }
		}
		x[i] = 1;
	E:;
	}
}

int n, m, c, sum;

int main() {
	prime(1000000);
	while (true) {
		sum = 0;
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m >> c;
			for (int j = m - c; j <= m - c; j++) {
				sum += x[j];
			}
			sum -= 1;
		}
		cout << sum << endl;
	}
}

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*2];
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,0,N){
    sum[i+1] += sum[i] + is_prime[i];
  }
  REP(i,N,2*N){
    sum[i+1] = sum[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;
      s = max(0LL,p-m-1);
      f = p+m;
      ans += (sum[f]-sum[s])-1;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int x[2000000];

void prime(int p) {
	x[2] = 1;
	for (int i = 3; i < p; i += 2) {
		for (int j = 3; j * j < p; j += 2) {
			if (i%j == 0) { goto E; }
		}
		x[i] = 1;
	E:;
	}
}

int n, m, c, sum;

int main() {
	prime(1000000);
	while (true) {
		sum = 0;
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m >> c;
			for (int j = m - c; j <= m - c; j++) {
				sum += x[j];
			}
			sum -= 1;
		}
		cout << sum << endl;
	}
}

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

int pri[1000000+1];
bool is_prime[1000000+1];
int prime(int n){
	int p=0;
	for(int i=0;i<n+1;i++){
		is_prime[i] = true;
	}
	is_prime[0] = is_prime[1] = false;
	for(int i=2;i<n+1;i++){
		if(is_prime[i]==true){
			pri[p++] = i;
			for(int j=i+i;j<n+1;j+=i){
				is_prime[j] = false;
			}
		}
	}
	return p;
}

int main()
{
	int max_p = prime(1000000);
	int n;
	while(cin >> n){
		if(!n)
			break;
		int p,m;
		int money = 0;
		for(int i=0;i<n;i++){
			cin >> p >> m;
			int sum = 0;
			for(int j=0;j<max_p;j++){
				if(p-m <= pri[j] && pri[j] <= p+m)
					sum++;
			}
			money += sum;
		}
		money -= n;
		cout << money << endl;
	}	
	return 0;
}