21
\$\begingroup\$

Given two positive integers p and q, your task is to return the array A created by applying the following algorithm:

  1. Start with A = [p, q] and d = 2
  2. For each pair (x, y) of contiguous numbers in A whose sum is divisible by d, insert (x + y) / d between x and y.
  3. If at least one matching pair was found, increment d and go on with step #2. Otherwise, stop and return A.

Example

Below is the detail of the process for p = 1 and q = 21.

  1  21             | Iteration #1: we start with d = 2 and A = [1, 21]
   \/               |               1 + 21  is divisible by 2 -> we insert 11
 22/2=11            |
                    |
  1  11 21          | Iteration #2: d = 3, A = [1, 11, 21]
   \/               |               1 + 11  is divisible by 3 -> we insert 4
 12/3=4             |
                    |
  1 4 11  21        | Iteration #3: d = 4, A = [1, 4, 11, 21]
        \/          |               11 + 21 is divisible by 4 -> we insert 8
      32/4=8        |
                    |
  1    4    11 8 21 | Iteration #4: d = 5, A = [1, 4, 11, 8, 21]
    \/   \/         |               1 + 4   is divisible by 5 -> we insert 1
  5/5=1 15/5=3      |               4 + 11  is divisible by 5 -> we insert 3
                    |
  1 1 4 3 11 8 21   | Iteration #5: d = 6, A = [1, 1, 4, 3, 11, 8, 21]
                    |               no sum of two contiguous numbers is divisible by 6
                    |               -> we stop here

Hence the expected output: [1, 1, 4, 3, 11, 8, 21]

Clarifications and rules

  • Input and output can be handled in any reasonable format. The integers p and q are guaranteed to be greater than 0. If that helps, you may assume q ≥ p.
  • The 2nd step of the algorithm should not be recursively applied to elements that have just been inserted at the same iteration. For instance, A = [1, 1] and d = 2 should lead to [1, 1, 1] (not an infinite list of 1's).
  • This is , so the shortest answer in bytes wins!

Test cases

  p |   q | Output
----+-----+-------------------------------------------------------------------------------
  1 |   1 | [1,1,1]
  1 |   2 | [1,2]
  1 |   3 | [1,1,2,3]
  2 |   6 | [2,1,2,1,4,1,2,6]
  3 |  13 | [3,1,8,1,3,1,7,1,2,1,5,1,3,2,13]
  9 |   9 | [9,6,9,6,9]
 60 |  68 | [60,13,1,4,31,2,3,5,2,19,64,7,13,1,2,5,2,27,44,3,4,8,2,1,12,1,5,3,28,2,4,16,1,
    |     |  2,12,1,2,1,10,1,6,68]
144 | 336 | [144,68,3,4,8,1,12,1,4,2,28,13,128,44,17,92,240,58,108,5,17,1,2,5,3,28,3,1,11,
    |     |  60,3,6,2,42,2,4,26,192,54,132,7,1,15,1,3,1,18,1,4,2,30,3,1,12,1,9,78,46,336]

If you'd like to test your code on a slightly bigger test case, here is the expected output for:

  • p = 12096 (26*33*7)
  • q = 24192 (27*33*7)
\$\endgroup\$

15 Answers 15

6
\$\begingroup\$

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!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ ohh so that's how ü works... this allowed me to improve one of my previous answers :-) \$\endgroup\$ – scottinet Oct 6 '17 at 19:40
  • \$\begingroup\$ @scottinet [1,2,3,4] ü = [[1,2],[2,3],[3,4]], also if you add "-d" in the arguments when running 05AB1E, it produces the "debug" output that I've attached above. (Added the debug link above too). The reason pairwise is neat is because for the commands that vectorize automatically, it just applies the command pairwise (running ü) on a list shows this well). \$\endgroup\$ – Magic Octopus Urn Oct 6 '17 at 19:41
  • \$\begingroup\$ I figured that out, it allowed me to save 1 byte on that answer. As for -d... I found about it waaay too late, after "debugging" with ,q to "print and stop". It was painful. \$\endgroup\$ – scottinet Oct 6 '17 at 19:47
  • \$\begingroup\$ @scottinet I'd been using 05AB1E for a full year before learning about it :(. I used = because it doesn't pop, and just prints the last item pushed to the stack. \$\endgroup\$ – Magic Octopus Urn Oct 6 '17 at 19:48
  • \$\begingroup\$ If you remove U you can replace X with Š. \$\endgroup\$ – Emigna Oct 6 '17 at 20:48
8
\$\begingroup\$

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:

  1. Insert (a+b)/d between every two elements a and b: (x+{##2,}&@@x) computes the sequence of a+b's, with an a+Null at the end. We divide by d, and Riffle inserts each (a+b)/d between a and b. Increment d.
  2. Pick out the Integer elements of the resulting list. (This gets rid of the Null 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

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ //. trumps FixedPoint, and I'd just take input as a pair of integers instead of two separate integers: (d=2;#//.x_:>x~Riffle~ListConvolve[{1,1}/d++,x]~Cases~_Integer)& \$\endgroup\$ – Martin Ender Oct 6 '17 at 15:33
  • \$\begingroup\$ Thanks! I keep forgetting to replace //. with FixedPoint, because I really really like FixedPoint. \$\endgroup\$ – Misha Lavrov Oct 6 '17 at 15:36
  • 1
    \$\begingroup\$ It's only 64 bytes. Looks like you inserted two unprintables inside Integer. \$\endgroup\$ – Martin Ender Oct 6 '17 at 15:38
  • \$\begingroup\$ Thanks again! I had no idea why my code kept failing after the change, and would go back to working when I did things that really shouldn't make a difference. \$\endgroup\$ – Misha Lavrov Oct 6 '17 at 15:46
  • 1
    \$\begingroup\$ The code snippets in comments sometimes have these unprintable characters, especially when the code snippet has a linebreak. Not sure why SE inserts them. \$\endgroup\$ – Martin Ender Oct 6 '17 at 15:48
4
\$\begingroup\$

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].

|improve this answer|||||
\$\endgroup\$
4
\$\begingroup\$

Haskell, 85 81 bytes

(a:b:c)#d=a:[div(a+b)d|mod(a+b)d<1]++(b:c)#d
l#d=l
l%d|l==l#d=l|e<-d+1=l#d%e
(%2)

Try it online!

The input is taken as a list, e.g. [1,2].

Edit: -4 bytes thanks to @Laikoni.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Save two bytes with l%d|l==l#d=l|e<-d+1=l#d%e. \$\endgroup\$ – Laikoni Oct 6 '17 at 21:29
  • \$\begingroup\$ @Laikoni: actually it's four bytes. Thanks! \$\endgroup\$ – nimi Oct 6 '17 at 21:45
3
\$\begingroup\$

Python 2, 112 110 108 105 103 bytes

-2 bytes thanks to Jonathan Frech
-5 bytes thanks to Erik the Outgolfer

y=input()
x=d=1
while x!=y:
 d+=1;x=y;y=x[:1]
 for a,b in zip(x,x[1:]):c=a+b;y+=[c/d,b][c%d>0:]
print x

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Is not y+=[...]*(...);y+=b, equivalent to y+=[...]*(...)+[b]? \$\endgroup\$ – Jonathan Frech Oct 6 '17 at 15:59
  • \$\begingroup\$ @JonathanFrech yes \$\endgroup\$ – Rod Oct 6 '17 at 16:00
  • \$\begingroup\$ 103 bytes \$\endgroup\$ – Erik the Outgolfer Oct 6 '17 at 16:20
  • \$\begingroup\$ @EriktheOutgolfer i was trying to do this, but was on 110b @.@ \$\endgroup\$ – Rod Oct 6 '17 at 16:25
3
\$\begingroup\$

Python 2, 98 bytes

f=lambda A,B=0,d=2:A*(A==B)or f(sum([[(l+r)/d,r][(l+r)%d>0:]for l,r in zip(A,A[1:])],A[:1]),A,d+1)

Invoke as f([p,q]). Try it online!

Jonathan Allan saved 12 bytes. Thanks~!

Explanation

f is a recursive function: f(A, B, d) evalutes to f(next_A, A, d+1), unless A == B, in which case it returns A. (This is handled by A*(A==B)or …: if A ≠ B, A*(A==B) is the empty list, which is false-y, so the part is evaluated; if A = B then A*(A==B) is A, which is non-empty and thus truthy, and it gets returned.)

next_A is computed as:

sum([[(l+r)/d,r][(l+r)%d>0:]for l,r in zip(A,A[1:])],A[:1])

This is best explained by example. When e.g. d = 5 and A = [1, 4, 11, 8, 21]:

  sum([[(l+r)/d,r][(l+r)%d>0:]for l,r in zip(A,A[1:])],A[:1])
= sum([[(1+4)/d, 4], [(4+11)/d, 11], [8], [21]], [1])
= [1] + [1, 4] + [3, 11] + [8] + [21]
= [1, 1, 4, 3, 11, 8, 21]
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Save 8 bytes using a zip in place of the enumerate and using [A[0]] as the sum's initial value. \$\endgroup\$ – Jonathan Allan Oct 7 '17 at 12:12
  • \$\begingroup\$ Save another 4 using a recursive function \$\endgroup\$ – Jonathan Allan Oct 7 '17 at 12:23
  • \$\begingroup\$ @JonathanAllan Nice! I saved another byte replacing [A[0]] with A[:1] :) \$\endgroup\$ – Lynn Oct 7 '17 at 12:31
  • 1
    \$\begingroup\$ And now I’m leading by 3 bytes thanks to A*(A==B). \$\endgroup\$ – Lynn Oct 7 '17 at 12:33
2
\$\begingroup\$

Python 2, 111 bytes

A=input()
m=d=1
while m:
 m=o=0;d+=1
 while A[o+1:]:
	o+=1;s=A[o-1]+A[o]
	if s%d<1:A[o:o]=s/d,;m=1;o+=1
print A

Try it online!

-8 thanks to Rod.
-2 thanks to Lynn.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Husk, 22 bytes

→UG`λf£NΣẊṠeo/⁰+:.)⁰tN

Takes a 2-element list, returns list of integers and floats. Try it online!

Explanation

→UG`λf£NΣẊṠeo/⁰+:.)⁰tN  Input is a list L.
  G                 tN  Cumulative reduce over the list [2,3,4..
                   ⁰    with initial value L
   `λ             )     using flipped version of this function:
     f£NΣẊṠeo/⁰+:.       Arguments are a list, say K=[1,3,3], and a number, say d=4.
                :.       Prepend 0.5: [0.5,1,2,3]
         Ẋ               For each adjacent pair,
               +         take their sum,
            o/⁰          divide by d,
          Ṡe             and pair it with the right number in the pair: [[0.375,1],[1.0,3],[1.5,3]]
        Σ                Concatenate: [0.375,1,1.0,3,1.5,3]
     f£N                 Remove non-integers: [1,1.0,3,3]
                        Now we have an infinite list of L threaded through 2,3,4.. using the expansion operation.
 U                      Take longest prefix of unique elements,
→                       then last element of that.
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 92 + 1 (-a) = 93 bytes

do{$c=0;@n=shift@F;$.++;push@n,($t=$_+$n[-1])%$.?$_:($c=$t/$.,$_)for@F;@F=@n}while$c;say"@F"

Try it online!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Retina, 111 bytes

\d+
$*1;
^
11@
{+`(1+); (1+);
$1; $1$2 $2;
(?<=(?=(1+)).*) (\1)+ 
 a$#2$*1; 
 1+ 
 
.*a
1$&
)`a

1+@

1+
$.&
;

Try it online!

Takes the input as space separated numbers. Quite naively follows the given algorithm, with the only notable technique being to use a marker symbol, a, to note when any of the numbers had been kept. This is used to work with Retina's somewhat limited looping capabilities, which only allows you to loop until a set of stages makes no overall change to the input to those stages.

Explanation:

This will use the same example as in the question.

\d+
$*1;

We change the input array of numbers into a semicolon separated unary array, so we'd have:

1; 111111111111111111111;

^
11@

Put d into our code at the beginning, giving us:

11@1; 111111111111111111111;

{+`(1+); (1+);
$1; $1$2 $2;

This is slightly more complicated. { starts a group of stages that will be executed until they reach a fixed point. Then, + indicates that this stage itself should be executed until a fixed point. This stage adds each pair of adjacent numbers but inserts them without the additional semicolon. Now we'd have:

11@1; 1111111111111111111111 111111111111111111111;

(?<=(?=(1+)).*) (\1)+ 
 a$#2$*1;

The other tricky stage, this one accumulates our divisor in the first capture group, and replaces any number in our list without a trailing semicolon with that number divided by d. We also add a leading a to these numbers, to indicate that something was kept, along with the ; to indicate it should be permanently part of the array. Now we'd have:

11@1; a11111111111; 111111111111111111111;
 1+ 

This deletes numbers that were not divisible by d nor in the array before this round. This makes no changes in our example.

.*a
1&$

This greedily matches from the beginning of the string to the last letter a in the input. This means there can be at most one match. If we made any changes, we add one to d, otherwise leaving it the same so we can exit the loop.

111@1; a11111111111; 111111111111111111111;

)`a

The ) closes the loop started by { (don't question it!) and otherwise this stage just removes the markers we put down earlier. Since this is the end of the loop, we would repeat the above stages many times, however I'm just going to continue as though I had forgotten the loop, since it makes the example more continuous.

111@1; 11111111111; 111111111111111111111;

1+@

This stage removes from our output:

1; 11111111111; 111111111111111111111;

1+
$.&

This stage replaces the unary numbers with decimal numbers:

1; 11; 21;

;

The final stage gets rid of the semicolons:

1 11 21

Obviously, skipping out on the loop makes us have an incorrect result here, but hopefully that isn't too confusing.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ My markup preview looks rather different from the output I'm seeing - anyone have any ideas? Particularly a bunch of code blocks are joining together when I don't think they should be. \$\endgroup\$ – FryAmTheEggman Oct 7 '17 at 2:51
1
\$\begingroup\$

JavaScript (ES6), 89 87 82 bytes

Thanks @Arnauld for -2 bytes and for helping save 5 more bytes.

f=(a,d=2,r)=>a.map(v=>b.push(v,...(v+=a[++i])%d<1?[r=v/d]:[]),b=i=[])|r?f(b,d+1):a

Takes input as an array: f([p,q]).

Test Cases

f=(a,d=2,r)=>a.map(v=>b.push(v,...(v+=a[++i])%d<1?[r=v/d]:[]),b=i=[])|r?f(b,d+1):a

;[[1,1],[1,2],[1,3],[2,6],[3,13],[9,9],[60,68],[144,336],[12096,24192]]
.forEach(test=>O.innerText+=JSON.stringify(test)+" -> "+JSON.stringify(f(test))+"\n")
<pre id=O></pre>

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I think you can update v (v+=b[++i]) instead of using s to save 1 byte. You may save another byte with |r instead of &&r (I think it's safe but I didn't double-check). \$\endgroup\$ – Arnauld Oct 7 '17 at 8:30
  • \$\begingroup\$ @Arnauld Thanks! Using |r indeed passed all the test cases. \$\endgroup\$ – Justin Mariner Oct 7 '17 at 10:28
  • \$\begingroup\$ Here is a 85-byte variant using push(). \$\endgroup\$ – Arnauld Oct 7 '17 at 11:58
  • \$\begingroup\$ @Arnauld Nice, I originally thought of using push just once instead of twice; after revisiting that idea I came to this for 86 bytes. Maybe that can be improved? \$\endgroup\$ – Justin Mariner Oct 7 '17 at 20:05
  • \$\begingroup\$ You can do push(v,...) and then use v+= again for 84 bytes. \$\endgroup\$ – Arnauld Oct 7 '17 at 20:56
1
\$\begingroup\$

Röda, 90 bytes

f a,d=2{A=[a()|slide 2|[_,(_1+_)/d]if[(_1+_2)%d=0]else[_1]]+a[-1]f A,d=d+1 if[a!=A]else A}

Try it online!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Java 8, 180 bytes

import java.util.*;p->q->{List<Integer>r=new Stack();r.add(p);r.add(q);for(int d=1,f=d,i;f==d++;)for(i=1;i<r.size();i++)if((q=r.get(i)+r.get(i-1))%d<1)r.add(i++,q/(f=d));return r;}

Explanation:

Try it here.

import java.util.*;           // Required import for List and Stack

p->q->{                       // Method with two integer parameters and List return-type
  List<Integer>r=new Stack(); //  Result-list
  r.add(p);r.add(q);          //  Add the two input-integers to the List
  for(int d=1,                //  Divisible integer (starting at 1)
          f=d,                //  Temp integer (starting at `d` / also 1)
          i;                  //  Index-integer
      f==d++;)                //  Loop (1) as long as `f` and `d` are equal
                              //  (and raise `d` by 1 so it starts at 2 inside the loop)
    for(i=1;                  //   Reset index-integer to 1
        i<r.size();i++)       //   Inner loop (2) over the List
      if((q=r.get(i)+r.get(i-1)) 
                              //    If the current + previous items (stored in `q`)
         %d<1)                //    are divisible by `d`:
        r.add(i++,q/(f=d));   //     Insert `q` divided by `d` to the List at index `i`
                              //     (and raise `i` by 1 and set `f` to `d` in the process)
                              //   End of inner loop (2) (implicit / single-line body)
                              //  End of loop (1) (implicit / single-line body)
  return r;                   //  Return the result-List
}                             // End of method
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

C#, 280 bytes

using System.Linq;class A{static void Main(string[] p){var l=new System.Collections.Generic.List<int>(p.Select(int.Parse));int r,d=2,c;do{c=0;for(int i=1;i<l.Count;++i){if((r=l[i-1]+l[i])%d==0){l.Insert(i++,r/d);++c;}}++d;}while(c>0);l.ForEach(v=>System.Console.Write((v+" ")));}}

First attempt at code golf, which is the whole program. Test It

Attempt 2, 159 bytes

Taking away the scaffolding, since the task is to provide a function that can take a pair of numbers (an array works) and returns an array. Given that a Func<int[],int[]> F can be used to satisfy the requirements, just define F:

F=v=>{var l=new List<int>(v);int i,r,d=2,c;do{c=0;for(i=1;i<l.Count;++i){if((r=l[i-1]+l[i])%d==0){l.Insert(i++,r/d);++c;}}++d;}while(c>0);return l.ToArray();};

Test Full Program Here

This could be smaller if a generic List is considered a valid output (drop the .ToArray() to save 10 bytes).

If the input can also be modified, then passing in a List<int> instead of an array removes the need to initialize the output (comes out at 126 bytes).

Taking this a step further, there doesn't really need to be a return value in this case. Using an Action instead removes the 9 bytes used by the return statement.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Nice first answer. \$\endgroup\$ – Arnauld Oct 9 '17 at 13:44
1
\$\begingroup\$

Jelly, 19 bytes

®‘©‘÷@+2\⁸żFfṀR$µÐL

Try it online!

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.