26
\$\begingroup\$

The plus-minus sequence

The plus-minus sequence is one that starts with two seeds, a(0) and b(0). Each iteration of this sequence is the addition and subtraction of the previous two members of the sequence. That is, a(N) = a(N-1) + b(N-1) and b(N) = a(N-1) - b(N-1).

Objective Produce the plus-minus sequence, in infinitude or the first K steps given K. You may do this using an infinite output program, a generator, or a function/program that gives the first K steps. The output order does not matter, so long as it is consistent. (I.e., b(K) a(K) or a(K) b(K), with some non-numeric, non-newline separator in between.) The output must start with the input.

Test cases

For inputs 10 2 (of a(0) b(0), this is a possible output for the first K approach (or a subsection of the infinite approach):

10     2
12     8
20     4
24     16
40     8
48     32
80     16
96     64
160    32
192    128
320    64
384    256
640    128
768    512
1280   256
1536   1024
2560   512
3072   2048
5120   1024
6144   4096
10240  2048
12288  8192
20480  4096
24576  16384
40960  8192
49152  32768
81920  16384
98304  65536

For inputs 2 20 10 (a(0) b(0) k):

2     20
22   -18
4     40
44   -36
8     80
88   -72
16    160
176  -144
32    320
352  -288

This is a , so the shortest program in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ I notice a(2n) = a(0)·2ⁿ and b(2n) = n(0)·2ⁿ, but that's probably not useful here. \$\endgroup\$ – Neil Apr 3 '16 at 21:18
  • \$\begingroup\$ Can the non-numeric separator between a and b be a newline? \$\endgroup\$ – Suever Apr 3 '16 at 22:32
  • \$\begingroup\$ @Suever No, it cannot. \$\endgroup\$ – Conor O'Brien Apr 3 '16 at 22:33
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ Thanks for the clarification! \$\endgroup\$ – Suever Apr 3 '16 at 22:34
  • 1
    \$\begingroup\$ Returning a sequence is fine @guifa \$\endgroup\$ – Conor O'Brien Jun 28 at 14:24

38 Answers 38

1
\$\begingroup\$

R, 41 bytes

The recursive solution, based on xnor's python solution:

f=function(a,b){cat(a,b,"\n");f(a+b,a-b)}

For the first-k method, the code size doubles to 82 bytes:

function(a,b,k=Inf,i=1){cat(a,b,"\n");while(i<k){cat(a<-a+b,b<-a-b*2,"\n");i=i+1}}

This function takes a and b as input, plus optionally k. If k is unspecified, it continues forever. (Well, until i overflows, which is a very big number, much larger than the recursion limit.)

\$\endgroup\$
1
\$\begingroup\$

Perl 5 -a, 41 bytes

@F=($F[0]+$F[1],$F[0]-$F[1])while say"@F"

Try it online!

Outputs the sequence infinitely.

\$\endgroup\$
1
\$\begingroup\$

dc, 27 bytes

rfr[rd3Rd_3R+p_3R-plxx]dsxx

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 46 bytes

: f 0 do 2dup swap . . cr 2dup + -rot - loop ;

Takes in A(0) B(0) and K

Try it online!

Code Explanation

: f           \ start a new word definition
  0 do        \ loop from 0 to k-1 (inclusive)
    2dup      \ duplicate A(n) and B(n)
    swap . .  \ print them out (swap the order so we get A(n) first)
    cr        \ print a newline
    2dup      \ duplicate A(n) and B(n) again
    + -rot -  \ Add A(n) and B(n), move the result, then subtract B(n) from A(n)
  loop        \ end the counted loop
;             \ end the word definition
\$\endgroup\$
1
\$\begingroup\$

JavaScript - 4438/120 Bytes

38 Bytes from n = 0 to infinity

f=(a,b)=>{console.log(a,b);f(a+b,a-b)}

Try it online!

120 Bytes from n = 0 to n = k

f(10,2,20);
f(2,20,20);

function f(a,b,k){for(o=[[a,b]],i=1;i<k;i++)o.push([o[i-1][0]+o[i-1][1],o[i-1][0]-o[i-1][1]]);console.log(o.join("\n"))}

Pascal - 71 70 Bytes

program A;

procedure F(a:integer;b:integer);begin WriteLn(a,' ',b);F(a+b,a-b)end;

begin { main program }
   F(10,2);
   F(2,20);
end.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Stax, 8 bytes

≈└i≈√=æ`

Run and debug it

Takes input in the form a(0) b(0) k.

\$\endgroup\$
0
\$\begingroup\$

Mathcad, 52 bytes

Two versions: one uses Mathcad's range variables (a form of iterator) and the second uses a for-loop inside a function.

enter image description here

The Mathcad user interface is a 2D "whiteboard", wherein expressions (which may be equations, text, plots or results) are evaluated in left-to-right, top-to-bottom order. Identifier names are usually typed letter-by-letter, but there are also many combinations that allow the user to enter Mathcad specific operators. For example, typing "=" will enter the definition operator (:=) and ";" will enter the range operator (..). Typing ctl-m brings up the matrix dialog, and typing the number of row and columns will create a matrix of that size. For golfing purposes, a Mathcad "byte" is taken to be the number of keyboard characters necessary to enter a letter/number/operator.

{the augment function is just there to make the display more table like but doesn't count to the byte total}

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 44 bytes, finite

k=>F=(a,b)=>k--&&F(a+b,a-b,console.log(a,b))

Try it online!

The function should be called with currying syntax (k)(a,b).

\$\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.