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;

int prime[1000001];

int main()
{
	for (int i = 2; i <= 1000000; i++){
		prime[i] = 1;
	}
	for (int i = 2; i * i <= 1000000; i++){
		for (int j = i * i; j <= 1000000; j += i){
			prime[j] = 0;
		}
	}
	for (int i = 1; i <= 1000000; i++){
		prime[i] += prime[i - 1];
	}
	
	int n;
	while (scanf("%d", &n), n){
		int res = 0;
		for (int i = 0; i < n; i++){
			int p, m;
			scanf("%d %d", &p, &m);
			int x = prime[min(p + m, 1000000)] - prime[max(p - m - 1, 0)];
			res += x ? x - 1 : -1;
		}
		printf("%d\n", res);
	}
	
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;

bool primeTable[1000001];
int primeBIT[1000001];

int sumBIT(int i,int* bit){
	int s = 0;
	while(i > 0){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void addBIT(int i, int x,int* bit,int n){
	while(i <= n){
		bit[i] += x;
		i += i & -i;
	}	
}

int main(){
	fill((bool*)primeTable,(bool*)primeTable+1000001,true);
	fill((int*)primeBIT,(int*)primeBIT+1000001,0);

	primeTable[0] = false;
	primeTable[1] = false;

	for(int i=1;i*i<=1000000;i++){
		if(primeTable[i]){
			for(int j=i+i;j<=1000000;j+=i){
				primeTable[j] = false;
			}
		}
	}

	for(int i=1;i<=1000000;i++){
		addBIT(i,primeTable[i],primeBIT,1000000);
	}


	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		int sum=0;
		for(int i=0;i<n;i++){
			int p,m;
			scanf("%d %d",&p,&m);

			int tmp=0;
			tmp = sumBIT(p+m >=1000000 ? 1000000 : p+m,primeBIT) 
				- sumBIT(p-m-1 >= 0 ? p-m-1 : 0,primeBIT);

			tmp > 0 ? sum+=tmp-1 : sum--;
		}

		printf("%d\n",sum >= 0 ? sum : 0);
	}
}

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

int pos[1000001],p[100000],size;

bool isPrime(int x){
	for(int i=0;i<size && p[i]<=sqrt(x);i++){
		if(x % p[i] == 0) return false;
	}
	return true;
}

void mkPrime(void){
	memset(pos,-1,sizeof(pos));
	p[0] = 2;
	pos[0] = pos[1] = pos[2] = 0;
	size = 1;

	for(int i=3;i<=999983;i++){
		if(isPrime(i)){
			p[size] = i;
			for(int j=i;pos[j]==-1;j--) pos[j] = size;
			size++;
		}
	}

	for(int i=999984;i<=1000000;i++){
		pos[i] = size;
	}
}

int calX(int from,int to){
	int res = 0;
	for(int i=pos[from];i < size && p[i]<=to;i++,res++);
	return res;
}

int main(void){
	int n;

	mkPrime();

	while(scanf("%d",&n) && n){
		int ans = 0;
		while(n--){
			int p,m;
			scanf("%d%d",&p,&m);
			int count = calX(p-m<0?0:p-m, p+m>1000000?1000000:p+m);
			if(count > 0) ans += count - 1;
			else if(count == 0) ans--;
		}

		printf("%d\n",ans);
	}

	return 0;
}

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

const int PRIME_SIZE = 1000000;

int main()
{
	bool prime[ PRIME_SIZE ] = { 0 };
	prime[ 0 ] = prime[ 1 ] = true;
	for( int i = 2; i < PRIME_SIZE; ++i ){
		if( prime[ i ] ) continue;
		for( int j = i + i; j < PRIME_SIZE; j += i ){
			prime[ j ] = true;
		}
	}

	int n;
	while( cin >> n && n ){
		int pay = -n;
		int* hit = new int[ PRIME_SIZE ]();
		for( int i = 0; i < n; ++i ){
			int p, m;
			cin >> p >> m;
			int low = max( 0, p - m );
			int high = min( PRIME_SIZE - 1, p + m );
			int count = 0;
			for( int j = low; j <= high; ++j ){
				++hit[ j ];
			}
		}
		for( int i = 0; i < PRIME_SIZE; ++i ){
			if( !prime[ i ] ) pay += hit[ i ];
		}

		cout << ( pay < 0 ? 0 : pay ) << endl;

		delete[] hit;
	}
	return 0;
}

C++: #include <iostream>
#include <set>
using namespace std;
bool a[1000000];
int main()
{
  int i,j,k,n,x,s;
  set<int> se;
  set<int>::iterator it;
  for(i=2;i<1000000;i++){
    if(!a[i]){
      se.insert(i);
      for(j=2;i*j<1000000;j++){
	a[i*j] = true;
      }
    }
  }
  for(;;){
    cin >> n;
    if(n==0)break;
    s = 0;
    while(n--){
      cin >> i >> j;
      x = 0;
      it = se.lower_bound(i-j);
      while((*it) <= i+j && it != se.end()){
	x++;
	it++;
      }
      if(x > 0) s += x-1;
      else s--;
    }
    cout << s << endl;
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;


bool primeTable[1000001];

int main(){
	fill((bool*)primeTable,(bool*)primeTable+1000001,true);

	primeTable[0] = false;
	primeTable[1] = false;

	for(int i=1;i*i<=1000000;i++){
		if(primeTable[i]){
			for(int j=i+i;j<=1000000;j+=i){
				primeTable[j] = false;
			}
		}
	}


	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		int sum=0;
		for(int i=0;i<n;i++){
			int p,m;
			scanf("%d %d",&p,&m);

			int tmp=0;
			for(int j= p-m >= 0 ? p-m : 0;j<=p+m && j<=1000000;j++){
				if(primeTable[j]){
					tmp++;
				}
			}

			tmp > 0 ? sum+=tmp-1 : sum--;
		}

		printf("%d\n",sum >= 0 ? sum : 0);
	}
}

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

using namespace std;

int main () {
	vector<int> furui(999984, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i < 999984; i++) {
		if (furui[i] == 0) {
			for (int j = 2; j <= 999983/i; j++) {
				furui[i*j] = 1;
			}
		}
	}
	int n;
	int a, b, p, m;
	int counter;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		counter = 0;
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			a = p-m;
			if (a < 2) {
				a = 2;
			}
			b = p+m;
			if (b > 999983) {
				b = 999983;
			}
			for (int j = a; j <= b; j++) {
				if (furui[j] == 0) {
					counter++;
				}
			}
			counter--;
		}
		cout << counter << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

const int MAX = 1000003;
bool prime[MAX];

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

    for(int i=0; i < MAX; i++){
            if(!prime[i]) continue;
            for(int j=i+i; j < MAX; j += i){
                        prime[j] = false;
                    }
        }
}

int main(){
    sieve();
    int n;
    while(cin >> n, n){
        int ans = 0;
        for(int i=0; i< n; i++){
            int a, b;
            cin >> a >> b;
            for(int j = max(2, a-b); j <= min(a+b, 999983); j++){
                if(prime[j]) ans++;
            }
            ans--;
        }
        cout << ans << endl;
    }
}