The Collatz Conjecture is a famous problem in number theory. It corresponds to the following function:
f ( n ) = { 2 n 3 n + 1 if n ≡ 0 ( m o d 2 ) if n ≡ 1 ( m o d 2 )
The conjecture states that for every n > 1 there exists a k such that f k ( n ) = 1 . In other words, given an n to start with, after iterating this function over and over again we will eventually get 1 , no matter the starting number.
How many 1 < n < 1 0 7 are there such that k is odd?
Note: We are looking for k to be minimal. There are technically infinitely many k because f ( 1 ) = 4 , f ( 4 ) = 2 and f ( 2 ) = 1 .
Clarification : f k ( n ) = k number of times f ( f ( f ( … ( f ( x ) ) … ) .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Very intuitive, I really admire your approach.
Ah! Good use of dynamic programming! Upvoted! :)
Just for the sake of variety, let me add my brute-force and bashy C++ solution here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Runs for about 26 seconds.
Here
's the full code in Ideone which cannot be executed in one run, so I run it twice by modifying the
for
loop in the code (one time for
2
≤
n
<
2
1
0
7
and the second time for
2
1
0
7
≤
n
<
1
0
7
).
Output:
Hence, our answer is the sum 2 4 9 7 0 4 7 + 2 5 0 9 3 8 7 = 5 0 0 6 4 3 4 .
Seeing your solution, I think I'll try to improve my original code later to reduce runtime and do the work in a single run.
For some weird reasons, my python code slows down exponentially when I use memorization.
Log in to reply
While I can't see your code, I'm guessing that when your code is finished it has saved every variable from 1 to 1 0 7 exclusive, but note that it's not exactly necessary to have every variable on hand at all times. Save a few key ones and as you work your way up, find a way to intelligently save and delete the variables you need and don't need.
Log in to reply
Yes. You're right. I am using external library for memorization. I'll try to improve that. I'll post my code later (I'm in my school right now).
Log in to reply
@Arulx Z – Haha I think it's so funny that you're in school right now and I'm just about to go to bed!
The same approche as Daniel Branscombe , in python 3.4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Problem Loading...
Note Loading...
Set Loading...
memorization is the key here, since in order to calculate the length for a given n you have to calculate the lengths for several other numbers along the way it is best to remember all of those intermediary lengths. This can be handled very easily in Mathematica
f[n_] := f[n] = If[n == 1, 0, If[Mod[n, 2] == 0, 1 + f[n/2], 1 + f[3 n + 1]]];
by defining a function f[x_]:=f[x]=....
this tells Mathematica to store in memory the value calculated anytime the function is calculated. Thus when revisiting an already calculated number it simply gets the value from memory.
As an explicit example, when calculating f[5] we get
f[5]=1+f[16]
f[16]=1+f[8]
f[8]=1+f[4]
f[4]=1+f[2]
f[2]=1+f[1]
f[1]=0
after calculating f[5], we now have in memory the values for f[16],f[8],f[4],f[2],f[1] and so if we were to calculate f[8] it would simply grab it from memory.