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>
using namespace std;
bool a[1000001];
int main() {
	fill(a, a + 1000001, true);
	a[0] = false; a[1] = false;
	for (int i = 2; i <= 999983; i++) {
		if (a[i]) {
			int j = 2;
			while (i*j <= 1000000) {
				a[i*j] = false;
				j++;
			}
		}
	}
	int n;
	while (cin >> n&&n != 0) {
		long long sum = 0;
		for (int i = 0; i < n; i++) {
			int p, m, X = 0, k;
			cin >> p >> m;
			k = (p - m);
			if ((p - m) < 0) k = 0;
			for (int j = k; (j <= p + m && j <= 1000000); ) {
				if (a[j]) X++;
				if (j % 2 == 0 && j != 2) j+=2;
				else j++;
			}
			if (X == 0) sum -= 1;
			else sum += (X - 1);
		}
		if (sum < 0) cout << "0" << endl;
		else cout << sum << endl;
	}
}

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

const int MAX = 999984;
bool isPrime[MAX];

void createIsPrime() {
    fill_n(isPrime, MAX, true);
    isPrime[0] = false;
    isPrime[1] = false;
    for (int i = 4; i < MAX; i += 2) {
        isPrime[i] = false;
    }
    for (int i = 3; i * i <= MAX; i += 2) {
        if (isPrime[i]) {
            for (int j = i * 2; j < MAX; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

int main() {
    createIsPrime();
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        int bill = 0;
        while (n--) {
            int p, m;
            cin >> p >> m;
            int prime = 0, start = p - m;
            if ((p - m) % 2 == 0) {
                start++;
            }
            if (p - m <= 2) {
                bill++;
            }
            for (int i = max(start, 3), end = min(p + m, MAX - 1); i <= end; i += 2) {
                if (isPrime[i]) {
                    prime++;
                }
            }
            if (prime > 0) {
                bill += prime - 1;
            } else {
                bill--;
            }
        }
        cout << bill << endl;
    }
}

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

const int MP = 999983;
int sv[MP+1] = {}, ac[MP+1] = {};

int main() {
	ios::sync_with_stdio(false);
	sv[0] = sv[1] = 1;
	for (int i=2; i*i<=MP; ++i) {
		if ( ! sv[i]) {
			for (int j=i*i; j<=MP; j+=i) {
				sv[j] = 1;
			}
		}
	}
	for (int i=1; i<=MP; ++i) {
		ac[i] = ac[i-1] + 1 - sv[i];
	}
	int n, p, m;
	while (cin >> n, n) {
		int ret = 0, X;
		for (int i=0; i<n; ++i) {
			cin >> p >> m;
			ret += ac[min(MP, p+m)] - ac[max(0, p-m-1)] - 1;
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

#define M 1000000

int m[3] = { 2, 7, 61 }; 

long mod(long a, long d, long n){
	if(a==0 || n==0) return 0;
	if(d==0) return 1 % n;
	
	unsigned long currentMod = a % n;
	unsigned long currentValue = (d%2 == 1) ? currentMod : 1;
	
	for(d>>=1; d>0; d>>=1){
		currentMod = (currentMod * currentMod) % n;
		if(d%2 == 1){
			currentValue = (currentValue * currentMod) % n;
		}
	}
	
	return currentValue;
}

bool isPrime(long x){
	if(x==2){
		return true;
	}
	else if(x%2 == 0){
		return false;
	}
	else if(x > 3 && x%6 != 1 && x%6 != 5){
		return false;
	}
	else{
		long s = 0;
		long d = x - 1;
		while(d%2 == 0){
			d = d / 2;
			s++;
		}
		
		for(int i=0; i<3; i++){
			long a = m[i];
			if(a%x == 0) continue;
			
			long pow = mod(a, d, x);
			if(pow == 1) continue;
			if(pow == x - 1) continue;
			
			bool con2 = false;
			for(int r=1; r<s; r++){
				pow = mod(pow, 2, x);
				if(pow ==  x - 1){
					con2 = true;
					break;
				}
			}
			
			if(!con2){
				return false;
			}
		}
		
		return true;
	}
}

int main(int argc, char** argv)
{
	char str[128];
	int* prime;
	int i, c, n, pay, size, cen, ran;

	prime = new int[M];
	for(i=2; i<M; i++){
		if(isPrime(i)){
			prime[i] = 1;
		}
	}

	while(true){
		cin >> str;
		size = atoi(str);
		if(size == 0) break;

		pay = 0;
		for(n=0; n<size; n++){
			cin >> str;
			cen = atoi(str);
			cin >> str;
			ran = atoi(str);
			c = 0;
			for(i=max(2, cen-ran); i<=min(cen+ran, M); i++){
				if(prime[i] == 1){
					c++;
				}
			}

			pay += c - 1;
		}

		cout << max(0, pay) << endl;
	}

	delete prime;
}

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

using namespace std;

bool p[1000001];
int cnt[1000001];
int main()
{
	p[2] = true;
	
	for (int i = 3; i <= 1000000; i += 2) p[i] = true;
	
	for (int i = 3; i <= 1000000; i += 2){
		if (p[i]){
			for (int j = 3 * i; j <= 1000000; j += 2 * i)
				p[j] = false;
		}
	}
	
	for (int i = 1; i <= 1000000; i++){
		cnt[i] = cnt[i - 1] + p[i];
	}

	int n;
	
	while (scanf("%d", &n) && n){
		int ans, a, b;
		ans = 0;
		for (int i = 0; i < n; i++){
			scanf("%d %d", &a, &b);
			ans += cnt[min(1000000, a + b)] - cnt[max(0, a - b - 1)] - 1;
		}
		printf("%d\n", ans);
	}
	
	return (0);
}

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

#define MAX 1000000

using namespace std;

int eratos[MAX];
int primes[MAX];
int max_primes;

void MakePrimes(){
	int n = 0;
	eratos[0] = 1;
	eratos[1] = 1;
	eratos[2] = 0;
	for(int i = 2; i < MAX; ++i){
		if( eratos[i] == 0 ){
			for(int j = 2; i * j < MAX; ++j){
				eratos[i*j] = 1;
			}
			primes[n++] = i;
		}
	}
	primes[n++] = 1000003;
	max_primes = n;
}
int bin_search_prime(int n){
	int r = max_primes - 1, l = 0, m;

	while( r > l + 1 ){
		m = ( r + l ) / 2; 
		if( primes[m] > n )
			r = m;
		else if( primes[m] < n )
			l = m;
		else
			return m;
	}
	return l;
}

int main()
{
	MakePrimes();
	while(true){
		int n;
		int ans = 0;
		
		scanf("%d", &n);
		if( n == 0 ) break;

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

			int l = bin_search_prime( std::max(0,p - m) );
			int r = bin_search_prime( std::min(MAX-1,p + m) );
			
			if( primes[l] < p-m ) ++l;
			X = (r - l) + 1;

			ans += X - 1;
		}

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