Skip to main content
2 of 2
-6 bytes thanks to Jonathan French
NofP
  • 794
  • 5
  • 8

R, 206 200 bytes

function(n){a=p=j=y=2
for(i in 2:n-1){while(sum(y)<4*a){x=as.double(rev(intToBits(j)))
y=c(y,x[cumsum(x)>0])
j=j+1}
b=1:a
y=y[-b]
z=outer(k<-b+a,p,'%%')
p=c(a<-k[!apply(z<1,1,sum)][1],p)}
sum(y[1:a])}

Try it online!

The algorithm tries also to "save" on space by iteratively removing bits as it cycles through the primes. I feel that the decimal to bit conversion could probably be shorter, but I could not figure out other alternatives.

Saved 6 bytes thanks to Jonathan French.

NofP
  • 794
  • 5
  • 8