an elegant algorithm related to Ulam's spiral
01-23-2018, 06:04 PM (This post was last modified: 03-09-2019 03:52 PM by Don Shepherd.)
Post: #1
 Don Shepherd Senior Member Posts: 749 Joined: Dec 2013
an elegant algorithm related to Ulam's spiral
Ulam's spiral is a simple structure with a couple of fascinating characteristics.

Code:
 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

The squares of odd numbers (1, 9, 25, 49) follow a diagonal path to the lower right; squares of even numbers (4, 16, 36, 64) follow a similar pattern to the upper left. There are many diagonal paths that include prime numbers. These are easily visible when you extend the spiral to higher values.

There is a very elegant algorithm for determining the non-trivial neighbors of a selected number that was shown to me by member Allen Thomson several years ago. For example, the non-trivial neighbors of number 22 are 7 and 45 (21 and 23 would be trivial neighbors of 22).

I have implemented this algorithm on the HP-12c, HP-17b solver, and the HP-65.

Here is the 17b solver solution. Enter a value for N and solve for ANS1 and ANS2.

Code:
 ULAM:0xL(A:2xIP(SQRT(4xN-2)))+ IF(S(ANS1):IF(2xIP(SQRT(4xN-6))<G(A): N+G(A)+3:N-G(A)+3)-ANS1: N+G(A)+5-ANS2)

Here is the solution for the 12c. Enter a value and press R/S. One solution will be displayed. Press X<-->Y for the second solution.

Code:
 01 STO 2 02 4 03 X 04 2 05 - 06 SQRT 07 INTG 08 2 09 X 10 STO 1 11 RCL 2 12 + 13 5 14 + 15 STO 3 16 RCL 1 17 RCL 2 18 4 19 X 20 6 21 - 22 SQRT 23 INTG 24 2 25 X 26 X<=Y 27 GOTO 35 28 RCL 2 29 RCL 1 30 - 31 3 32 + 33 RCL 3 34 GOTO 00 35 - 36 X=0 37 GOTO 28 38 RCL 2 39 RCL 1 40 + 41 GOTO 31

edited on 3/9/2019 to add HP-65 solution

Here is the solution for the HP-65. Enter the value you want to check and press A. Then press B and C for the two answers.

Code:
 01 LBL A 03 STO 1 04 4 05 x 06 2 07 - 08 SQRT 10 INT 12 2 13 x 14 STO 2 15 RTN 16 LBL B 18 RCL 1 19 RCL 2 20 + 21 5 22 + 23 RTN 24 LBL C 26 RCL 1 27 4 28 x 29 6 30 - 31 SQRT 33 INT 35 2 36 x 37 RCL 2 38 X>Y 39 GOTO 1 41 RCL 1 42 RCL 2 43 - 44 3 45 + 46 RTN 47 LBL 1 49 RCL 1 50 RCL 2 51 + 52 3 53 + 54 RTN
 « Next Oldest | Next Newest »

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