Ulam spiral – non-trivial neighbors of a generic element (HP-12C)
01-25-2018, 05:17 PM (This post was last modified: 01-25-2018 11:37 PM by Gerson W. Barbosa.)
Post: #1
 Gerson W. Barbosa Senior Member Posts: 1,199 Joined: Dec 2013
Ulam spiral – non-trivial neighbors of a generic element (HP-12C)
Ulam spiral

64 63 62 61 60 59 58 57
37 36 35 34 33 32 31 56
38 17 16 15 14 13 30 55
39 18 05 04 03 12 29 54
40 19 06 01 02 11 28 53
41 20 07 08 09 10 27 52
42 21 22 23 24 25 26 51
43 44 45 46 47 48 49 50

Code:
 01 ENTER  02 ENTER  03 ENTER  04 4 05 * 06 2 07 - 08 √x 09 INTG 10 ENTER  11 + 12 + 13 5 14 + 15 x<>y 16 1 17 - 18 √x 19 FRAC 20 x=0 21 GTO 42 22 Rv 23 x<>y 24 4 25 * 26 3 27 - 28 √x 29 FRAC 30 x=0 31 GTO 42 32 Rv  33 x<>y 34 ENTER  35 + 36 8 37 + 38 x<>y 39 - 40 LASTx 41 GTO 00 42 Rv  43 ENTER  44 ENTER  45 2 46 - 47 GTO 00

Examples:

22 R/S -> 45
x<>y -> 7

31 R/S -> 56
x<>y -> 58

n1 = 5 + n + 2*INT(√(4*n - 2)) (This formula from Don Shepherd’s recent article on the same subject).

For elements on the main diagonals, where either (n - 1) or (4*n - 3) are perfect squares,

n2 = n1 - 2

Otherwise

n2 = 2*n + 8 - n1

The central element, 1, has three non-trivial neighbors: 4, 6 and 8. The program will return 6 and 8.

Code-optimization is left as an exercise.

Edited to fix a typo in step 04.
01-25-2018, 05:27 PM (This post was last modified: 01-25-2018 05:50 PM by Don Shepherd.)
Post: #2
 Don Shepherd Senior Member Posts: 627 Joined: Dec 2013
RE: Ulam spiral – non-trivial neighbors of a generic element (HP-12C)
Thanks Gerson. Yes, I knew my programs get a sqrt(neg number) error for n=1.
01-26-2018, 05:25 AM
Post: #3
 Don Shepherd Senior Member Posts: 627 Joined: Dec 2013
RE: Ulam spiral – non-trivial neighbors of a generic element (HP-12C)
I just noticed something interesting. The two non-trivial neighbors of an odd number are always even numbers, and the two non-trivial neighbors of an even number are always odd numbers. The same is true of trivial neighbors, but that I would naturally expect. I suppose this is due to the natural structure of the spiral.
01-27-2018, 03:26 AM (This post was last modified: 01-27-2018 04:09 AM by Gerson W. Barbosa.)
Post: #4
 Gerson W. Barbosa Senior Member Posts: 1,199 Joined: Dec 2013
RE: Ulam spiral – non-trivial neighbors of a generic element (HP-12C)
(01-25-2018 05:27 PM)Don Shepherd Wrote:  Yes, I knew my programs get a sqrt(neg number) error for n=1.

Don, I did notice that error, but it was not my intention to fix it. I was just interested in knowing the formulas you have used. After analyzing the first few lines of your 12C program and getting the formula for the first non-trivial neighbor I got lazy and rather than trying to figure out the formula for the second one I decided to find a relationship between them. That was not hard, but probably a straightforward conversion of your program to RPL would have yielded better byte counts. Each of the following is 117.5 bytes long:

« DUP 5 + OVER 4 * 2 - √ IP DUP + + SWAP DUP 4 * 3 - √ FP OVER 1 - √ FP * NOT { DROP DUP 2 - } { 4 + DUP + OVER - } IFTE
»

« DUP 5 + OVER 4 * 2 - √ IP DUP + + 2 OVER - ROT DUP 4 * 3 - √ FP OVER 1 - √ FP * NOT { DROP NEG } { 3 + DUP + + } IFTE
»

« DUP 5 + OVER 4 * 2 - √ IP DUP + + DUP 2 - ROT DUP 4 * 3 - √ FP OVER 1 - √ FP * NOT { DROP } { 3 + DUP + SWAP - } IFTE
»
01-27-2018, 08:06 AM
Post: #5
 Don Shepherd Senior Member Posts: 627 Joined: Dec 2013
RE: Ulam spiral – non-trivial neighbors of a generic element (HP-12C)
(01-27-2018 03:26 AM)Gerson W. Barbosa Wrote:
(01-25-2018 05:27 PM)Don Shepherd Wrote:  Yes, I knew my programs get a sqrt(neg number) error for n=1.

Don, I did notice that error, but it was not my intention to fix it. I was just interested in knowing the formulas you have used. After analyzing the first few lines of your 12C program and getting the formula for the first non-trivial neighbor I got lazy and rather than trying to figure out the formula for the second one I decided to find a relationship between them. That was not hard, but probably a straightforward conversion of your program to RPL would have yielded better byte counts. Each of the following is 117.5 bytes long:

« DUP 5 + OVER 4 * 2 - √ IP DUP + + SWAP DUP 4 * 3 - √ FP OVER 1 - √ FP * NOT { DROP DUP 2 - } { 4 + DUP + OVER - } IFTE
»

« DUP 5 + OVER 4 * 2 - √ IP DUP + + 2 OVER - ROT DUP 4 * 3 - √ FP OVER 1 - √ FP * NOT { DROP NEG } { 3 + DUP + + } IFTE
»

« DUP 5 + OVER 4 * 2 - √ IP DUP + + DUP 2 - ROT DUP 4 * 3 - √ FP OVER 1 - √ FP * NOT { DROP } { 3 + DUP + SWAP - } IFTE
»
Thanks Gerson.

I continue to be amazed at what can be accomplished with RPL. You have my deepest respect.

Don
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)