Created
September 2, 2011 02:21
-
-
Save kusold/1187795 to your computer and use it in GitHub Desktop.
Project Euler #14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| def collatz(n): | |
| length = 0 | |
| i = n | |
| while( i > 1): | |
| if(i%2 == 0): | |
| i /= 2 | |
| else: | |
| i = 3*i + 1 | |
| length += 1 | |
| return length | |
| def main(): | |
| start = time.time() | |
| count = 0 | |
| highestcount = 0 | |
| for x in xrange(1, 1000001): | |
| if(x%2 != 0): | |
| count = collatz(x) | |
| if(count > highestcount): | |
| highest = x | |
| highestcount = count | |
| #print("New Highest: " + str(highest)) | |
| executiontime = (time.time() - start) * 1000 | |
| print "Elapsed Time:", (time.time() - start) * 1000, "millisecs" | |
| print highestcount | |
| return executiontime | |
| i = 0 | |
| avgtime = 0 | |
| while (i<10): | |
| avgtime += main() | |
| i += 1 | |
| print "Average Time:", (avgtime/i), "millisecs" | |
| a=raw_input('Press return to continue') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment