Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def countCollageTerms(n):
- # Counter = 1, because we also count the first number "n" in the chain
- counter = 1
- while n != 1:
- if n % 2 == 0:
- n /= 2
- counter += 1
- else:
- n = 3 * n + 1
- counter += 1
- return counter
- # MAIN FUNCTION
- from timeit import default_timer as timer
- LIMIT = 10**3
- collatz = 0
- maxSequenceChain = 0
- start = timer()
- for n in range(LIMIT):
- value = countCollageTerms(n)
- if value > maxSequenceChain:
- collatz = n
- maxSequenceChain = value
- end = timer()
- print()
- print("******** EXECUTION TIME UNTIL " + str(LIMIT) + " = " + str(1000*(end-start)) + "ms ********")
- print("From 1 to " + str(LIMIT) + " the number with the longest Collatz sequence is: " + str(collatz) + ". It contains " + str(maxSequenceChain) + " numbers.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement