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)
	}
}

Python: n=1000000
p=[1]*(n+1)
p[0],p[1]=0,0
for i in range(2,int(n**0.5)+1):
    if p[i]:
        for j in range(i*i,n,i):
            p[j]=0
while 1:
    l=int(input())
    if l==0:break
    ans=-l
    while l:
        l-=1
        a,m=map(int,input().split())
        ans+=sum(p[max(1,a-m)-1:min(n,a+m)])
    print(ans)

Python: import bisect

LENGTH = 1000000


def eratosthenes(length):
    from math import sqrt

    def is_prime_number(num):
        limit = int(sqrt(num)) + 1

        for lp in range(3, limit, 2):
            if num % lp == 0:
                return False

        return True

    is_prime_number_list = [True] * (LENGTH + 1)
    is_prime_number_list[0] = False
    is_prime_number_list[1] = False
    is_prime_number_list[2] = True

    for index in range(4, length + 1, 2):
        is_prime_number_list[index] = False

    limit = int(sqrt(length)) + 1

    for lp in range(3, limit, 2):
        if is_prime_number(lp):

            for index in range(lp * 2, length + 1, lp):
                is_prime_number_list[index] = False

    return is_prime_number_list


is_prime_number_list = eratosthenes(LENGTH)
prime_number_list = [index for index, item in enumerate(is_prime_number_list) if item]

while True:
    input_count = int(input())

    if input_count == 0:
        break

    pay = 0

    for _ in range(input_count):
        p, m = [int(item) for item in input().split(" ")]

        lower = p - m if 0 < p - m else 0
        upper = p + m if p + m < LENGTH else LENGTH

        lower_index = bisect.bisect_left(prime_number_list, lower)
        upper_index = bisect.bisect_right(prime_number_list, upper)

        prize = upper_index - lower_index
        pay += prize - 1

    print(pay)

Python: from itertools import accumulate
MAX = 1000000
is_prime = [1] * MAX
is_prime[0] = is_prime[1] = 0
for i in range(2, int(MAX ** (1 / 2)) + 1):
  if is_prime[i]:
    for j in range(i * i, MAX, i):
      is_prime[j] = 0
acc_prime = list(accumulate(is_prime))

while True:
  n = int(input())
  if n == 0:
    break
  ans = 0
  for _ in range(n):
    p, m = map(int, input().split())
    ans += acc_prime[min(p + m, MAX - 1)] - acc_prime[max(0, p - m - 1)] - 1
  print(ans)

Python: M = 1000001

p = [1]*M
p[0] = p[1] = 0
for i in range(2, int(M**.5)+1):
    if p[i]:
        for j in range(i*i, M, i):
            p[j] = 0
cs = [0]*M
cur = 0
for i in range(M):
    if p[i]:
        cur += 1
    cs[i] = cur

while 1:
    N = int(input())
    if N == 0:
        break
    ans = 0
    for i in range(N):
        p, m = map(int, input().split())
        x = cs[min(p+m, M-1)] - cs[max(p-m-1, 0)]
        if x:
            ans += x-1
        else:
            ans -= 1
    print(ans)

Python: n=1000000
p=[1]*(n+1)
pr=[]
p[0],p[1]=0,0
for i in range(2,int(n**0.5)+1):
    if p[i]:        
        for j in range(i*i,n,i):
            p[j]=0
for i in range(2,n):
    if p[i]:
        pr.append(int(i))
while 1:
    l=int(input())
    if l==0:break
    ans=-l
    while l:
        l-=1
        a,m=map(int,input().split())
        j=0
        while j<len(pr) and pr[j]<=a+m:
            if(pr[j]>=a-m):ans+=1
            j+=1
    print(ans)

Python: # AOJ 0165 Lottery
# Python3 2018.6.23 bal4u

#define MAX  1000000
#define SQRT 1000     // sqrt(MAX)



MAX = 1000000
SQRT = 1000     # sqrt(MAX)
comp = [0]*(MAX+2)

def sieve():
	for i in range(3, SQRT, 2):
		if comp[i] == 0:
			for j in range(i*i, MAX, i): comp[j] = 1
sieve()
tbl = [0]*(MAX+2)
tbl[2] = k = 1
for i in range(3, MAX, 2):
	if comp[i] == 0: k += 1
	tbl[i+1] = tbl[i] = k

while 1:
	n = int(input())
	if n == 0: break
	ans = 0;
	for i in range(n):
		p, m = map(int, input().split())
		a, b = p-m, p+m
		if a < 2: a = 2
		if b > 1000000: b = 1000000
		ans += tbl[b]-tbl[a-1] - 1;
	if ans < 0: ans = 0
	print(ans)

Python: import bisect
n=1000000
p=[1]*(n+1)
p[0],p[1]=0,0
for i in range(2,int(n**0.5)+1):
    if p[i]:        
        for j in range(i*i,n+1,i):
            p[j]=0
p=[i for i in range(n+1) if p[i]==1]
while 1:
    n=int(input())
    ans=-n
    if n==0:break
    while n:
        n-=1
        a,m=map(int,input().split())
        ans+=bisect.bisect_left(p,a+m+1)-bisect.bisect_left(p,a-m)
    print(ans)

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define inf (1<<29)
#define MAX 1000004
using namespace std;

vector<int> prime;
bool isntprime[MAX+1]; 
void init()
{	
  isntprime[0] = isntprime[1] = true;
  int j;	
  for(int i=2;i<= MAX;i++)if(!isntprime[i])for(prime.push_back(i),j=2*i;j<=MAX;j+=i)isntprime[j] = true;
}

int main()
{
  init();
  int n;
  while(cin >> n,n)
    {
      int p,m;
      int ans = n/101;
      rep(i,n)
	{
	  cin >> p >> m;
	  int X = 0;
	  REP(j,max(0,p-m),min(999983,p+m)+1)
	    {
	      if(!isntprime[j])X++;
	    }
	  if(X == 0)ans--;
	  else      ans += X-1;
	}
      cout << ans << endl;
    }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
bool prime[1000001];
vector<int> ppp;

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

	for(int i=0;i<=1000000;i++){
		if(!prime[i]){
			ppp.push_back(i);
		}
	}
}

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);
			int mi = p<m ? 0 : p-m;
			int mx = p+m>1000000?1000000:p+m;
			x = 0;
			for(int i=0;i<ppp.size();i++){
				if(ppp[i]>mx)break;
				if(ppp[i]>=mi){
					ret++;
				}
			}
			if(x){
				ret += x-1;
			}else{
				ret--;
			}
		}
		printf("%d\n",ret<0?0:ret);
	}
}

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

const long N=1000000;

int main(){
	bool prime[N+1];
	
	for(long i=0; i<=N; ++i) prime[i]=true;
	prime[0]=prime[1]=false;
	for(long i=2; i<=N; ++i){
		if(prime[i]) for(long j=2; i*j<=N; ++j) prime[i*j]=false;
	}
	
	while(1){
		int n;
		scanf(" %d", &n);
		if(n==0) break;
		
		long ans=0;
		for(int i=0; i<n; ++i){
			long p, m;
			scanf(" %ld %ld", &p, &m);	
			
			long x=0;
			int y=0;
			if((p+m)%2==0) y=1;
			
			if(p-m<=2) ++x;
			
			for(long j=p-m+y; j<=p+m-y; j+=2){
				//printf("j=%ld\n", j);
				if(j<2) continue;
				if(j>N) break;
				
				if(prime[j]) x++;
			}
			
			ans+=x-1;
			//printf("%ld\n", ans);
		}	
		
		printf("%ld\n", ans);
	}
}