![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4
![]() |
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
__________________
-- lostcauz Stepped in what?... Behind whose barn?... I didn't even know they had a cow! |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Looks good... no real suggestions myself
![]()
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|