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 <algorithm>

using namespace std;

#define MP 999983

int main() {
	vector<int> data(MP+1, 0);
	data[0] = data[1] = 1;
	vector<int> data2;
	for (int i = 2; i*i <= MP; i++) {
		if (data[i] == 0) {
			for (int j = 2; i*j <= MP; j++) {
				data[i*j] = 1;
			}
		}
	}
	for (int i = 0; i <= MP; i++) {
		if (data[i] == 0) {
			data2.push_back(i);
		}
	}
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int p, m;
		int counter2 = 0;
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			int ma, mi;
			ma = min(p+m, MP);
			mi = max(p-m, 0);
			vector<int>::iterator ite = lower_bound(data2.begin(), data2.end(), mi);
			vector<int>::iterator ite2 = upper_bound(data2.begin(), data2.end(), ma);
			//cout << ite2-ite << endl;
			counter2 += ite2-ite-1;
		}
		cout << counter2 << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

void makePrimeNumber(int N, vector<bool>& prime)
{
    prime.assign(N+1, true);
    prime [0] = prime[1] = false;
    for(int i=2; i*i<=N; i++){
        if(prime[i]){
            for(int j=i; i*j<=N; j++){
                prime[i*j] = false;
            }
        }
    }
}

int main()
{
    const int MAX = 1000000;

    vector<bool> prime;
    makePrimeNumber(MAX, prime);

    vector<int> num(MAX+1, 0);
    for(int i=1; i<=MAX; ++i){
        num[i] = num[i-1];
        if(prime[i])
            ++ num[i];
    }

    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        int ret = -n;
        for(int i=0; i<n; ++i){
            int p, m;
            cin >> p >> m;
            int a = max(1, p-m);
            int b = min(MAX, p+m);
            ret += num[b] - num[a-1];
        }
        cout << ret << endl;
    }
}

C++: #include <iostream>
#include <stdio.h>

using namespace std;

#define M 1000000

int m[3] = { 2, 7, 61 }; 

long mod(long a, long d, long n){
	if(a==0 || n==0) return 0;
	if(d==0) return 1 % n;
	
	unsigned long currentMod = a % n;
	unsigned long currentValue = (d%2 == 1) ? currentMod : 1;
	
	for(d>>=1; d>0; d>>=1){
		currentMod = (currentMod * currentMod) % n;
		if(d%2 == 1){
			currentValue = (currentValue * currentMod) % n;
		}
	}
	
	return currentValue;
}

bool isPrime(long x){
	if(x==2){
		return true;
	}
	else if(x%2 == 0){
		return false;
	}
	else{
		long s = 0;
		long d = x - 1;
		while(d%2 == 0){
			d = d / 2;
			s++;
		}
		
		for(int i=0; i<3; i++){
			long a = m[i];
			if(a%x == 0) continue;
			
			long pow = mod(a, d, x);
			if(pow == 1) continue;
			if(pow == x - 1) continue;
			
			bool con2 = false;
			for(int r=1; r<s; r++){
				pow = mod(pow, 2, x);
				if(pow ==  x - 1){
					con2 = true;
					break;
				}
			}
			
			if(!con2){
				return false;
			}
		}
		
		return true;
	}
}

int main(int argc, char** argv)
{
	char str[128];
	int* prime;
	int i, c, n, pay, size, cen, ran;

	prime = new int[M];

	while(true){
		cin >> str;
		size = atoi(str);
		if(size == 0) break;

		pay = 0;
		for(n=0; n<size; n++){
			cin >> str;
			cen = atoi(str);
			cin >> str;
			ran = atoi(str);
			c = 0;
			for(i=max(2, cen-ran); i<=min(cen+ran, M); i++){
				if(prime[i] != 0 && prime[i] != 1){
					if(isPrime(i)){
						prime[i] = 1;
						c++;
					}
					else{
						prime[i] = 0;
					}
				}
				else{
					if(prime[i]==1){
						c++;
					}
				}
			}

			pay += c - 1;
		}

		cout << max(0, pay) << endl;
	}
}

C++: #include "bits/stdc++.h"
using namespace std;

vector<int>primes(1e8);
vector<int>sums(1e8 + 1);
void hurui(const int amax) {
	static bool flag = false;
	if (flag)return;
	vector<int>sos;
	sos = vector<int>(amax + 1, true);
	sos[0] = false; sos[1] = false;
	for (int i = 2; i <= amax; ++i) {
		if (sos[i]) {
			for (int j = 2 * i; j <= amax; j += i)sos[j] = false;
		}
	}
	for (int i = 0; i <= amax; ++i) {
		if (sos[i]) {
			primes[i] = true;
		}
	}
	flag = true;
}


int main() {
	hurui(999983);
	for (int i = 0; i < 1e7; ++i) {
		sums[i + 1] = sums[i] + primes[i + 1];
	}
	while (1) {
		int N; cin >> N;
		if (!N)break;
		long long int prize = 0;
		while (N--) {
			long long int p, m; cin >> p >> m;
			int amax = min(999983ll, p + m);
			int amin = max(0ll, p - m-1);
			const int aprize = sums[amax] - sums[amin];
			prize += aprize - 1;
		}
		cout << max(0,prize )<< endl;
	}
	return 0;
}