an elegant algorithm related to Ulam's spiral
01-23-2018, 06:04 PM
Post: #1
 Don Shepherd Senior Member Posts: 547 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 and HP-17b solver.

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
 « Next Oldest | Next Newest »

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