![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4
![]() |
In a numerical spiral such as below the question was given to determine the sum of both diagonals in a 1001 by 1001 spiral.
73 74 75 76 77 78 79 80 81 72 43 44 45 46 47 48 49 50 71 42 21 22 23 24 25 26 51 70 41 20 7 8 9 10 27 52 69 40 19 6 1 2 11 28 53 68 39 18 5 4 3 12 29 54 67 38 17 16 15 14 13 30 55 66 37 36 35 34 33 32 31 56 65 64 63 62 61 60 59 58 57 I'm curious what algorithm others would have chosen. Mine follows. I noticed a pattern with the diagonal numbers - every 4th iteration add 2 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8 1,3,5,7,9,13,17,21,25,31,37,43,49,57,65,73,81 mov ecx, 1 ; first diagonal spiral number xor ebx, ebx ; determines distance to next diagonal mov eax, ecx ; begin accumulating sum inner: add ebx, 2 ; update distance between diagonals mov edx, 4 ; will use distancefor 4 iterations outer: add ecx, ebx ; go to next diagonal cmp ecx, 1002001; is it the last one? ja getout ; yes, go away add eax, ecx ; no, add to sum dec edx ; decrement inner loop counter jz inner ; if zero- reset counter jmp outer ; next number getout:
__________________
-- lostcauz Stepped in what?... Behind whose barn?... I didn't even know they had a cow! |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|