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;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

vector<int> prime;
vector<bool> used;

int main() {
	used.resize(1000001, false);
	used[0] = used[1] = true;
	prime.push_back(-INF);
	for (int i = 2; i * i <= 1000001; i++) {
		if (!used[i]) {
			prime.push_back(i);
			for (int j = i; j <= 1000001; j += i) used[j] = true;
		}
	}
	REP(i, 1000001) if (!used[i]) prime.push_back(i);
	
	int n;
	while (cin >> n, n) {
		long long ans = 0;
		REP(i, n) {
			int p, m;
			scanf("%d %d", &p, &m);
			vector<int>::iterator it1 = upper_bound(prime.begin(), prime.end(), p + m) - 1;
			vector<int>::iterator it2 = upper_bound(prime.begin(), prime.end(), p - m) - 1;
			int cnt;
			
			if (*it1 < p - m) {
				if (*(it1 + 1) > p + m) it1 = prime.end();
				else it1++;
			}
			if (*it2 < p - m) {
				if (*(it2 + 1) > p + m) it2 = prime.end();
				else it2++;
			}
			
			if (it1 == prime.end() || it2 == prime.end())
				cnt = 2 - (it1 == prime.end()) - (it2 == prime.end()); 
			else
				cnt = it1 - it2 + 1;
			
			if (cnt == 0) ans--;
			else ans += cnt - 1;
		}
		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;
			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];
			
			if(x==0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}
			
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: using namespace std;
int M=1000001;
bool P[1000001];
int main(){
	for(int i=2;i<M;i++)if(!P[i])for(int j=i+i;j<M;j+=i)P[j]=1;
	P[0]=P[1]=1;
	int n,p,m,c,S;
	while(cin>>n,n){
		for(S=0;n--;){
			cin>>p>>m;
			c=0; for(int i=p-m>1?p-m:2;i<=p+m&&i<M;i++)if(!P[i])c++;
			S+=c>0?c-1:-1;
		}
		cout<<S<<endl;
	}
	return 0;
}

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

bool nonprime[1000001] = {};
int accum[1000001] = {};
int scoresheet[1000001];

int main(){
    nonprime[0] = nonprime[1] = true;
    for(int i = 2; i <= 1000000; i++){
        if(!nonprime[i]){
            for(int j = i+i; j <= 1000000; j+=i){
                nonprime[j] = true;
            }
        }
    }

    for(int i = 2; i <= 1000000; i++){
        if(nonprime[i-1]){
            accum[i] = accum[i-1];
        }else{
            accum[i] = accum[i-1] + 1;
        }
    }

    while(1){
        int n;
        cin >> n;
        if(n == 0)  break;

        for(int i = 0; i <= 1000000; i++)   scoresheet[i] = 0;

        set<int> result;
        
        for(int i = 0; i < n; i++){
            int p, m;
            cin >> p >> m;

            int cnt = accum[min(p+m, 1000000)] - accum[max(0, p-m)];
            if(p+m <= 1000000 && !nonprime[p+m])    cnt++;

            result.insert(p);
            scoresheet[p] += cnt;
        }

        int ans = 0;
        for(set<int>::iterator it = result.begin(); it != result.end(); it++){
            int x = scoresheet[*it];
            if(x == 0)   ans--;
            else         ans += x-1;
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
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 <= i; 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 = max(0, m - c); j <= min(1000000, m + c); j++) {
				sum += x[j];
			}
			sum -= 1;
		}
		cout << sum << endl;
	}
}

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

const int MAX = 1000000;
bool isPrime[MAX];

void createIsPrime() {
    fill_n(isPrime, MAX, true);
    isPrime[0] = false;
    isPrime[1] = false;
    for (int i = 4; i < MAX; i += 2) {
        isPrime[i] = false;
    }
    for (int i = 3; i * i <= MAX; i += 2) {
        if (isPrime[i]) {
            for (int j = i * 2; j < MAX; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

int main() {
    createIsPrime();
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        int bill = 0;
        while (n--) {
            int p, m;
            cin >> p >> m;
            int prime = 0;
            for (int i = max(p - m, 2); i <= min(p + m, MAX - 1); i++) {
                if (isPrime[i]) {
                    prime++;
                }
            }
            if (prime > 0) {
                bill += prime - 1;
            } else {
                bill--;
            }
        }
        cout << bill << endl;
    }
}

C++: // 2011/10/16 Tazoe

#include <iostream>
using namespace std;

void prime(bool prm[], int n)
{
	if(n>=1)
		prm[0] = false;
	if(n>=2)
		prm[1] = false;

	for(int i=2; i<n; i++)
		prm[i] = true;

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

int main()
{
	bool prm[1000001];
	prime(prm, 1000001);

	int prm2[1000001];
	int cnt = 0;
	for(int i=0; i<=1000000; i++){
		if(prm[i])
			cnt++;
		prm2[i] = cnt;
	}

	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int sum = 0;
		for(int i=0; i<n; i++){
			int p, m;
			cin >> p >> m;

			int bgn;
			if(p-m-1<0)
				bgn = 0;
			else
				bgn = p-m-1;

			int end;
			if(p+m>1000000)
				end = 1000000;
			else
				end = p+m;

			sum += (prm2[end]-prm2[bgn]);

			sum--;

//			cout << sum << endl;

		}

		cout << sum << endl;
	}

	return 0;
}

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

const int MP = 999983;
bool sieve[MP+1];
int ac[MP+1];

void makeSieve() {
    memset(sieve, false, sizeof sieve);
    sieve[0] = sieve[1] = true;
    for (int i=2; i*i<=MP; ++i) {
        if (sieve[i]) continue;
        for (int j=i*i; j<=MP; j+=i) {
            sieve[j] = true;
        }
    }
}

void acum() {
    ac[0] = 0;
    for (int i=1; i<=MP; ++i) {
        ac[i] = ac[i-1] + !sieve[i];
    }
}

int main() {
    makeSieve(); acum();
    int n, p, m;
    while (cin >> n, n) {
        int ret = 0;
        for (int i=0; i<n; ++i) {
            cin >> p >> m;
            int l = max(0, p-m);
            int h = min(MP, p+m);
            int X = ac[h] - (l-1 < 0 ? 0 : ac[l-1]);
            ret += X - 1;
        }
        cout << ret << endl;
    }
    return 0;
}