Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   Collatz Problem (http://www.programmingforums.org/showthread.php?t=1517)

lostcauz Dec 12th, 2004 3:59 PM

If n is even divide by 2, if odd multiply 3 add 1.
Testing for longest chain less than one million.
Any suggestions to speed it up? 4.0504s on 233/128
:

xor edi, edi      ; zero -longest chain           
 xor esi, esi      ; zero -inner loop counter     
 mov ecx, 1000000    ; set main loop counter       
 mov edx, ecx      ; copy to edx
begin:
 mov eax, edx      ; prepare for even/odd test
 and eax, 1      ; test even/odd number
 jz inner        ; if zero number is even
 lea eax, [edx*2+edx+1] ; number is odd perform 3n+1
 mov edx, eax      ; load result back to edx
inner:         
 shr edx, 1      ; numbers are all even now so div by 2
 inc esi        ; increment present number chain
 cmp edx, 1      ; if number is 1 we are done with it
 jne begin      ; continue chain
 cmp esi, edi      ; if chain > longest_chain
 jb outer        ; no, goto outer
 mov edi, esi      ; yes, update longest_chain
 mov ebx, ecx      ; save number having longest chain
outer:
 xor esi, esi      ; reset inner loop counter
 dec ecx        ; decrement outer loop counter
 mov edx, ecx      ; save as next number to iterate
 jnz begin      ; if outer loop counter > 0 continue
 mov eax, ebx      ; return final result


kurifu Dec 13th, 2004 2:11 AM

Looks good... no real suggestions myself :P


All times are GMT -5. The time now is 3:52 PM.

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