# Calculate golden ratio

Write the shortest code, in number of bytes, to display, return, or evaluate to the golden ratio (that is, the positive root of the quadratic equation: , approximately 1.618033988749895), to at least 15 significant figures. No input will be given to your program.

Sample in Stutsk programming language:

1 100 { 1 + 1 swp / } repeat print

• This question will need a scoring criteria, input/output specification, etc. Please read the FAQ - codegolf.stackexchange.com/faq – ardnew Jul 26 '12 at 20:27
• @ardnew: I'll try to at least nail down an input (namely none) and winning criterion (shortest code). The expected output is, well...most languages support double-precision, so let's do that and call it good. :-) – Chris Jester-Young Jul 30 '12 at 17:22

## Perl, Python - 10 chars

probably other languages too

.5+5**.5/2

• That's also a nice entry for the polyglot challenge :) (codegolf.stackexchange.com/q/6764/3527) – Cristian Lupascu Jul 27 '12 at 6:47
• .5+5^.5/2 will work in Octave/MATLAB, probably some others too – Griffin Jul 27 '12 at 9:31

# k (10 chars)

As continued fraction:

{%x%x+1}/1


Or in closed form for 11:

%2%1+sqrt 5


# J, 7 chars

-:1+%:5


some more text for the filter (my first J solution, heh)

• When I run -:1+%:5, the result is 1.61803. Is something more needed in the program (or system settings) to get the required "at least 15 significant figures"? – r.e.s. Jul 31 '12 at 2:23
• @r.e.s the question asks " to display, return, or evaluate". It is evaluated to the correct precision, just not displayed. It's a compliant answer. – Griffin Jul 31 '12 at 10:10

# Mathematica 11

GoldenRatio


This is the irrational number itself, not an approximation of it.

Examples (first 2 examples from Mathematica documentation)

FullSimplify[GoldenRatio^4 - GoldenRatio]
FullSimplify[GoldenRatio^20 + 1/GoldenRatio^20]
FullSimplify[GoldenRatio^2 - GoldenRatio - 1]


3 + Sqrt[5]

15127

0

## JavaScript, 17 chars

Math.sqrt(5)/2+.5


# dc, 8 chars

Fk5v1+2/


The value is on top of the stack - can be printed by adding p to the end of the program. F pushes 15 on the stack (trick found here), ksets the precision to 15 digits. The rest is normal postfix notation :-) v is a square root. Trailing p for print was omitted.

• It can be argued that the p is not needed, because the requirement is to evaluate (not necessarily display) to 15 places. – r.e.s. Feb 2 '14 at 2:02
• @r.e.s. interesting bending of rules :) thanks, updated :) – Tomas Feb 2 '14 at 2:53

# Ruby - 14 chars

(­5**0.5)/2+0.5


Based on the Javascript Perl answer above.

• Better base it on the Perl answer instead, 5**0.5 is shorter than Math.sqrt(­5). – Ilmari Karonen Sep 27 '12 at 14:14
• My mind was skipping on me, as I could not recall what the exponential equivalent to sqrt was.... – fr00ty_l00ps Sep 27 '12 at 14:21

# Language Agnostic, 15 chars

9227465/5702887


If all you need is enough precision for an IEEE 32 bit float, you can do it in 9 chars:

6765/4181


This will only work for languages that don't treat integer division specially.

• 9227465/5702887 produces only 13 correct digits - it differs on 14. digit. – Tomas Feb 2 '14 at 3:24
• 14930352/9227465 is probably the shortest, you can find it using optimal algorithm as advised on math.SE – Tomas Feb 2 '14 at 3:37

PHP 17 chars

This one is just trolling, but hey.

1.618033988749895


# APL, 7

2÷⍨1+√5
÷2÷1+√5
.5×1+√5
.5+√5÷4


Curses! I can't find a way to do it in less than 7 characters! Dialect is Nars2000.

# dc - 11 chars

15k5v2/.5+p


The most character-consuming task is setting the decimal precision..

# Mathematica - 31

N[x/.Solve[x^2-x-1==0][[2]],16]

1.618033988749895


(It's going to be the longest code, I expect...:)

# Almost language agnostic, 9 chars

### (tested in R):

.5+5^.5/2


In R, evaluates full double precision. More digits can be seen by setting options(digits=99). The question says "evaluate", so that goes with the rules.

J, 10 9 8 chars

p.1,1,_1


(root of polynomial: -x^2+x+1)

>:@%^:_+1


(continued fraction (9 chars))

%:@>:^:_+1


(continued root: (10 chars))