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

#define N 1000000

int main(void){
	vector<bool> isprime(N+1);
	for(int i=0;i<isprime.size();i++) isprime[i]=true;
	int upto = (int)sqrt(isprime.size());
	isprime[0]=isprime[1]=false;
	for(int i=2;i<=upto;++i){
		if(isprime[i]){//消されてなければその倍数を消す
			for(int j=2;i*j<isprime.size();++j) isprime[i*j] = false;
		}
	}
	vector<int> s(N+1,0);
	for(int i=0;i<N;++i) s[i+1]+=s[i]+((isprime[i+1])?1:0);
	int n;
	while(cin>>n,n){
		int ans=0;
		for(int i=0,p,m,x=0;i<n;++i){
			cin>>p>>m;
			x=s[(p+m>N)?N:p+m]-s[(p-m<0)?0:p-m];
			ans+=(x)?x-1:-1;
		}
		cout<<ans<<endl;
	}
	return 0;
}

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<cstdio>
#include<bitset>
#include<algorithm>
using namespace std;
int main()
{
	const int N = 1000000;
	bitset<N> isPrime;
	static int lpCount[N] = {0};
	int i,j;
	isPrime.set();
	isPrime[0] = isPrime[1] = false;
	for(i = 2; i < N; ++i)
	{
		if(isPrime[i])
		{
			for(j = i*2; j<N; j+=i)
				isPrime[j] = false;
			lpCount[i] = lpCount[i-1]+1;
		}
		else
			lpCount[i] = lpCount[i-1];
	}

	int p,m,n,s,l,r;
	while(scanf("%d",&n),n)
	{
		s=0;
		while(n--)
		{
			scanf("%d%d",&p,&m);
			l = max(0,p-m);
			r = min(N-1,p+m);
			s += (lpCount[r]-lpCount[l]+isPrime[l])-1;
		}
		printf("%d\n",s);
	}
	
	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(to<0) to = 0;

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

const int MP = 999983;
int n;

bool is_prime(int num)
{
    if(num > MP) return false;
    if(num == 2) return true;
    if(num % 2 == 0) return false;

    bool ret = true;
    for(int i = 3; i*i <= num; i+=2) {
        if(num % i == 0) {
            ret = false;
            break;
        }
    }
    return ret;
}

int main()
{
    while(cin >> n && n) {
        int p,m,sum,x;
        sum = 0;
        for(int i = 0; i < n; i++) {
            cin >> p >> m;
            x = 0;
            for(int a = p - m; a <= p + m; a++) {
                if(a <= 1) continue;
                if(is_prime(a)) 
                    x++;
            }
            sum += x - 1;
        }
        cout << sum << endl;
    }
}

C++: #include<iostream>
#include <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 1000000
#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(;pair_num--;){
			int p,m;
			int x;
			cin>>p>>m;
			if(p+m>MP){
				x=prime_num[MP]-prime_num[p-m];
			}else{
				x=prime_num[p+m]-prime_num[p-m];
			}
			if(x<0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
		}
		cout<<ans<<endl;
	}*/
	return 0;
}

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<MP;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}
	
	
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		for(;pair_num--;){
			int p,m;
			cin>>p;
			cin>>m;
			int x=0;
			if(p+m>MP){
				x=prime_num[MP]-prime_num[p-m];
			}else if(p-m<0){
				x=prime_num[p+m]-prime_num[0];
			}else{
				x=prime_num[p+m]-prime_num[p-m];
			}
			
			if(x==0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
			
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

const int mx = 1e6+3;
bool tab[mx+100];
int  amt[mx+100];

int main() {
	for(int i = 0; i < mx+100; i++)
		tab[i] = true;
	tab[0] = tab[1] = false;
	for(int i = 2; i*i < mx+100; i++) {
		if(tab[i]) {
			for(int j = i*2; j < mx+100; j += i) {
				tab[j] = false;
			}
		}
	}

	for(int i = mx; i < mx+100; i++)
		tab[i] = false;

	for(int i = 1; i < mx+100; i++) {
		amt[i] = amt[i-1];
		if(tab[i]) amt[i]++;
	}

	int n;
	while(cin >> n, n) {
		int res = 0;
		for(int i = 0; i < n; i++) {
			int p, m;
			cin >> p >> m;
			res += (amt[min(mx-1,p+m)] - amt[max(0,p-m-1)]) - 1;
		}
		cout << res << endl;
	}
}

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

bool prime[1000001];
int dp[1000001];

void seive(int s){
	for(int i=0;i<=s;i++)prime[i]=true;
	prime[0]=prime[1]=false;
	for(int i=2;i<=s;i++){
		if(prime[i]){
			for(int j=i*2;j<=s;j+=i)prime[j]=false;
		}
	}
}

void pp(int s){
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=s;i++){
		dp[i]=dp[i-1]+prime[i];
	}
}

int main(void){
	seive(1000000);
	pp(1000000);
	int n;
	while(1){
		int ans=0;
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			int p,m;
			scanf("%d%d",&p,&m);
			int ff=max(p-m,0);
			int tt=min(p+m,1000000);
			ans+=dp[tt]-dp[ff]+prime[ff];
		}
		printf("%d\n",ans-n);
	}
    return 0;
}