0

Collatz Conjecture - which is more efficient?

I was given this challenge: Get a starting number, then form a sequence by these rules: 1. If the number is even, divide it by two 2. If the number is odd, multiply it by 3 and add 1 3. Continue sequence until you reach 1 and and at that point, state that you've reached one and give how many steps it took. Without looking at the sample first, I came up with something quite a bit different but the results are identical. I know this is a small code and really, it won't matter, but I think it is important to learn early on which codes are cleaner, more efficient, and would run faster. I'd appreciate any input those with more experience would like to give. Version 1: https://code.sololearn.com/cPIYc3MhZqHc/?ref=app Version 2: https://code.sololearn.com/crCRAPAhVXJ8/?ref=app

6th Nov 2019, 2:49 PM
ZIAMI
ZIAMI - avatar
2 Antworten
+ 1
There are some ways to measure this. Look for example into time.perf_counter or time.timeit. You can run both algos a big number of times and see which took longer.
6th Nov 2019, 6:44 PM
HonFu
HonFu - avatar
- 1
I think that both variants are good, and it won't give such big difference in speed
6th Nov 2019, 3:04 PM
Asman-H
Asman-H - avatar