20
\$\begingroup\$

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: \$x^2-x-1=0\$, 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 
\$\endgroup\$
2
  • 7
    \$\begingroup\$ This question will need a scoring criteria, input/output specification, etc. Please read the FAQ - codegolf.stackexchange.com/faq \$\endgroup\$ Commented Jul 26, 2012 at 20:27
  • \$\begingroup\$ @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. :-) \$\endgroup\$ Commented Jul 30, 2012 at 17:22

46 Answers 46

1
2
1
\$\begingroup\$

Japt, 2 bytes

MQ 

Test it

Or 6 bytes without the built-in:

½+5¬/2 

Test it

\$\endgroup\$
1
  • \$\begingroup\$ # (commonmark migration) \$\endgroup\$ Commented Jul 4, 2020 at 21:18
1
\$\begingroup\$

MathGolf, 1 byte

φ 

Builtins ftw ¯\_(ツ)_/¯

Try it online.

Without builtins it's 6 bytes:

51α√½Σ 

Try it online.

Explanation:

φ # Push golden ratio builtin 1.618033988749895 # (output the entire stack joined together implicitly as result) 5 # Push 5 1 # Push 1 α # Wrap the last two values into a list: [5,1] √ # Take the square-root of each value: [2.23606797749979,1.0] ½ # Halve each: [1.118033988749895,0.5] Σ # And sum this list: 1.618033988749895 # (after which the entire stack joined together is output implicitly as result) 
\$\endgroup\$
1
\$\begingroup\$

CJam, 7 bytes

X5mq+2/ 

Try it online

Just one more byte than 05AB1E! Pretty simple stack-based translation of the equation on the Wikipedia page:

X5 Push 1 and 5 on to the stack mq Square root the top number + Add the top two numbers on the stack 2/ Divide by two (implicit) output the stack 
\$\endgroup\$
2
  • 2
    \$\begingroup\$ I've removed the non-competing tag as that no longer exists, but otherwise, nice first answer! Be sure to check out our tips for golfing in CJam as well, and hope you enjoy it here! \$\endgroup\$ Commented Jul 11, 2020 at 21:34
  • \$\begingroup\$ @caird Thanks! I've read old Code Golf threads for a while and I've seen that a few times, so I figured it was required. Thanks for the tips, I'm glad to finally try participating! \$\endgroup\$ Commented Jul 11, 2020 at 21:37
1
\$\begingroup\$

MUMPS, 11 bytes

w 5**.5+1/2 

Output: 1.618033988749894849

This highlights a quick about MUMPS: order of operations is evaluated left to right. Something like .5+5**.5/2 would give us (5.5**.5)/2 (1.172603939955857389).

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

05AB1E, 4 bytes

5t>; 

Try it online!

5t>; # full program ; # divide... t # square root of... 5 # literal... > # plus 1... ; # by 2 # implicit output 
\$\endgroup\$
1
\$\begingroup\$

Gol><>, 4 bytes

S1n; 

This answer feels like cheating because S1 simply pushes the golden ratio onto the stack.

This answer doesn't work with TIO, but it does work with this interpreter: Try it online!

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

Tcl, 23 bytes

puts [expr (1+5**.5)/2] 

Try it online!

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

Factor + math.unicode, 4 bytes

φ . 

Try it online!

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

Vyxal , 2 bytes

kg 

Try it Online!

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

POSIX bc, 13+2(?)=15(?) bytes

bc forbids non-integer exponents, so:

.5+sqrt(5/4) 

(incl. the newline) plus -l for the math library (which also conveniently bumps the scale); therefore:

$ echo '.5+sqrt(5)/2' | bc -l 1.61803398874989484820 
\$\endgroup\$
1
\$\begingroup\$

J-uby, 20 bytes

Not the shortest possible J-uby solution but more interesting. Same idea as coltim's K solution and others.

-:p^(:/&1.0|:+&1!~1) 

Attempt This Online!

Explanation

-:p ^ (:/ & 1.0 | :+ & 1 !~ 1) 

:/ & 1.0 | :+ & 1 is equivalent to ->n{ 1+1.0/n }. !~ applies that function to an initial value (here 1) until a fixed point. -:p prints it.

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

Pip, 6 bytes

URT5/2 

Attempt This Online!

Explanation

 RT5 Square root of 5 U Increment /2 Divide by 2 
\$\endgroup\$
1
\$\begingroup\$

Nekomata, 6 bytes

1¥ᵑ{ŗ→ 

Attempt This Online!

Nekomata uses fractions instead of floats, so a Diophantine approximation is used here. The output differs from the true value of \$\phi\$ by \$\sim8.5\times10^{-108}\$.

Explanation:

1¥ᵑ{ŗ→ | Full program -------+---------------------------- 1 | Starting with 1, ¥ᵑ{ | do the following 256 times: ŗ | Reciprocal → | Add 1 
\$\endgroup\$
0
\$\begingroup\$

Python, 33 bytes

ratio=(1+5**(1/2))/2 print(ratio) 

Output:

1.618033988749895 
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome to the site! You should indicate in your answer which language this is, as well as the byte count. Furthermore, I've edited your answer slightly to improve the formatting. Could you also edit in a link to a site where users can test your code, such as Try It Online!? Hope you enjoy it here at CGCC! \$\endgroup\$ Commented Aug 26, 2020 at 21:22
  • 1
    \$\begingroup\$ (1+5**(1/2))/2 is by itself is a snippet so wouldn't be a valid answer, and ratio=(1+5**(1/2))/2<newline>print(ratio) is sufficiently long it may not be a serious contender for the winning criteria of the challenge. \$\endgroup\$ Commented Dec 6, 2020 at 22:46
  • \$\begingroup\$ 22 bytes, making it a serious submission \$\endgroup\$ Commented Dec 7, 2020 at 0:05
  • 1
    \$\begingroup\$ @Lyxal 19 bytes \$\endgroup\$ Commented Dec 7, 2020 at 0:12
0
\$\begingroup\$

C (gcc), 37 bytes

Very basic solution, calculates exact value & prints.

main(){printf("%.15f",.5+sqrt(5)/2);} 

Try it online!

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

Jelly, 2 bytes

Øp 

Try it online!

\$\endgroup\$
1
2