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 <bits/stdc++.h>
using namespace std;

const int MAXP = 1000002;
int isP[MAXP], S[MAXP];

int main() {
  fill(isP, isP+MAXP, 1);
  isP[0] = isP[1] = 0;
  for(int i = 2; i < MAXP; ++i) {
    if(!isP[i]) continue;
    for(int j = i*2; j < MAXP; j += i) isP[j] = 0;
  }
  for(int i = 0; i+1 < MAXP; ++i) S[i+1] = S[i] + isP[i];

  int n;
  while(cin >> n && n) {
    int res = 0;
    for(int i = 0; i < n; ++i) {
      int p, m;
      cin >> p >> m;
      int X = S[min(MAXP-1,p+m+1)] - S[max(0,p-m)];
      if(X) {
        res += X-1;
      } else {
        res -= 1;
      }
    }
    cout << res << endl;
  }
  return 0;
}

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

using namespace std;

int N;
int BIT[6000000];

void add(int a, int w){
	
	// a?????????????´????w????¶???? O(log N)
	
	for(int x = a; x <= N; x += x & -x){
		BIT[x] += w;
	}
}

int sum(int a){
	
	// 1??????a?????§???????´????????¨?????????? O(log N)
	
	int ret = 0;
	for(int x = a; x > 0; x -= x & -x){
		ret += BIT[x];
	}
	return ret;
}

int main(){
	
	N = 2000000;
	int num_max = 1000000;
	
	bool a[1000000];
	
	for(int i = 0; i < num_max; i++){
		a[i] = true;
	}
	
	a[0] = false;
	a[1] = false;
	
	for(int i = 2; i < num_max; i++){
		if(a[i]){
			for(int j = i * 2; j < num_max; j += i){
				a[j] = false;
			}
		}
	}
	
	for(int i = 0; i < 6000000; i++){
		BIT[i] = 0;
	}
	
	for(int i = 0; i < num_max; i++){
		if(a[i]){
			add(i, 1);
		}
	}
	
	while(true){
		
		int n;
		
		cin >> n;
		
		if(n == 0){
			break;
		}
		
		int ans = 0;
		
		for(int i = 0; i < n; i++){
			int p, m, hit_num;
			cin >> p >> m;
			if(p - m <= 2){
				hit_num = sum(p + m);
			}else{
				hit_num = sum(p + m) - sum(p - m - 1);
			}
			
			//cout << hit_num << endl;
			
			ans = ans + hit_num - 1;
			
		}
		
		if(ans < 0){
			ans = 0;
		}
		
		cout << ans << endl;
		
	}
	
	return 0;
}

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

bool li[1000000];

void calpri();
int calx(int p, int m);
int money(int x);

int main(){
	int n, p[1000000], m[1000000], x[1000000], summoney = 0;
	while(1){
	cin >>n;										//IÊÌüÍ
	if(n == 0) break;								//nª0Åbreak
	for(int i = 0; i < n; i++){						//nÂÌIÊÌîñ
		cin >>p[i] >>m[i];
		x[i] = calx(p[i], m[i]);
		summoney += money(x[i]);
		}
		cout <<summoney <<endl;
	}
	
	return 0;
}

void calpri(){										//GgXelXÌâ¿Éæèboolì
	int max = 1000000, min = 2;						//iªf=true fÅÍÈ¢=false
	li[0] = false;
	li[1] = false;
	
	for(int i = 2; i <= max; i++){
		li[i] = 1;
	}
	
	for(int i = 2; i <= max; ){
		for(int j = (i + 1); j <= max; j++){
			if(j % i == 0){
				li[j] = false;
			}
		}
		do{
			i++;
		}while(!li[i]);
	}
}


int calx(int p, int m){								//zñliÌtrueÌ©çfJEg
	int nop = 0, max, min;
	max = p + m;
	min = p - m;
	for(int i = min; i <= max; ){
		if(li[i]){
			nop++;
		}
		do{
			i++;
		}while(!li[i]);
	}
	return nop;
}

int money(int x){							//ÜàvZ
	if(x == 0) return 0;
	else return(x - 1);
}

C++: using namespace std;
int N=1000000,sqrtN=1000;
bool isprime[1000001];

void sieve_of_atkin() {
  int n;
  for (int z = 1; z <= 5; z += 4) {
    for (int y = z; y <= sqrtN; y += 6) {
      for (int x = 1; x <= sqrtN && (n = 4*x*x+y*y) <= N; ++x)
        isprime[n] = !isprime[n];
      for (int x = y+1; x <= sqrtN && (n = 3*x*x-y*y) <= N; x += 2)
        isprime[n] = !isprime[n];
    }
  }
  for (int z = 2; z <= 4; z += 2) {
    for (int y = z; y <= sqrtN; y += 6) {
      for (int x = 1; x <= sqrtN && (n = 3*x*x+y*y) <= N; x += 2)
        isprime[n] = !isprime[n];
      for (int x = y+1; x <= sqrtN && (n = 3*x*x-y*y) <= N; x += 2)
        isprime[n] = !isprime[n];
    }
  }
  for (int y = 3; y <= sqrtN; y += 6) {
    for (int z = 1; z <= 2; ++z) {
      for (int x = z; x <= sqrtN && (n = 4*x*x+y*y) <= N; x += 3)
        isprime[n] = !isprime[n];
    }
  }
  for (int n = 5; n <= sqrtN; ++n)
    if (isprime[n])
      for (int k = n*n; k <= N; k+=n*n)
        isprime[k] = false;
  isprime[2] = isprime[3] = true;
}

int main(){
	sieve_of_atkin();
	int n,p,m,c,S,mn,mx;
	while(scanf("%d",&n),n){
		S=0;
		for(int j=0;j<n;j++){
			scanf("%d%d",&p,&m); mn=p-m; if(mn<2)mn=2; mx=p+m; if(mx>N-1)mx=N-1;
			c=0; for(int i=mn;i<=mx;i++)if(isprime[i])c++;
			if(c>0)c--; else c=-1;
			S+=c;
		}
		printf("%d\n",S);
	}
	return 0;
}

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

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

using namespace std;

int prime[10000001];
int num[1000001];

void CreatePrime()
{
	rep(i,1000001) prime[i] = 1;

	prime[0] = prime[1] = 0;

	REP(i,2,sqrt(1000001)+1)
	{
		for(int j=0;i*(j+2)<1000001;j++)
		{
			prime[i*(j+2)] = 0;
		}
	}
	
	memset(num,0,sizeof(num));
	int cnt = 1;
	REP(i,1,1000001)
	{
		if(prime[i]) 
		{
			num[i] = cnt;	
			cnt++;
		}
		else
		{
			num[i] = num[i-1];
		}
	}
}

int main()
{
	CreatePrime();
	
	int n;
	while(cin >> n && n)
	{
		int ans = -n;

		rep(i,n)
		{
			int p,m;
			cin >> p >> m;

			int s = p-m;
			int e = p+m;

			if(s < 0) s = 0;
			if(e > 1000000) e = 1000000;
			
			int count = num[e] - num[s];
			if(prime[s]) count++;

			if(count < 0) ans--;
			else ans += count;
		}
		
		if(ans < 0) cout << 0 << endl;
		else cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n){
    if(n <= 1) return vector<int>();
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i <= n; ++i) {
        if(is_prime[i]) prime.emplace_back(i);
        for (auto &&j : prime){
            if(i*j > n) break;
            is_prime[i*j] = false;
            if(i % j == 0) break;
        }
    }
    return prime;
}

auto v = get_prime(1000000);
int main() {
    int n;
    while(cin >> n, n){
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            int p, m;
            cin >> p >> m;
            ans += upper_bound(v.begin(),v.end(), p+m) - lower_bound(v.begin(),v.end(), p-m)-1;
        }
        cout << ans << "\n";
    }
    return 0;
}