# HP Forums

Full Version: A simple 11c/15c mini-challenge (phi)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

The goal is to obtain the golden ratio phi=1.618033989 using the least number of programming steps. There are at least two four-step solutions (LBL and RTN don't count).

Gerson.

We can get a good approximation with:

```4
PI
/
x^2
```

A solution with 4 steps excluding LBL that requires manual input before starting program:

```LBL A
1/x
1
+
RTN```

To run:

```1
fA
fA
fA
...
fA```

Eventually, the value converges to phi. This program implements the continued fraction formula:

phi = 1/(1 + 1/(1 + 1/(...)))

Or, an even shorter solution (3 steps):

```LBL A
1
+
SQRT
RTN```

implementing the formula

phi = sqrt(1 + sqrt(1 + ...))

Again, run by pressing fA fA fA ...; this will converge with anything in X that won't cause SQRT to return an error, therefore -1 or anything greater will do.

Or, even simpler (but 5 steps):

```LBL A
3
6
cos
2
x
RTN```

because phi = 2 cos 36 degrees.

Or

```1
ENTER
.5
->P
LSTx
+```

because phi = 0.5 + diagonal of rectangle with sides (1/2, 1), but this requires 7 steps.

Edited: 20 Aug 2009, 3:25 p.m.

```Lbl A
1
+
sqrt
GOTO A
```
and press R/S after running a few seconds. As you mentioned, as long as the stack starts with x>-1 it will converge to phi.

CHUCK

Edited: 20 Aug 2009, 3:29 p.m.

Since:

```1
+
SQRT
```

is no shorter than:

```1
+
1/x
```

I propose the following, which works with all starting values:

```LABEL A
1
+
1/x
GOTO A
RTN
```

press fA, let run for a while, then press R/S. If not converged, or if stopped with 0.6180339898 in the display, press R/S twice in rapid succession.

Quote:
Or

```1
ENTER
.5
->P
LSTx
+```

because phi = 0.5 + diagonal of rectangle with sides (1/2, 1), but this requires 7 steps.

This is interesting but 5 sqrt 1 + 2 / would be one step shorter. The other solutions are also interesting but the one we are looking for is non-iterative. Tip: this one (and its variation) will work on the 11c and the 15c, but will not work on any other Voyager.

An older related mini-challenge by Paul Dale:

Regards,

Gerson.

Something with factorial function perhaps?

HP-11C and HP-15C factorial is actually gamma(n+1) while 12C factorial is just factorial and does not accept decimal values. 16C does not have factorial and I don't know if 10C factorial is gamma or just factorial.

Quote:
I don't know if 10C factorial is gamma or just factorial.

Answer in the Museum's Calculator Comparison Table.

The solution I've found doesn't involve Gamma.

Gerson.

Approximations are welcome. The following is better but requires five steps:

```2
ex
ex
1E03
/
```

Regards,

Gerson.

I can find three command sequences of length four that produce the required answer. Two are basically the same. All are exactly six key presses.

- Pauli

Looks like you found them (and an extra one)! Mine, which are basically the same, are four-step long and requires exactly six key presses as well.

Regards,

Gerson.

Quoting myself:

"The goal is to obtain the golden ratio phi=1.618033989 using the least number of programming steps."

I think your program meets the requirement, even though that was not what I had in mind.

Regards,

Gerson.

Okay, here us the unique one then:

```6
g x^2
2
f ->R
```

Four commands & six keystrokes. It assumes the calculator is in DEG mode so it isn't as good as the other solutions.

I won't spoil the other two :-)

- Pauli

I did not think for a second that that is what you had in mind. I'm sure there is a better way.

Not wanting to spoil the 4-step solutions either, I'll just offer this remarkable HP-15C-specific solution which works in any angular mode:

```    .5
Re<>Im
SIN-1
Re<>Im
ex
```

Best regards from V.

Hello Valentin,

Thanks for participating. This is based on formula (26) in http://mathworld.wolfram.com/GoldenRatio.html. I wonder why its hyperbolic counterpart, which only lacks the imaginary constant, is not there. Well, it appears now I have spoiled it all :-)

Best regards,

Gerson.

Do multi-digit number entries count as only one step?

If so, then

```1.25
sqrt
.5
+
```

Actually, .5 is part of one of the solutions and that alone takes up two steps on the 11c/15c.

Regards,

Gerson.

So is the anwswer:

```.5
sinh-1
ex
```

or:

```2
1/x
sinh-1
ex
```

Either sequence takes me six keystrokes.

...

Here's a two-step program that may or may not produce the desired result, depending on how lucky you get :-)

```RAND
1/x
```

You may have better luck replacing the second step with e^x.

The three-step program

```RAND
1
+
```
should produce the desired result, if iterated enough times.

Please do not consider these as serious attempts at the mini-challenge.

Yes, you've found 'em!. Interesting isn't it?
When googling for "sinh(ln(phi))" I found only six hits. It appears only the German and Polish versions of wikipedia mentions it:

http://pl.wikipedia.org/wiki/Funkcje_hiperboliczne (and now I know what the Polish currency, the zloty, comes from :-)

The equality is easy to prove:

```phi=(sqrt(5)+1)/2
sinh(ln(phi))=(e^ln(phi) - 1/e^ln(phi))/2 = (phi - 1/phi)/2 = 1/2
```

Gerson.

:-)

You only have to find the right seed. See message #9 in the link below:

Strange as it might seem I considered this yesterday. I thought of trying to find a seed that would generate i/phi. But I realized it would take up more than 2 steps, if it existed...

Gerson.

Edited: 21 Aug 2009, 1:51 p.m.

Yes, quite interesting. Thanks for sharing!

I certainly did not find the above references. I looked at equation 26 in the reference of your previous post:

```sin(i ln(phi)) = 0.5 i
```

Then I found a calculator that can do arcsin with a complex argument (my 42s) and took arcsin(0.5i), which is 0+ i0.48121182506. I guess that told me that ln(phi) is equal to 0.48121182506. You had mentioned hyperbolics, so I wondered if arcsinh(0.5i) might go somewhere, which it did not. Then tried arcsinh(0.5) and found it equals 0.48121182506, previously found to be ln(phi), so I was done. So basically, using the clues you gave, I just fiddled around until I got the right answer.

.

Edited: 21 Aug 2009, 4:02 p.m.

Until yesterday I hadn't looked up any reference either. I was trying to simplify some constants so that they took up less steps on the 11C. For instance, I had found 4 LN HYP SIN was equivalent to 1.875, which was a gain (3 steps instead of 5). For other constants I would do the backward sequence: k HYP-1 SIN ex. I don't remember for what reason, since there's no way to get .5 with less than two steps, I did .5 HYP-1 SIN ex. I recognized the number on the display as phi...

The following is somewhat interesting:

It appears the continued fraction form for 1/n+sqrt(1+(1/n)^2), which is equivalent to e^sinh-1(1/n), where n is an integer greater than 1, is always [1; n-1, 1, 1, n-1, 1, 1, n-1, 1, 1, ...]. When n=2, the continued fraction form for the expression will be [1; 1, 1, 1, 1, 1, ...], that is, phi.

Quote:
Not wanting to spoil the 4-step solutions either, I'll just offer this remarkable HP-15C-specific solution which works in any angular mode:
...

This doesn't work if there is a value in the complex X register :-)

- Pauli

My alternate proof of the arcsinh version.

```    arcsinh(x) = ln(x + sqrt(x^2+1))
```

Taking EXP of this gives us:

```    e^arcsinh(x) = x + sqrt(x^2+1)
```

Not let x = 1/2 and substitute into the above:

```    1/2 + sqrt(1/4 + 1)
= 1/2 + sqrt(5/4)
= 1/2 + sqrt(5)/2
= (1 + sqrt(5)) / 2
= phi
```

Now for my alternate solution:

```    36 2 ->RECT in DEG mode
```

The conversion from polar to rectangular coordinates:

```       |  STACK IN   |      STACK OUT
Y |   theta     |    r . SIN(theta)
X |     r       |    r . COS(theta)
```

We don't care about the output in the Y register. So consider the X register output when theta is 36 degrees and r is 2:

```    X = r . COS(theta)
= 2 . COS(36)
```

Now, a quick look into some trigonometric identities / trigonometric tables or Wolfram alpha :-) gives us this interesting result:

```    COS(36) = (1 + sqrt(5)) / 4
```

Finally, substitute this into the above:

```    X = 2 . COS(36)
= 2 . (1 + sqrt(5)) / 4
= (1 + sqrt(5)) / 2
= phi
```

- Pauli

Quote:
This doesn't work if there is a value in the complex X register :-)

This had occurred to me too, but in my tests it always worked. In complex mode, when only the real part is entered (.5) it is assumed the imaginary part is 0 and it will replace whatever value is there. Do you have an example?

Gerson.

Quote:
Now, a quick look into some trigonometric identities / trigonometric tables or Wolfram alpha :-) gives us this interesting result:

```    COS(36) = (1 + sqrt(5)) / 4
```

And 2COS(36) gives

```(1+sqrt(5))/2
1.6180339887498948482...
1/2 + sqrt(5)/2
[1; 1...]
```

but no explicit reference to phi...

On the other hand, 1.6180339887498948482 gives

```Phi+1
(1+sqrt(5))/2
1/Phi, where Phi is the golden ratio conjugate
```

e^ArcSinh(1/2) gives a lot of interesting results though.

Gerson.

Yeah, it is a bit contrived though:

```    22
Re<>Im
CLx
.5
Re<>Im
ASIN
Re<>Im
e[sup]2[/sup]
```

Looks like the 15c's complex mode outsmarted me again :-(

- Pauli

Edited: 22 Aug 2009, 6:30 p.m.