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 <algorithm>
#include<functional>//greaterの使用
using namespace std;
#define MAX_LIST 999983+100
#define MP 999983

int main(){
	bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	for(int i=MP+1;i<MAX_LIST;i++){
		prime[i]=0;
	}
	
	int prime_num[MAX_LIST];
	prime_num[0]=1;
	for(int i=1;i<MP;i++){
		prime_num[i]=prime_num[i-1]+prime[i];
	}
	
	
	int pair_num;
	while(cin>>pair_num,pair_num){
		int ans=0;
		
		for(;pair_num--;){
			int p,m;
			int x;
			cin>>p;
			cin>>m;
			/*
			if(p+m>MP){
				x=prime_num[MP]-prime_num[p-m];
			}else{
				x=prime_num[p+m]-prime_num[p-m];
			}
			if(x<0){
				ans--;
			}else{
				x--;
				ans=ans+x;
			}*/
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

int count[1000001];
int prime[80000],size;
int flg[1000001];

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

void mkPrime(void){
	prime[0] = 2;
	size = flg[2] = 1;
	flg[0] = flg[1] = 0;

	for(int i=3;i<=999983;i+=2){
		if(isPrime(i)){
			flg[i] = 1;
			prime[size++] = i;
		}
	}
}

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

int main(void){
	int n;

	mkPrime();

	while(cin>>n && n){
		memset(count,-1,sizeof(count));
		while(n--){
			int p,m;
			cin>>p>>m;
			if(count[p] == -1) count[p] = 0;
			count[p] += calX(p-m<0?0:p-m, p+m>1000000?1000000:p+m);
		}

		int ans = 0;
		for(int i=0;i<=1000000;i++){
			if(count[i] > 0) ans += count[i] - 1;
			else if(count[i] == 0) ans--;
		}

		cout<<ans<<endl;
	}

	return 0;
}

C++: using namespace std;
int M=1000001;
bool P[1000001];
int main(){
	for(int i=2;i<1002;i++)if(!P[i])for(int j=i+i;j<M;j+=i)P[j]=1;
	P[0]=P[1]=1;
	int n,p,m,c,S,mn,mx;
	while(cin>>n,n){
		S=0;
		for(int j=0;j<n;j++){
			cin>>p>>m; mn=p-m; if(mn<2)mn=2; mx=p+m; if(mx>M-1)mx=M-1;
			c=0; for(int i=mn;i<=mx;i++)if(!P[i])c++;
			if(c>0)c--; else c=-1;
			S+=c;
		}
		cout<<S<<endl;
	}
	return 0;
}

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

using namespace std;

int main () {
	vector<int> furui(1000001, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i < 1000001; i++) {
		if (furui[i] == 0) {
			for (int j = 2; j <= 1000000/i; j++) {
				furui[i*j] = 1;
			}
		}
	}
	int n;
	int a, b;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int p, m;
		int 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 <iostream>
#include <cstdio>
using namespace std;
bool prime[1000001];

void p_init(){
	prime[0] = prime[1] = true;
	for(int i=0;i<=1000;i++){
		if(!prime[i])
			for(int j=i<<1;j<=1000000;j+=i){
				prime[j] = true;
			}
	}
}

int main(){
	p_init();
	int x,n,p,m;
	int ret;
	while(cin >> n && n){
		ret = 0;
		for(int i=0;i<n;i++){
			scanf("%d %d",&p,&m);
			x = 0;
			for(int o=p<m ? 0 : p-m
			;o<=(p+m>1000000?1000000:p+m);
			o++){
				if(!prime[o]){
					x++;
				}
			}
			if(x){
				ret += x-1;
			}else{
				ret--;
			}
		}
		printf("%d\n",(ret<0?0:ret));
	}
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
const int N = 1000000;
const int RATE = 9973;

int p[N];

void prime(){
  REP(i, N) p[i] = 1;
  p[0] = 0; p[1] = 0;
  for(int i = 2; i < N; ++i){
    if(!p[i]) continue;
    for(int j = i + i; j < N; j += i) p[j] = 0;
  }
  FOR(i, 1, N) p[i] += p[i - 1];
}

int main() {
  prime();
  int n;
  while(cin >>n && n){
    vector<P> v(n);
    map<int, int> m;
    REP(i, n){
      cin >>v[i].first >>v[i].second;
      m[v[i].first] += p[min(N - 1, v[i].first + v[i].second)] - p[max(0, v[i].first - v[i].second - 1)];
    }
    int ans = 0;
    for(map<int, int>::iterator it = m.begin(); it != m.end(); ++it){
      if((*it).second == 0) --ans;
      else ans += ((*it).second - 1);
    }
    cout <<max(0, ans) <<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#define INF (long long)1000001
using namespace std;
int main(){
	int SOSU[INF];
	fill(SOSU,SOSU+INF,1);
	for(int i=2;i<INF;i++){
		if(SOSU[INF]==1){
			for(int j=i+i;j<INF;j+=i) SOSU[j] = 0;
		}
	}
	for(int i=2;i<INF;i++) SOSU[i] += SOSU[i-1];

	long long a,sum,b,c;
	while(cin >> a && a){
		sum = 0;
		while(a--){
			cin >> b >> c;
			sum += p[min(b+c,INF-1)]-p[max(0,b-c-1)]+1;
		}
		cout << sum << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30
#define MAX_P 1000000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;


int p[MAX_P], n;

int main(){
  p[0] = p[1] = 1;
  REP(i, 2, MAX_P/2){
    if(p[i]) continue;
    for(int j = i+i; j < MAX_P; j += i) p[j] = 1;
  }
  //  rep(i, 100) if(!p[i])printf("%d ", i); puts("");

  while(scanf("%d", &n) && n){
    int ip, m, res = 0;
    rep(i, n){
      scanf("%d%d", &ip, &m);
      int c = 0;
      REP(j, ip-m, ip+m+1) if(j > 0 && j < MAX_P && !p[j]) c++;
      if(c) res += c-1;
      else res--;
    }
    printf("%d\n", res);
  }
  return 0;
}

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

#define mx 1000001

using namespace std;

bool prm[mx];
int cnt[mx];

void mkPrm()
{
  fill(prm, prm+mx, true);
  prm[0] = prm[1] = false;

  for(int i = 2; i < mx; ++i)
    if(prm[i])
      for(int j = i+i; j < mx; j+=i) prm[j] = false;

  int t = 0;
  for(int i = 0; i < mx; ++i){
    if(prm[i]) ++t;
    cnt[i] = t;
  }

  return ;
}


int main()
{
  int n, s, p, m, c;
  mkPrm();

  while(cin>>n && n){
    s = 0;
    while(n--){
      cin >> p >> m;
      c = ((p+m >= mx)? cnt[mx-1]: cnt[p+m]);
      c -= (p-m < 0)? cnt[0] : cnt[p-m];
      s--;
      if(c > 0) s += c;
    }
    cout << s << endl;
  }

  return 0;
}