A naturally occurring prime generator
Pyth, 14 13 bytes
meaYhP+sY-5dQ
Uses a(n) = Lpf(6 - n + sum(a(i) for i in range(1, n))
where Lpf
means least prime factor.
Try it here online.
Python 3.5.0b1+, 95 93 bytes
Link to Python 3.5.0b1+ release
import math
def f(k,n=2,a=7,L=[]):x=math.gcd(n,a);return k and f(k-1%x,n+1,a+x,L+1%x*[x])or L
A direct implementation of the recurrence, featuring:
- Our good friend
1%x
, and math.gcd
, as opposed tofractions.gcd
.
Julia, 110 bytes
n->(a(n)=(n≥1&&(n==1?7:a(n-1)+gcd(n,a(n-1))));i=2;j=0;while j<n x=a(i)-a(i-1);x>1&&(j+=1;println(x));i+=1end)
Ungolfed:
function a(n::Int)
n ≥ 1 && (n == 1 ? 7 : a(n-1) + gcd(n, a(n-1)))
end
function f(n::Int)
i = 2;
j = 0;
while j < n
x = a(i) - a(i-1)
if x > 1
j += 1
println(x)
end
i += 1
end
end