New Order #3: 5 8 6
Jelly, 32 bytes
;®»ṀƊSÆn_S
ẎṀ©+LRḟẎḣL;Ç$ṭ
1Ç¡Fị@
Try it online! - very slow as it builds n rows first, for a faster version which doesn't, at 37 bytes, try this.
Perl 6, 80 77 bytes
{({$!=@_;+(1...{$_∉$!&&(|$!,$_).rotor(1..*).one.sum.is-prime-1})}...*)[$_]}
Try it online!
Explanation:
{ } # Anonymous code block
( ...*)[$_] # Index into the infinite sequence
{ } # Where each element is
$!=@_; # Save the list of previous elements into $!
+(1...{ }) # Return the first number that
$_∉$! # Has not appeared in the list so far
&& # And
(|$!,$_) # The new sequence
.rotor(1..*) # Split into rows of increasing length
# And ignoring incomplete rows
.one # Have exactly one row
.sum # Where the sum
.is-prime-1 # Is not prime (i.e. just the first row)
Haskell, 122 120 bytes
import Data.Numbers.Primes
l%a|(p,q)<-splitAt l a,(s,k:t)<-span(not.isPrime.(+sum p))q=p++k:(l+1)%(s++t)
((1:1%[2..])!!)
Try it online! (has an extra 2 bytes for f=
)
EDIT: Now uses 0-based indexing to save 2 bytes. Thanks @wastl for pointing that out, I must have missed it in the OP.
This was very fun to write! The helper function %
takes a length l
and a list of values it can use a
. It returns an infinite list of values for the sequence. The length is one less than the length of the current triangle row and the list is infinite and pre-sorted. First we just yield the first l
values from a
and then look through the rest of a until we find the first (smallest) value that makes the sum prime. We break up the list around that value using span
and some pattern matching. Now all we have to do is yield that new value and recur with the next line length l+1
and the remaining values in a
. For the final result we prepend 1 (special case for n=0) and index into it with !!
.