Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Dec 16th, 2004, 4:38 AM   #1
lostcauz
Hobbyist Programmer
 
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4 lostcauz is on a distinguished road
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
So I set an inner loop counter at 4 and incremented a reg by 2 each time.
 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!
lostcauz is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC