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 <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 pr[N];

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

int main() {
  prime();
  int n;
  while(cin >>n && n){
    //map<int, int> m;
    int ans = 0;
    REP(i, n){
      int p, m; cin >>p >>m;
      //m[p] += (pr[min(N - 1, p + m)] - pr[max(0, p - m - 1)]);
      int tmp = (pr[min(N - 1, p + m)] - pr[max(0, p - m - 1)]);
      if(tmp == 0) --ans;
      else ans += (tmp - 1);
    }
    cout <<ans <<endl;
    //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++: using namespace std;
int M=1000001;
bool P[1000001];
int main(){
	for(int i=2;i<M;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;
	while(cin>>n,n){
		S=0;
		for(int j=0;j<n;j++){
			cin>>p>>m;
			c=0; for(int i=p-m>1?p-m:2;i<=p+m&&i<M;i++)if(!P[i])c++;
			S+=c>0?c-1:-1;
		}
		cout<<S<<endl;
	}
	return 0;
}