An expanding array
Mathematica, 72 64 59 58 bytes
(d=2;#//.x_:>Riffle[x,(x+{##2,}&@@x)/d++]~Cases~_Integer)&
Try it online!
How it works
We take the input as a list {p,q}
. The iteration step is reformulated as:
- Insert
(a+b)/d
between every two elementsa
andb
:(x+{##2,}&@@x)
computes the sequence ofa+b
's, with ana+Null
at the end. We divide byd
, andRiffle
inserts each(a+b)/d
betweena
andb
. Incrementd
. - Pick out the
Integer
elements of the resulting list. (This gets rid of theNull
introduced by{##2,}
, too.)
This is repeated until the result doesn't change (which can only happen because we removed all the new elements, because none of them were integers).
-8 bytes thanks to @MartinEnder from using //.
instead of FixedPoint
(and from taking input as a list).
-6 more because ListConvolve
actually isn't that great
05AB1E, 28 19 18 bytes
[Ðü+NÌ/‚ζ˜ʒ.ï}DŠQ#
Try it online!
eh, can definitely be improved hardcore. still working to refactor.
Probably as good as I'm getting it.
-1 thanks to, who else but, Emigna! For pointing out swap worked better than the registers.
[ // Infinite loop.
Ð // Triplicate [p, ..., q]
U // Pop 1 of 3 copies into register X.
ü+ // Pairwise addition.
NÌ/ // Divide by current iteration + 2 (which is d).
‚ // Group original [p, ..., q] with pairwise additives.
ζ˜ // Transpose together and flatten.
ʒ.ï} // Filter out non-integer entities (includes the space added by zip).
DXQ // Dupe result, see if equal to original.
# // If new array is original array, nothing happened, quit & return.
Debug dump for [p,q] = [1,3]
:
Full program: [ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
current >> [ || stack: []
ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
Full program: ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
current >> Ð || stack: []
current >> U || stack: [[1, 3], [1, 3], [1, 3]]
current >> ü || stack: [[1, 3], [1, 3]]
Full program: +
current >> + || stack: [1, 3]
stack > [4]
current >> N || stack: [[1, 3], [4]]
current >> Ì || stack: [[1, 3], [4], 0]
current >> / || stack: [[1, 3], [4], 2]
current >> ‚ || stack: [[1, 3], [2.0]]
current >> ζ || stack: [[[1, 3], [2.0]]]
current >> ˜ || stack: [[[1, 2.0], [3, ' ']]]
current >> ʒ || stack: [[1, 2.0, 3, ' ']]
Filter: .ï
Full program: .ï
current >> . || stack: [1]
stack > [1]
Full program: .ï
current >> . || stack: [2.0]
stack > [1]
Full program: .ï
current >> . || stack: [3]
stack > [1]
Full program: .ï
current >> . || stack: [' ']
invalid literal for int() with base 10: ' '
stack > []
current >> D || stack: [[1, 2.0, 3]]
current >> X || stack: [[1, 2.0, 3], [1, 2.0, 3]]
current >> Q || stack: [[1, 2.0, 3], [1, 2.0, 3], [1, 3]]
current >> # || stack: [[1, 2.0, 3], 0]
stack > [[1, 2.0, 3]]
Full program: ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
current >> Ð || stack: [[1, 2.0, 3]]
current >> U || stack: [[1, 2.0, 3], [1, 2.0, 3], [1, 2.0, 3]]
current >> ü || stack: [[1, 2.0, 3], [1, 2.0, 3]]
Full program: +
current >> + || stack: [1, 2.0]
stack > [3.0]
Full program: +
current >> + || stack: [3.0, 2.0, 3]
stack > [3.0, 5.0]
current >> N || stack: [[1, 2.0, 3], [3.0, 5.0]]
current >> Ì || stack: [[1, 2.0, 3], [3.0, 5.0], 1]
current >> / || stack: [[1, 2.0, 3], [3.0, 5.0], 3]
current >> ‚ || stack: [[1, 2.0, 3], [1.0, 1.6666666666666667]]
current >> ζ || stack: [[[1, 2.0, 3], [1.0, 1.6666666666666667]]]
current >> ˜ || stack: [[[1, 1.0], [2.0, 1.6666666666666667], [3, ' ']]]
current >> ʒ || stack: [[1, 1.0, 2.0, 1.6666666666666667, 3, ' ']]
Filter: .ï
Full program: .ï
current >> . || stack: [1]
stack > [1]
Full program: .ï
current >> . || stack: [1.0]
stack > [1]
Full program: .ï
current >> . || stack: [2.0]
stack > [1]
Full program: .ï
current >> . || stack: [1.6666666666666667]
stack > [0]
Full program: .ï
current >> . || stack: [3]
stack > [1]
Full program: .ï
current >> . || stack: [' ']
invalid literal for int() with base 10: ' '
stack > []
current >> D || stack: [[1, 1.0, 2.0, 3]]
current >> X || stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
current >> Q || stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3], [1, 2.0, 3]]
current >> # || stack: [[1, 1.0, 2.0, 3], 0]
stack > [[1, 1.0, 2.0, 3]]
Full program: ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
current >> Ð || stack: [[1, 1.0, 2.0, 3]]
current >> U || stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
current >> ü || stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
Full program: +
current >> + || stack: [1, 1.0]
stack > [2.0]
Full program: +
current >> + || stack: [2.0, 1.0, 2.0]
stack > [2.0, 3.0]
Full program: +
current >> + || stack: [2.0, 3.0, 2.0, 3]
stack > [2.0, 3.0, 5.0]
current >> N || stack: [[1, 1.0, 2.0, 3], [2.0, 3.0, 5.0]]
current >> Ì || stack: [[1, 1.0, 2.0, 3], [2.0, 3.0, 5.0], 2]
current >> / || stack: [[1, 1.0, 2.0, 3], [2.0, 3.0, 5.0], 4]
current >> ‚ || stack: [[1, 1.0, 2.0, 3], [0.5, 0.75, 1.25]]
current >> ζ || stack: [[[1, 1.0, 2.0, 3], [0.5, 0.75, 1.25]]]
current >> ˜ || stack: [[[1, 0.5], [1.0, 0.75], [2.0, 1.25], [3, ' ']]]
current >> ʒ || stack: [[1, 0.5, 1.0, 0.75, 2.0, 1.25, 3, ' ']]
Filter: .ï
Full program: .ï
current >> . || stack: [1]
stack > [1]
Full program: .ï
current >> . || stack: [0.5]
stack > [0]
Full program: .ï
current >> . || stack: [1.0]
stack > [1]
Full program: .ï
current >> . || stack: [0.75]
stack > [0]
Full program: .ï
current >> . || stack: [2.0]
stack > [1]
Full program: .ï
current >> . || stack: [1.25]
stack > [0]
Full program: .ï
current >> . || stack: [3]
stack > [1]
Full program: .ï
current >> . || stack: [' ']
invalid literal for int() with base 10: ' '
stack > []
current >> D || stack: [[1, 1.0, 2.0, 3]]
current >> X || stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
current >> Q || stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
current >> # || stack: [[1, 1.0, 2.0, 3], 1]
[1, 1.0, 2.0, 3]
stack > [[1, 1.0, 2.0, 3]]
Try it online with debug!
Ruby, 80 bytes
->a,d=2{*b=a[0];a.each_cons 2{|x,y|z=x+y;b+=z%d>0?[y]:[z/d,y]};b==a ?a:f[b,d+1]}
Try it online!
Recursive function f
taking input as the array [p, q]
.