Generate the prime sequence with primes labeled 1 mod 4 or 3 mod 4. Add count of primes to sequence if just 4 primes occur before interruption by a prime congruent to 1 mod 4.

EXAMPLE

a(5)=111 because 111 sets of 4 primes occur below 10^5, each run interrupted by a prime congruent to 1 mod 4.

MATHEMATICA

p1 = p3 = 0; p = 15; s = Mod[{2, 3, 5, 7, 11, 13}, 4]; Do[ While[p < 10^n, If[s == {3, 1, 1, 1, 1, 3}, p1++]; If[s == {1, 3, 3, 3, 3, 1}, p3++]; p = NextPrime@ p; s = Join[ Take[s, -5], {Mod[p, 4]}]]; Print[{p1, p3}], {n, 2, 9}] (* Robert G. Wilson v, Sep 30 2011 *)