# Recamán's triangular fibonacci

This challenge is based upon three sequences, below are their formulae:

a1 = 0; for n > 0, an = an-1 - n

if positive and not already in the sequence, otherwise

an = an-1 + n

an = (Φn – (–Φ)–n) / √5

where Φ denotes the golden ratio, and

a1 = 0

a1 = 0; an = n * (n+1) / 2

Your task is to take specified nth terms of each sequence and perform certain basic mathematical operations on them, namely addition, subtraction, multiplication, division, and the support of parentheses, nothing more is required.

Input is always in infix notation, and output formats are unrestricted.

Your code must be able to handle all real numbers from -10,000,000 to +10,000,000

Standard loopholes apply, and built-in functions for these sequences are not allowed.

Examples:

(These examples use an example output format of 1R = 1st term of R, 3430T = 3430th term of T, and so on, however you are free to use whichever output format you desire.)

Examples:

• (1R + 1F) * 3

= ( 0 + 0 ) * 3

= 0

• (1R * 1F) - 3

= ( 0 * 0 ) - 3

= - 3

• (1R + 2R) + (57R - 35R) / (51R - 41R) ) + (31F + 32F) - (54T)

= { [ (0 + 1) + (30 - 113) ] / (33 - 38) } + (832040 + 1346269) - (1431)

= { [ (0 + 1) + (-83) ] / (-5) } + (2178309) - (1431)

= [ (1 - 83 ) / (-5) ] + (2178309) - (1431)

= (-82 / -5) + (2178309) - (1431)

= (16.4) + (2178309) - (1431)

= 2176894.4

• Reposting deleted challenges is poor form and can lead to an automatic question ban. If a challenge of yours should be undeleted, please flag it for moderator attention. Nov 11 '16 at 16:00
• @DrMCMoylex Regardless of suggestions in chat, having a heavily downvoted, closed, deleted question contributes to automatic question bans, which cannot be lifted by moderators. The only way of lifting the ban is to fix those questions, which isn't possible if they have already been duped. Nov 11 '16 at 16:10
• Nov 11 '16 at 16:21

## JavaScript (ES6), 164163 148 bytes

Input format: F(n), R(n), T(n)

s=>eval(s.replace(/[FRT]/g,c=>(F=n=>${c<'R'?'n>1?F(n-2)+F(n-1):n':c>'R'?'n*++n/2':'(N=>{for(l=[1];--n;)l[x=y+=++N>y|l[y-N]?N:-N]=1})(x=y=0)|x'})))  ### Example This may take a few seconds to complete. let f = s=>eval(s.replace(/[FRT]/g,c=>(F=n=>${c<'R'?'n>1?F(n-2)+F(n-1):n':c>'R'?'n*++n/2':'(N=>{for(l=[1];--n;)l[x=y+=++N>y|l[y-N]?N:-N]=1})(x=y=0)|x'})))

console.log(f("((R(1) + R(2)) + (R(57) - R(35))) / (R(51) - R(41)) + (F(30) + F(31)) - (T(53))"));

• The input format is just an example so you could save bytes with R(1) F(30) and T(53) instead of 1R 30F and 53T
– Hedi
Nov 11 '16 at 20:55
• @Hedi Oh, thanks for pointing this out. I didn't pay attention to that part of the rules. Nov 11 '16 at 23:39

# Perl, 201199 169 + 1 = 170 bytes

Run with the -n flag.

Whitespace is provided for readability and is not part of the code.

sub R{
$_=pop; %m=(@_=0,1);$m{push@_,$m{$a=$_[-1]-@_}||$a<0?$a+2*@_:$a}=1 for 2..$_; pop } sub F{ @_=(0,1,@_); push@_,$_[-1]+$_[-2]for 3..pop; pop } sub T{ ($_=pop)*\$_++/2
}
say eval


Pretty straightforward. R calculates the nth Recaman number, F calculates the nth Fibonacci number, and T calculates the nth Triangular number. The script accepts input of an expression with terms such as R(##), T(##), and F(##), and evaluates it.

I'm sure I can reduce the byte-count for R, but I'll have to spend more time thinking about it.

## Ruby, 135 132 129 bytes

->s{R,F,T=->x{*y=r=0;(x-1).times{|i|r<<y;y=r-[z=y+~i]!=r||z<0?y-~i:z};y},->x{a,b=1,0;(x-1).times{a=b+b=a};b},->x{x*~-x/2};eval s}


Input format: string using the sequence names and subscript. Example:

( (1R + 2R) + (57R - 35R) / (51R - 41R) ) + (31F + 32F) - (54T)


becomes

"( (R[1] + R[2]) + (R[57] - R[35]) / (R[51] - R[41]) ) + (F[31] + F[32]) - (T[54])"


Sorry for adding the missing bracket to the example, it wouldn't work otherwise. :-)

### Explanation

The evalis using the definition of R, T, and F functions.

Fibonacci: my own entry in the Fibonacci contest: https://codegolf.stackexchange.com/a/103329/18535 changed to start from 0 instead of 1.

Recamán: inspired by Martin Ender's answer: https://codegolf.stackexchange.com/a/37640/18535 then golfed away a couple of bytes.

Triangular: starting from 0, instead of n*(n+1)/2 I could use n*(n-1)/2