Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Dec 12th, 2004, 3:59 PM   #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
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!
lostcauz is offline   Reply With Quote
Old Dec 13th, 2004, 2:11 AM   #2
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
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
kurifu is offline   Reply With Quote
Reply

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 9:58 PM.

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