| Bytes | Lang | Time | Link |
|---|---|---|---|
| 196 | Bespoke | 250818T205626Z | Josiah W |
| 006 | Pip | 211206T153751Z | DLosc |
| 007 | TIBASIC TI83 Plus | 250506T131609Z | madeforl |
| 015 | Haskell | 250502T070744Z | basu |
| 020 | Juby | 250501T193831Z | Jordan |
| 008 | Rattle | 220218T175040Z | d01 |
| nan | POSIX bc | 221124T124317Z | набиячлэ |
| 008 | K ngn/k | 221123T234012Z | coltim |
| 093 | brainf | 220711T234242Z | joyofori |
| 008 | Desmos | 220713T084652Z | fad |
| 002 | Vyxal ḋ | 220711T222701Z | naffetS |
| 004 | Factor + math.unicode | 211207T013658Z | chunes |
| 023 | Tcl | 211206T153308Z | sergiol |
| 004 | Gol><> | 210222T154009Z | qwatry |
| 003 | Pyth | 210207T071106Z | hakr14 |
| 025 | ><> | 210205T225054Z | qwatry |
| 002 | Jelly | 210205T081337Z | Adam |
| 016 | Whispers v3 | 210205T005600Z | Michael |
| 037 | C gcc | 201222T162410Z | engineer |
| 004 | 05AB1E | 201208T224128Z | Makonede |
| 033 | Python | 200826T204259Z | Mateusz |
| 011 | Python 3 | 201119T075532Z | jasonpi |
| 011 | MUMPS | 201119T135830Z | Joã |
| 004 | Husk | 201119T091554Z | Dominic |
| 003 | Arn | 200827T024642Z | ZippyMag |
| 007 | CJam | 200711T152706Z | Ethan Ch |
| 013 | x86 machine code | 200708T145731Z | anatolyg |
| 001 | MathGolf | 200708T135023Z | Kevin Cr |
| 006 | 05AB1E | 200705T080126Z | user9206 |
| 010 | JavaScript ECMAScript | 120802T073702Z | Inkbug |
| 002 | Japt | 200704T172504Z | Shaggy |
| 002 | Julia 0.7 | 200703T180143Z | Kirill L |
| 008 | J | 140202T002559Z | barbermo |
| 009 | Almost language agnostic | 140202T030141Z | Tomas |
| 008 | dc | 140201T222146Z | Tomas |
| 007 | APL | 140202T000516Z | Tobia |
| 017 | PHP | 140201T234924Z | Tim Segu |
| 031 | Mathematica | 140201T233724Z | cormulli |
| 015 | Language Agnostic | 121010T220024Z | Wug |
| 014 | Ruby | 120927T134637Z | fr00ty_l |
| 011 | Mathematica | 120928T032117Z | DavidC |
| 011 | dc | 120927T143758Z | daniero |
| 007 | J | 120730T205214Z | Griffin |
| 010 | k | 120727T191359Z | skeevey |
| 010 | Perl | 120726T202426Z | ardnew |
Bespoke, 196 bytes
phi is from golden mean,a number I really do see commonly
having a mystical proportion for its Fibonacci sequence:important numbers
Leonardo Bonacci gave Fibonacci sequence beautiful name
golden X
Outputs 1.618033988749894; the digits are hardcoded.
Alternate version, 273 bytes
PUT XX:FOUR SEXTET
PUSH I
OUTPUT N
OUTPUT CH
PUT XXX:I BI FIFTH
PUT XX:I NUMBERZERO
PUT XX:FOUR BI
STACKTOP POW
STACKTOP PRODUCTOF
PUSH I PUSH TRI STACKTOP MINUS
STACKTOP POW
PUT XX:I NUMBERZERO
PUT XX:BI I
STACKTOP POW
PUSH FIFTH
STACKTOP PRODUCTOF
STACKTOP MINUS
OUTPUT N
Outputs 1.6180339887498948482045; prints the number 1, then ., then the result of \$\lfloor \sqrt{125 \times 10^{42}} \rfloor - 5 \times 10^{21}\$ (the closest integer approximation of \$(\phi - 1) \times 10^{22}\$).
TI-BASIC (TI-83 Plus), 7 bytes
.5(1+√(5
pretty simple
Haskell (15 bytes)
g=(sqrt 5+1)/2
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)
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.
Rattle, 8 bytes
=5e.5+/p
Explanation
=5 set top of stack to 5
e.5 square root
+ increment
/ divide by 2
p print value
This first approach is pretty boring, it's just a direct calculation. Here's a slightly more exciting 13-byte method which calculates many Fibonacci numbers then finds the ratio between them:
+s[+~$]99$/~p
Explanation
+s increment the top of the stack (to 1) and save to memory
[ ]99 loop 99 times
+~ increment top of stack by the value in memory
$ swap the top of the stack for the value in memory
$ swap the top of the stack for the value in memory
/~ divide the top of the stack by the value in memory
p print value
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
K (ngn/k), 8 bytes
(1+1%)/1
Adapted from the built-in docs.
(...)/1set up a converge-reduce, seeded with 1 and run until two successive iterations return the same result(1+1%)add one to the inverse of the current value, and feed that into the next iteration
brainf, 93 bytes
+++++++[>+++++++<-]>.---.++++++++.-----.+++++++.--------.+++..++++++.-..-.---.+++++.-.+.----.
It works :/ Not very optimised though. I can’t think of any way to optimize this, although I’m sure a way exists.
Explanation:
+++++++ Add seven to cell at 0.
[ Begin a loop.
>+++++++ Add seven to cell at 1.
< Go back to cell 0.
- Decrement the counter there (soon it will reach 0).
] End loop when cell 0 reaches 0 after 7 repetitions.
> Go to cell 1 which is now 49.
. Print it (1).
---. Decrement 3 to get 46 (period) then print it.
Afterwards, not much explanation is needed. Just add some
or subtract some and repeat until everything is printed.
++++++++.-----.+++++++.--------.+++..++++++.-..-.---.+++++.-.+.----.
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!
><>, 29, 25 bytes
Saved 4 bytes!
After posting this answer I noticed the example Stutsk program and realized that I could probably save a few bytes. My new answer is based off the example given in the question. This program works because the golden ration can be expressed as a continued fraction.
golden_ratio = 1+1/(1+1/(1+1/...))
ff*101.;n~<
$1$,1+$:?!^1-
Old Answer
f201.;n,2+1~<
$:5$,+2,$:?!^1-
The second line approximates the sqrt of 5. After looping 15 times, this value is used to calculate the golden ratio.
C (gcc), 37 bytes
Very basic solution, calculates exact value & prints.
main(){printf("%.15f",.5+sqrt(5)/2);}
05AB1E, 4 bytes
5t>;
5t>; # full program
; # divide...
t # square root of...
5 # literal...
> # plus 1...
; # by 2
# implicit output
Python, 33 bytes
ratio=(1+5**(1/2))/2
print(ratio)
Output:
1.618033988749895
Python 3, 11 bytes
(1+5**.5)/2
= 1.618033988749895
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).
Husk, 4 bytes
(floating-point; accurate to 15 significant figures)
½→√5
This seems (to me) to be surprisingly readable for a golfing language...
√5 # square root of 5
→ # increment
½ # halve
Husk, 9 bytes
(calculation as arbitrary precision rational number)
!Ẋ/İf!5İ⁰
Try it online! (TIO header converts the rational number [expressed in Husk as a fraction] to its first 1000 decimal digits)
/ # get the ratio of
Ẋ # every pair of elements of
İf # the fibonacci sequence;
! # now select the ratio at position
!5İ⁰ # 10^5 (change to !9İ⁰ for more accuracy with same byte-count)
Arn, 3 bytes
phi
Just a builtin, too short to be compressed. Returns 1.61803398874989484820458683436563811.
A more interesting one. If running in the downloadable version, this will print n digits, given the command:
arn run file.arn -p n
or 25 digits if the -p flag is not provided.
If running in the online version, this will print the first 50 digits.
6 bytes
l[├Qn0
Explanation
Unpacked: :-1+:/5
:- Halve
1 Literal one
+ Plus
:/ Square root of
5 Literal five
CJam, 7 bytes
X5mq+2/
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
x86 machine code, 13 bytes
Hexdump:
b1 7f d9 e8 d9 e8 de c1 d9 fa e2 f8 c3
Disassembly:
100139D0 B1 7F mov cl,7Fh
100139D2 D9 E8 fld1
again:
100139D4 D9 E8 fld1
100139D6 DE C1 faddp st(1),st
100139D8 D9 FA fsqrt
100139DA E2 F8 loop again (100139D4h)
100139DC C3 ret
Uses the converging sequence an = sqrt(an-1 + 1).
The number of iterations is determined by the contents of ecx, which is mostly garbage. The minimal number of iterations is 127, which guarantees good precision (actually, 30 iterations should be enough). In the worst case, the calculation will take a few minutes (232-1 iterations), and in the best case, it's instantaneous (127 iterations).
MathGolf, 1 byte
φ
Builtins ftw ¯\_(ツ)_/¯
Without builtins it's 6 bytes:
51α√½Σ
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)
05AB1E, 6 bytes
5X‚t;O
Explanation
5 Push 5
X Push 1
‚ Pair: [5, 1]
t Square root: [2.23606797749979, 1.0]
; Halve: [1.118033988749895, 0.5]
O Sum: 1.618033988749895
JavaScript (ECMAScript), 10 chars
5**.5/2+.5
This is the same as the Perl & Python submission - thanks to Redwolf Programs for telling me about this.
However, back in 2012, when this answer was originally written, the ** operator did not exist in JavaScript. While almost all browsers and do now support the exponentiation operator, according to Can I Use, as of July 2020, around 9% of users still does not support it, including the latest version of Internet Explorer. Thus, the old version of the answer:
JavaScript (backwards-compatible), 17 chars
Math.sqrt(5)/2+.5
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))
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.
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), k sets the precision to 15 digits. The rest is normal postfix notation :-) v is a square root. Trailing p for print was omitted.
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.
PHP 17 chars
This one is just trolling, but hey.
1.618033988749895
Mathematica - 31
N[x/.Solve[x^2-x-1==0][[2]],16]
1.618033988749895
(It's going to be the longest code, I expect...:)
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.
Ruby - 14 chars
(5**0.5)/2+0.5
Based on the Javascript Perl answer above.
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
dc - 11 chars
15k5v2/.5+p
The most character-consuming task is setting the decimal precision..
J, 7 chars
-:1+%:5
some more text for the filter (my first J solution, heh)
k (10 chars)
As continued fraction:
{%x%x+1}/1
Or in closed form for 11:
%2%1+sqrt 5
Perl, Python - 10 chars
probably other languages too
.5+5**.5/2