ClearAll[TwinPrimeCount] TwinPrimeCount[mx_] := Module[{pmax, min, max, total}, pmax = PrimePi[mx]; total = 0; Do[ min = 10^6 i; min = Max[min, 1]; max = 10^6 (i + 1); max = Min[max, pmax]; total += Count[Differences[Prime[Range[min, max]]], 2] , {i, 0, Ceiling[pmax/10^6]} ]; total ] Do[Print[{10^i, TwinPrimeCount[10^i]}], {i, 9}]