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<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;
        int ans = 0;

        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;

            if(cnt == 0)    ans--;
            else            ans += cnt-1;
        }
/*
        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 <stdio.h>
#include <stdlib.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>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pii;

int prime[1000000];
bool is_prime[1000001];

void sieve(int n){
	int p=0;
	for(int i=0;i<=n;i++) is_prime[i]=true;
	is_prime[0]=is_prime[1]=false;
	for(int i=2;i<=n;i++){
		if(is_prime[i]){
			prime[p++]=i;
			for(int j=2*i;j<=n;j+=i) is_prime[j]=false;
		}
	}
}

int main(){
	int n,p,m;
	sieve(1000000);
	while(1){
		int ans=0;
		cin>>n;
		if(n==0) break;
		rep(i,0,n){
			cin>>p>>m;
			int c=0;
			rer(j,max(0,p-m),min(p+m,1000000)){
				if(is_prime[j]) c++;
			}
			//o(c);
			if(c==0) ans--;
			else ans+=c-1;
		}
		o(ans);
	}
}

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

const int MAX = 1000000;
const int MP = 999983;
int n;
bool is_prime[(MAX + 1) * 2 + 1];
int prime_count[(MAX + 1) * 2 + 1];

void sieve()
{
    for(int i = 0; i <= (MAX + 1) * 2; i++) 
        is_prime[i] = false;

    is_prime[2] = true;
    for(int i = 3; i <= MP; i+=2) {
        is_prime[i] = true;
        for(int j = 3; j*j <= i; j+=2) {
            if(i % j == 0) {
                is_prime[i] = false;
                break;
            }
        }
    }
}

void calc_count()
{
    prime_count[0] = 0;
    for(int i = 1; i <= (MAX + 1) * 2; i++) {
        prime_count[i] = prime_count[i-1];
        if(is_prime[i])
            prime_count[i]++;
    }
}

int main()
{
    sieve();
    calc_count();
    while(cin >> n && n) {
        int p,m,sum,x;
        sum = 0;
        for(int i = 0; i < n; i++) {
            cin >> p >> m;
            int lb, ub;
            lb = (p - m - 1 >= 0) ? p - m - 1 : 0;
            ub = p + m;
            x = prime_count[ub] - prime_count[lb];
            sum += x - 1;
        }
        cout << sum << endl;
    }
}

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

#define P_MAX 999984
using namespace std;

vector<int> Prime;
bool isPrime[P_MAX];

void make() {
  fill(isPrime, isPrime+P_MAX, true);
  isPrime[0] = isPrime[1] = false;
  for(int i = 2; i < P_MAX; ++i) {
    if(isPrime[i]) {
      Prime.push_back(i);
      for(int j = i*2; j < P_MAX; j += i) isPrime[j] = false;
    }
  }
}

int n, p, m, sum;

int calcX() {
  int a = p - m;
  int b = p + m;
  vector<int>::iterator it1 = lower_bound(Prime.begin(), Prime.end(), a);
  vector<int>::iterator it2 = upper_bound(Prime.begin(), Prime.end(), b);
  return distance(it1, it2);
}

void add() {
  int X = calcX();
  if(X == 0) sum -= 1;
  else       sum += X - 1;
}

main() {
  make();
  while((cin >> n) && n) {
    sum = 0;
    for(int i = 0; i < n; ++i) {
      cin >> p >> m;
      add();
    }
    cout << sum << endl;
  }
}

C++: #include <iostream>
#include <vector>

using namespace std;

#define MP 999983

int main() {
	vector<int> data(MP+1, 0);
	data[0] = data[1] = 1;
	for (int i = 2; i*i <= MP; i++) {
		if (data[i] == 0) {
			for (int j = 2; i*j <= MP; j++) {
				data[i*j] = 1;
			}
		}
	}
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int p, m;
		int counter2 = 0;
		//vector<int> data2(MP+1, 0);
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			int ma, mi;
			ma = min(p+m, MP);
			mi = max(p-m, 0);
			int counter = 0;
			for (int j = mi; j <= ma; j++) {
				if (data[j] == 0) {
					counter++;
				}
			}
			counter2 += counter-1;
			//data2[m] += counter;
		}
		/*for (int i = 0; i <= MP; i++) {
			if (data2[i] > 0) {
				cout << data2[i] << endl;
				counter2 += data2[i]-1;
			}
		}*/
		cout << counter2 << endl;
	}
	return 0;
}