from collections import defaultdict
from time import clock

import primesieve

def count_last_digs():
    t0 = clock()
    counts = defaultdict(int)
    primes = primesieve.Iterator()
    primes.next_prime()
    primes.next_prime()
    primes.next_prime()
    p1 = primes.next_prime()
    p1d = p1 % 10
    limit = 10**6
    while True:
        p2 = primes.next_prime()
        p2d = p2 % 10
        counts[p1d, p2d] += 1
        if p2 > limit:
            print("{:_}".format(limit), clock() - t0)
            print_counts(counts)
            limit = 10*limit if limit <= 1000_000_000 else limit + 10_000_000_000
        p1d = p2d
    return counts

def print_counts(counts):
    S = sum(counts.values())
    C = {k: v / S for k, v in counts.items()}
    V = [1, 3, 7, 9]
    for v1 in V:
        for v2 in V:
            print("{:6.3f}".format(C[v1, v2]), end="  ")
        print()
    print()

count_last_digs()


"""
1_000_000
 0.041   0.080   0.083   0.045
 0.056   0.036   0.074   0.085
 0.065   0.069   0.037   0.079
 0.089   0.065   0.056   0.040

10_000_000
 0.043   0.078   0.080   0.049
 0.058   0.039   0.073   0.080
 0.064   0.069   0.039   0.078
 0.085   0.065   0.058   0.042

100_000_000
 0.044   0.076   0.078   0.052
 0.059   0.042   0.072   0.078
 0.064   0.068   0.042   0.076
 0.083   0.064   0.059   0.044

1_000_000_000
 0.046   0.075   0.076   0.054
 0.060   0.044   0.071   0.076
 0.064   0.068   0.044   0.075
 0.080   0.064   0.060   0.046

10_000_000_000
 0.047   0.074   0.074   0.055
 0.060   0.046   0.070   0.074
 0.064   0.067   0.046   0.074
 0.079   0.064   0.060   0.047

 
340_000_000_000 14630.772475548303
 0.049   0.072   0.072   0.057
 0.061   0.048   0.069   0.072
 0.063   0.067   0.048   0.072
 0.077   0.063   0.061   0.049

350_000_000_000 15045.708864189168
 0.049   0.072   0.072   0.057
 0.061   0.048   0.069   0.072
 0.063   0.067   0.048   0.072
 0.077   0.063   0.061   0.049

1_140_000_000_000 32903.831111384796
 0.049   0.072   0.072   0.057
 0.061   0.048   0.069   0.072
 0.063   0.066   0.048   0.072
 0.076   0.063   0.061   0.049

2_610_000_000_000 73682.21293810359
 0.049   0.072   0.072   0.057
 0.061   0.049   0.069   0.072
 0.063   0.066   0.049   0.072
 0.076   0.063   0.061   0.049

 3_130_000_000_000 88038.33787216355
 0.050   0.072   0.071   0.057
 0.061   0.049   0.069   0.071
 0.063   0.066   0.049   0.072
 0.076   0.063   0.061   0.050

 4_240_000_000_000 118131.35975553434
 0.050   0.072   0.071   0.057
 0.061   0.049   0.068   0.071
 0.063   0.066   0.049   0.072
 0.076   0.063   0.061   0.050

"""