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;
bool p[1000000];
int prime[80000];
int top;
int F(int p,int m){
	int left,right,midl,midr;
	left=0,right=top;
	while(true){
		if(left>=right-1)break;
		midl=(left+right)/2;
		if(prime[midl]<p-m)left=midl;
		else right=midl;
	}
	if(prime[midl]>=p-m)midl--;
	if(p-m<=2)midl=-1;
	left=0,right=top;
	while(true){
		if(left>=right-1)break;
		midr=(left+right)/2;
		if(prime[midr]==p+m)break;
		if(prime[midr]<p+m)left=midr;
		else right=midr;
	}
	if(prime[midr]>p+m)midr--;
	//cout<<"%"<<prime[midl]<<" "<<prime[midr]<<endl;
	return midr-midl;
}
int main(){
	for(int i=0;i<1000000;i++)p[i]=true;
	for(int i=2;i<1000;i++)if(p[i])for(int j=i*i;j<1000000;j+=i)p[j]=false;
	top=0;
	for(int i=2;i<1000000;i++)if(p[i]){prime[top]=i;top++;}
	int n,p,m,ans;
	while(true){
	cin>>n;
	if(n==0)break;
	ans=0;
	for(int u=0;u<n;u++){
		cin>>p>>m;
		ans+=F(p,m)-1;
		//cout<<"#"<<F(p,m)<<endl;
	}
	cout<<ans<<endl;
	}
	return 0;
}

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

using namespace std;

int const MP = 999983;

bool is_prime[MP+1];
int sum_prime[MP+1];

void Sieve() {
  
  fill(is_prime, is_prime+MP+1, true);
  is_prime[0] = is_prime[1] = false;
  
  for(int i=2; (long long)i*i <= MP; i++) {
    if(is_prime[i]) {
      for(long long j=i*i; j<=MP; j+=i) {
	is_prime[j] = false;
      }
    }
  }
  
  for(int i=0; i<MP+1; i++) {
    sum_prime[i+1] += sum_prime[i] + is_prime[i+1];
  }
  
}

int main() {
  
  Sieve();
  
  int N;
  while(cin >> N && N) {
    int X = 0;
    for(int i=0; i<N; i++) {
      int p, m; cin >> p >> m;
      int L = max(p-m, 1), R = min(p+m, MP);
      int cnt = sum_prime[R]-sum_prime[L-1] - 1;
      X += cnt;
    }
    cout << X << endl;
  }
  
  return 0;
}

C++: using namespace std;
int N=1000000,sqrtN=1000;
bool isprime[1000001];
int nprime[10000021];
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();
	for(int i=1;i<N+1;i++)nprime[i]=nprime[i-1]+isprime[i-1];
	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=nprime[mx+1]-nprime[mn];
			if(c>0)c--; else c=-1;
			S+=c;
		}
		printf("%d\n",S);
	}
	return 0;
}

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

using namespace std;

const int MaxLimit = 1000001;
bool PrimeNumber[MaxLimit] = { false,false, true,true,false };
int Total[MaxLimit] = { 0,0,1,2,2 };

void findPrimeNumber() {
	int limit;
	bool flag;

	for (int i = 5; i < MaxLimit; i++) {
		if (i % 2 == 0) {
			PrimeNumber[i] = false;
			Total[i] = Total[i - 1];
			continue;
		}

		limit = sqrt(i) + 1;
		for (int j = 2; j < limit; j++) {
			if (i%j == 0) {
				flag = false;
				PrimeNumber[i] = false;
				Total[i] = Total[i - 1];
				break;
			}
			flag = true;
		}
		if (flag) {
			PrimeNumber[i] = true;
			Total[i] = Total[i - 1] + 1;
		}
	}
}

int main() {
	int n, p, m, minLimt, maxLimt, cnt, sum;

	findPrimeNumber();
	while (cin >> n, n) {
		sum = 0;
		for (int i = 0; i < n; i++) {
			cnt = 0;
			cin >> p >> m;
			minLimt = max(1, p - m);
			maxLimt = min(MaxLimit-1, p + m);
			cnt = Total[maxLimt] - Total[minLimt-1];
			//cout << p << ":" << m << " = " << cnt << endl;
			if (cnt == 0) {
				--sum;
			}
			else {
				sum += cnt - 1;
			}
		}
		cout << sum << endl;
	}

	return 0;
}

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

int prime[100000],size;
bool flg[1000008];

int isPrime(int x){
	for(int i=0;i<size && prime[i]<=sqrt(x);i++){
		if(x % prime[i] == 0) return false;
	}
	return true;
}

void mkPrime(void){
	prime[0] = 2;
	size = 1;

	flg[0] = flg[1] = false;
	flg[2] = true;

	for(int i=3;i<1000008;i++){
		if(i>999983){
			flg[i] = false;
		}
		else if((flg[i] = isPrime(i)) == true){
			prime[size++] = i;
		}
	}
}

int main(void){
	mkPrime();

	int n;

	while(cin>>n && n){
		int rst = 0;
		while(n--){
			int p,m;
			int count = 0;

			cin>>p>>m;

			int from = p - m;
			int to = p + m;

			if(from<0) from = 0;
			if(from>999983) from = 999984;
			if(to<0) to = 0;
			if(to>999983) to = 999984;

			for(int i=from;i<=to;i++){
				if(flg[i]) count++;
			}

			if(count == 0){
				rst--;
			}
			else{
				count--;
				rst += count;
			}
		}
		cout<<rst<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string.h>
using namespace std;
#define LIM 1000000

bool is_prime[LIM+10];
int sum[LIM + 10];

int main(){
	memset(is_prime, true, sizeof(is_prime));
	is_prime[0] = is_prime[1] = false;

	for(int p=2;p*p<=LIM;p++)if(is_prime[p]){
		for(int k=p*p;k<=LIM;k+=p)is_prime[k] = false;
	}

	sum[0] = 0;
	for(int i=0;i<=LIM;i++){
		sum[i+1] = sum[i] + is_prime[i];
	}


	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(LIM, p + m);

			pay += sum[high + 1] - sum[low] - 1;
		}

		cout << pay << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
const int N = 1000000;
const int RATE = 9973;

int p[N];

void prime(){
  REP(i, N) p[i] = 1;
  p[0] = 0; p[1] = 0;
  for(int i = 2; i < N; ++i){
    if(!p[i]) continue;
    for(int j = i + i; j < N; j += i) p[j] = 0;
  }
  FOR(i, 1, N) p[i] += p[i - 1];
}

int main() {
  prime();
  int n;
  while(cin >>n && n){
    vector<P> v(n);
    map<int, int> m;
    REP(i, n){
      cin >>v[i].first >>v[i].second;
      m[v[i].first] = p[min(N - 1, v[i].first + v[i].second)] - p[max(0, v[i].first - v[i].second - 1)];
    }
    int ans = 0;
    for(map<int, int>::iterator it = m.begin(); it != m.end(); ++it){
      if((*it).second == 0) --ans;
      else ans += ((*it).second - 1);
    }
    cout <<max(0, ans) <<endl;
  }
  return 0;
}