Basic Optimization - Collatz Sum Sequence
Description:
The Collatz Conjecture
I'm pretty sure you know about this famous conjecture already. Given this recursively defined function
the task is to decide whether or not a given starting number will ever reach one if f is iterated enough. These iterations give us a sequence with a certain length that ends when one is finally reached:
10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
when starting with 10, the length of the sequence is 6
(the one is excluded)
The conjecture says that every natural number will eventually reach one, but whether or not this is true is an open question. We will assume it is true for the purposes of this kata.
A simple sum sequence
Let's define a sequence using the previous function!
Given a number n, we define l(n)
as the length of the sequence given by the iteration of the collatz function above. Then, we define collatz_sum(n)
as the sum of all l(i) for all i in the range [1, n]
.
Your task is to define the collatz_sum
function and to make it as fast as possible.
Technical notes
The validation function takes approximately 5 seconds to execute in order to force your solution to be approximately as fast as mine (which is not very hard).
All the inputs are in the range [1, 2*10^6]. The exact test ranges are given when the tests are executed. Most tests are random.
Similar Kata:
Stats:
Created | Feb 13, 2020 |
Published | Feb 13, 2020 |
Warriors Trained | 288 |
Total Skips | 85 |
Total Code Submissions | 242 |
Total Times Completed | 43 |
Python Completions | 43 |
Total Stars | 9 |
% of votes with a positive feedback rating | 85% of 24 |
Total "Very Satisfied" Votes | 18 |
Total "Somewhat Satisfied" Votes | 5 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 19 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 7 kyu |