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>

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 = 3;
				counter++;
			}
			b = p+m;
			if (b > 999983) {
				b = 999983;
			}
			for (int j = a; j <= b; j++) {
				if (j % 2 != 0 && furui[j] == 0) {
					counter++;
				}
			}
			counter--;
		}
		cout << counter << endl;
	}
	return 0;
}

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

bool nonprime[1000001] = {};
int accum[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;
        
        map<int, 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[p] += cnt;
        }

        int ans = 0;
        for(map<int,int>::iterator it = result.begin(); it != result.end(); it++){
            pair<int,int> p = *it;
            if(p.second == 0)   ans--;
            else                ans += p.second-1;
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

const int MAX = 1000000;

int main()
{
	vi prime(MAX+1, 1);
	prime[0] = prime[1] = 0;
	for(int i=2; i*i<=MAX; ++i){
		if(prime[i]){
			for(int j=i*2; j<=MAX; j+=i)
				prime[j] = 0;
		}
	}
	reep(i, 1, MAX+1)
		prime[i] += prime[i-1];
	
	int n;
	while(scanf("%d", &n), n){
		int sum = 0;
		rep(i, n){
			int p, m;
			scanf("%d%d", &p, &m);
			sum += prime[min(p+m, MAX)] - prime[max(p-m-1, 0)];
		}
		
		printf("%d\n", sum - n);
	}
	
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;


int prime[MAX_PRIME+1];
bool isPrime[MAX_PRIME+1];
int p;

void erats(){
	// primeツδ環スツトツづーツ渉可甘コツ可サ
	fill(isPrime,isPrime+MAX_PRIME,true);
	p = 0;
	isPrime[0] = isPrime[1] = false;

	for(int i = 2; i <= MAX_PRIME; i++){
		if(isPrime[i]){
			prime[p++] = i;
		}
		else
			continue;

		// ツづ督づゥツつ「ツづ可つゥツつッツづゥ
		for(int j = 2*i; j <= MAX_PRIME; j+=i){
			isPrime[j] = false;
		}
		cout << flush;
	}
}


int main(){

	// ツ素ツ青板づーツ仰づ淞づゥ
	erats();

	int n;
	while(cin>>n&&n!=0){
		int sum=0;
		for(int i = 0; i < n; i++){
			int pp,mm;
			cin>>pp>>mm;
			int cnt=0;

			int *lb = lower_bound(prime,prime+p,pp-mm);
			for(int *it = lb;; it++){
				if(*it<=pp+mm){
					cnt++;
				}
				else if((*it>pp+mm)||(*it>=999983))
					break;
			}
			//for(int j = 0; j < p; j++){
			//	if(prime[j]>=pp-mm&&prime[j]<=pp+mm){
			//		cnt++;
			//	}
			//	else if(prime[j]>pp+mm)
			//		break;
			//}
			sum+=max(0,cnt-1);
			if(cnt==0)
				sum--;
		}
		cout<<max(0,sum)<<endl;
	}

	return 0;
}

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

#define NMAX 1000000

int main() {
  int n, p, m, x, v;
  int a[NMAX+1];
  a[0] = a[1] = 0;
  for (int i=2; i<=NMAX; i++) {
    a[i] = 1;
  }
  for (int i=2; i*i<=NMAX; i++) {
    if (!a[i]) continue;
    for (int j=i+i; j<=NMAX; j+=i) {
      a[j] = 0;
    }
  }
  for (int i=1; i<=NMAX; i++) {
    a[i] += a[i-1];
  }

  while (1) {
    cin >> n;
    if (!n) break;
    v = 0;
    for (int i=0; i<n; i++) {
      cin >> p >> m;
      x = a[min(p+m,NMAX)] - a[max(0,p-m-1)];
      v += x-1;
    }
    cout << max(v,0) << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int p[1000001];
int main(){
for(int i=0;i<1000001;i++)p[i]=1;
p[0]=p[1]=0;
for(int i=2;i<1000001;i++)
if(p[i])
for(int j=i+i;j<1000001;j+=i)
p[j]=0;
for(int i=1;i<1000001;i++)p[i]+=p[i-1];
int a;while(scanf("%d",&a),a){
long long r=0;
while(a--){
int b,c;
scanf("%d%d",&b,&c);
r+=p[min(b+c,1000000)]-p[max(0,b-c-1)]-1;
}printf("%lld\n",r);}}