6
\$\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\$
  • 5
    \$\begingroup\$ This question will need a scoring criteria, input/output specification, etc. Please read the FAQ - codegolf.stackexchange.com/faq \$\endgroup\$ – ardnew Jul 26 '12 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\$ – Chris Jester-Young Jul 30 '12 at 17:22

16 Answers 16

14
\$\begingroup\$

Perl, Python - 10 chars

probably other languages too

.5+5**.5/2
| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

k (10 chars)

As continued fraction:

{%x%x+1}/1

Or in closed form for 11:

%2%1+sqrt 5
| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

J, 7 chars

-:1+%:5

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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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"? \$\endgroup\$ – r.e.s. Jul 31 '12 at 2:23
  • \$\begingroup\$ @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. \$\endgroup\$ – Griffin Jul 31 '12 at 10:10
3
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

JavaScript, 17 chars

Math.sqrt(5)/2+.5
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ There's now a ** operator, so 5**.5/2+.5 would work too. Note that this is the same as the python submission. \$\endgroup\$ – Redwolf Programs 8 hours ago
2
\$\begingroup\$

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.

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

Ruby - 14 chars

(­5**0.5)/2+0.5

Based on the Javascript Perl answer above.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Better base it on the Perl answer instead, 5**0.5 is shorter than Math.sqrt(­5). \$\endgroup\$ – Ilmari Karonen Sep 27 '12 at 14:14
  • \$\begingroup\$ My mind was skipping on me, as I could not recall what the exponential equivalent to sqrt was.... \$\endgroup\$ – fr00ty_l00ps Sep 27 '12 at 14:21
  • \$\begingroup\$ 8 years late... the parentheses are not needed. \$\endgroup\$ – Dingus Apr 23 at 0:30
1
\$\begingroup\$

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.

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

PHP 17 chars

This one is just trolling, but hey.

1.618033988749895
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

dc - 11 chars

15k5v2/.5+p

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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Mathematica - 31

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

1.618033988749895

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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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))

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 3, 10 chars

5**.5/2+.5

Try it online!

| improve this answer | |
New contributor
Tanmay is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! Currently, this answer doesn't following our standard rules for input/output. You can easily fix this by adding print to your answer, and you can even reduce the number of bytes you score by switching to Python 2. However, the question states that you should output to 15 sig figs, and your answer, unfortunately, only outputs to 12. I'd recommend deleting your answer, editing it to correct it, then undeleting it. \$\endgroup\$ – caird coinheringaahing 7 hours ago
0
\$\begingroup\$

Julia 0.7, 2 bytes

φ

Try it online!

I'm surprised nobody has posted this yet...

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy