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

	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(isPrime(i)){
					c++;
				}
			}

			pay += c - 1;
		}

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

	delete prime;
}

C++: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <queue>
#define MAX 1000001
using namespace std;

int main(void){
	int i,j,k,n,m,p,x,sum;
	bool a[MAX];

	for(i=0;i<MAX;i++) a[i] = true;
	for(i=2;i<MAX;i++){
		if(a[i]){
			for(j=i+i;j<MAX;j+=i) a[j] = false;
		}
	}

	while(1){
		cin >> n;
		if(n == 0) break;
		x = 0;
		for(i=0;i<n;i++){
			cin >> p >> m;
			for(j=p-m;j<=p+m;j++){
				if(j < 2 && p + m >= 2) j = 2;
				else if(j < 2) break;
				if(j >= MAX) break;
				if(a[j]) x++;
			}
			x--;
		}
		cout << x << endl;
	}
	return 0;
}

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

using namespace std;

int main()
{
	
	bool a[1000001];
	int pr[78499], p, n, m, t, q;
	
	fill(a, a+1000001, true);
	a[0] = a[1] = false;
	
	for(int i = 2; i <= 1000000; i++){
		if(a[i]){
			for(int j = 2*i; j <= 1000000; j+=i){
				a[j] = false;
			}
		}
	}
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		t = 0;
		while(n--){
			cin >> p >> m;
			for(int i = (p-m < 0)? 2: p-m, max = (m+p > 1000000)? 999983: m+p; i <= max; i++){
				if(a[i]){
					bool f = true;
					for(int j = 0; j < q; j++){
						if(pr[j] == i){
							f = false;
							break;
						}
					}
					if(f){
						pr[q++] = i;
						t++;
					}
				}
			}
			t--;
			if(t < 0){
				t = 0;
			}
			cout << t << endl;
		}
	}
	
	return 0;
}

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

using namespace std;

const int amax = 1000000;

bool prime(int v)
{
	if(v == 2){
		return true;
	}
	if(v < 2 || v%2 == 0){
		return false;
	}
	
	for(int i = 3; i*i <= v; i+=2){
		if(v%i == 0){
			return false;
		}
	}
	
	return true;
}

int main()
{
	int n, s, t, c;
	bool a[amax];
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		c = 0;
		fill(a, a+amax, true);
		while(n--){
			cin >> s >> t;
			for(int i = s-t; i <= s+t && i < amax; i++){
				if(prime(i) && a[i]){
					a[i] = false;
					c++;
				}
			}
		}
		
		c--;
		if(c < 0){
			c = 0;
		}
		cout << c << endl;
	}
	
	return 0;
}

C++: #include <iostream>
 
using namespace std;
 
#define MP 999983
#define MAX 1000000
int Prime[MAX+1];
bool prime[MAX+1];
 
void make()
{
    fill(prime, prime+MAX, true);
    prime[0] = prime[1] = false;
 
    for (int i = 2; i*i <= MAX; i++) {
	if (prime[i]) {
	    for (int j = i*i; j <= MAX; j += i) {
		prime[j] = false;
	    }
	}
    }
 
    int sum = 0;
    for (int i = 0; i <= MAX; i++) {
	if (prime[i]) {
	    sum++;
	}
	Prime[i] = sum;
    }
}
 
int main()
{
    int n;
 
    make();
    while (cin >> n, n) {
	int p, m, ans = 0;
	for (int i = 0; i < n; i++) {
	    cin >> p >> m;
 
	    int large = p + m;
	    int small = p - m;
 
	    if (large > MP) {
		large = MP;
	    }     
	    if (small < 0) {
		small = 0;
	    }
	    int X = Prime[large] - Prime[small] + prime[small];
	    if (X == 0) {
		ans--;
	    } else {
		ans += X-1;
	    }
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

#define MAX_P 999983
#define MAX 1000000
int Prime[MAX+1];
bool prime[MAX+1];

void make(){
  fill(prime,prime+MAX,true);
  prime[0] = prime[1] = false;

  for(int i = 2 ; i*i <= MAX ; i++){
    if(prime[i]){
      for(int j = i*i ; j <= MAX ; j += i){
	prime[j] = false;
      }
    }
  }

  int sum = 0;
  for(int i = 0 ; i <= MAX ; i++){
    if(prime[i]){
      sum++;
    }
    Prime[i] = sum;
  }
}

int main(){
  int n;

  make();

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

      int large = p + m;
      int small = p - m;

      if(large > MAX_P){
	large = MAX_P;
      }     
      if(!prime[large] && !prime[small] &&
	 Prime[large] == Prime[small]) continue;

      if(small < 0){
	small = 0;
      }
      ans += Prime[large] - Prime[small];
      if(Prime[large] || Prime[small]) ans--;
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second

int n;

bool is_prime[2000100];
int cnt[2000100];

int main(){
	fill(is_prime, is_prime + 1000000, 1);
	is_prime[0] = 0;
	is_prime[1] = 0;
	for (int i = 2; i <= (int)1e6; i++){
		if (is_prime[i]){
			for (int j = i * 2; j <= (int)1e6; j += i){
				is_prime[j] = 0;
			}
		}
	}
	FOR(i, 1, 2000000+10){
		if (is_prime[i])cnt[i]++;
		cnt[i] += cnt[i - 1];
	}
	//REP(i, 10){
	//	cout << i << " " << cnt[i] << endl;
	//}
	//for (int i = 999983 - 20; i <= 999983 + 20; i++){
	//	cout << i << " " << cnt[i] << endl;
	//}
	while (cin >> n&&n){
		int ans = 0;
		REP(i, n){
			int p, m;
			cin >> p >> m;
			int x = cnt[p + m];
			if (p - m - 1 >= 0)x -= cnt[p - m - 1];
			ans += (x - 1);
		}
		cout << ans << endl;
	}
}