<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Collatz Conjecture on kenji.blog</title><link>http://kenji.blog/en/tags/collatz-conjecture/</link><description>Recent content in Collatz Conjecture on kenji.blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>kenjinote</copyright><lastBuildDate>Tue, 15 Jul 2025 18:03:03 +0900</lastBuildDate><atom:link href="http://kenji.blog/en/tags/collatz-conjecture/index.xml" rel="self" type="application/rss+xml"/><item><title>Collatz Conjecture</title><link>http://kenji.blog/en/p/%E3%82%B3%E3%83%A9%E3%83%83%E3%83%84%E4%BA%88%E6%83%B3/</link><pubDate>Tue, 15 Jul 2025 18:03:03 +0900</pubDate><guid>http://kenji.blog/en/p/%E3%82%B3%E3%83%A9%E3%83%83%E3%83%84%E4%BA%88%E6%83%B3/</guid><description>&lt;img src="http://kenji.blog/p/%E3%82%B3%E3%83%A9%E3%83%83%E3%83%84%E4%BA%88%E6%83%B3/img.png" alt="Featured image of post Collatz Conjecture" />&lt;h1 id="is-it-true-that-any-number-eventually-becomes-1--playing-with-the-collatz-conjecture">Is it true that &amp;ldquo;any number eventually becomes 1&amp;rdquo;? ── Playing with the Collatz Conjecture
&lt;/h1>&lt;p>Hello! I&amp;rsquo;m kenji.&lt;/p>
&lt;p>Suddenly, but if you hear &amp;ldquo;a rule where any number eventually becomes 1&amp;rdquo;,
isn&amp;rsquo;t it a bit mysterious?&lt;/p>
&lt;blockquote>
&lt;p>For example, 19, or 87, or even 1000000.
If you tweak the numbers according to appropriate rules, for some reason it converges to &amp;ldquo;1&amp;rdquo; at the end.&lt;/p>
&lt;/blockquote>
&lt;p>Such a dream-like story is the &lt;strong>Collatz Conjecture&lt;/strong>.&lt;/p>
&lt;hr>
&lt;h2 id="what-is-the-collatz-conjecture-anyway">What is the Collatz Conjecture anyway?
&lt;/h2>&lt;p>First, let me introduce the rules.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Start: Choose any &lt;strong>positive integer&lt;/strong>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Operation:&lt;/p>
&lt;ul>
&lt;li>If it is even → Halve it (n → n / 2)&lt;/li>
&lt;li>If it is odd → Triple it and add 1 (n → 3n + 1)&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>If you repeat this forever, the conjecture says that &lt;strong>any number will eventually reach 1&lt;/strong>.&lt;/p>
&lt;p>For example, starting from &lt;code>6&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>It properly became &amp;ldquo;1&amp;rdquo;. Welcome back!&lt;/p>
&lt;hr>
&lt;h2 id="lets-do-it-in-code-collatz-in-python">Let&amp;rsquo;s do it in code: Collatz in Python
&lt;/h2>&lt;p>Now, in times like this, it&amp;rsquo;s faster to try it in code!
Let&amp;rsquo;s output the &amp;ldquo;Collatz sequence&amp;rdquo; in Python.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">collatz&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">n&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">steps&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">[&lt;/span>&lt;span class="n">n&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">while&lt;/span> &lt;span class="n">n&lt;/span> &lt;span class="o">!=&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">n&lt;/span> &lt;span class="o">%&lt;/span> &lt;span class="mi">2&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">n&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">n&lt;/span> &lt;span class="o">//&lt;/span> &lt;span class="mi">2&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">else&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">n&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">3&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="n">n&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="mi">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">steps&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">append&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">n&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="n">steps&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Example: Starting from 19&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">collatz&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">19&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>When you execute it:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">[19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>It splendidly reaches 1.
Even though it takes quite a detour, it firmly reaches the goal at the end!&lt;/p>
&lt;p>By the way, even if you start from 29, it reaches 1 in the same way.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">collatz&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">29&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>When you execute it:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;span class="lnt">8
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">[27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242,
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350,
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167,
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479,
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911,
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732,
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35,
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Surprisingly, it takes 111 steps!&lt;/p>
&lt;p>Moreover, there are scenes where it balloons to over 9000 along the way.
It&amp;rsquo;s a pattern that takes a huge detour before reaching the goal.&lt;/p>
&lt;hr>
&lt;h2 id="so-whats-amazing-about-it-in-the-end">So, what&amp;rsquo;s amazing about it in the end?
&lt;/h2>&lt;p>What&amp;rsquo;s amazing about this conjecture is,&lt;/p>
&lt;blockquote>
&lt;p>&lt;strong>Even though it hasn&amp;rsquo;t been proven, it seems to become 1 no matter what number you use&lt;/strong>&lt;/p>
&lt;/blockquote>
&lt;p>That&amp;rsquo;s the point.&lt;/p>
&lt;p>Eh? Then, what about 1 trillion, or 10 quadrillion&amp;hellip;?&lt;/p>
&lt;p>If you thought that, you are sharp.
Actually, it has been verified up to about &amp;ldquo;2 to the 68th power&amp;rdquo; using computers,
and &lt;strong>all have reached 1&lt;/strong>. Unbelievable&amp;hellip;&lt;/p>
&lt;p>But, &lt;strong>it hasn&amp;rsquo;t been theoretically proven that &amp;ldquo;it always happens&amp;rdquo;&lt;/strong>.
This is what they call an &amp;ldquo;unsolved problem&amp;rdquo; in the world of mathematics.&lt;/p>
&lt;hr>
&lt;h2 id="who-is-mr-collatz">Who is Mr. Collatz?
&lt;/h2>&lt;p>So, reading this far, you might wonder &amp;ldquo;who is Collatz anyway?&amp;rdquo;.
Let me introduce him properly!&lt;/p>
&lt;ul>
&lt;li>Name: &lt;strong>Lothar Collatz&lt;/strong>&lt;/li>
&lt;li>Nationality: Germany&lt;/li>
&lt;li>Year of birth: 1910 - 1990&lt;/li>
&lt;li>Title: Mathematician (Active in the fields of functional analysis and number theory)&lt;/li>
&lt;/ul>
&lt;p>He proposed this conjecture in 1937,
and since then, for over 80 years, &lt;strong>no one has been able to prove or disprove it&lt;/strong>.&lt;/p>
&lt;p>By the way, this problem is so simple yet so deep that
even Paul Erdős (a super famous mathematician) is said to have said this:&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;Mathematics may not be ready for such problems.&amp;rdquo;&lt;/p>
&lt;/blockquote>
&lt;p>In other words, the theory that human mathematics hasn&amp;rsquo;t caught up with this mystery yet&amp;hellip;&lt;/p>
&lt;hr>
&lt;h2 id="no-complex-math-formulas-are-necessary">No &amp;ldquo;complex math formulas&amp;rdquo; are necessary
&lt;/h2>&lt;p>The good thing about the Collatz Conjecture is that &lt;strong>anyone can play with it&lt;/strong>.&lt;/p>
&lt;p>You can do it if you have paper and pen.
If you write code in Python, you can test it automatically.
And yet, &lt;strong>cutting-edge mathematicians are seriously challenging it&lt;/strong>.&lt;/p>
&lt;p>Doesn&amp;rsquo;t it make you excited?&lt;/p>
&lt;hr>
&lt;h2 id="bonus-code-to-test-it-all-at-once">Bonus: Code to test it all at once
&lt;/h2>&lt;p>I&amp;rsquo;ll also include code to test various numbers all at once.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> &lt;span class="n">n&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="nb">range&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">21&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">steps&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">collatz&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">n&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">n&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">: &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">steps&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2"> (Steps: &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="nb">len&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">steps&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">)&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>This outputs the Collatz sequences from &amp;ldquo;1 to 20&amp;rdquo; all at once.&lt;/p>
&lt;hr>
&lt;h2 id="conclusion-this-world-is-indeed-mysterious">Conclusion: This world is indeed mysterious
&lt;/h2>&lt;p>So, that&amp;rsquo;s the Collatz Conjecture.&lt;/p>
&lt;ul>
&lt;li>Even though it&amp;rsquo;s super simple&lt;/li>
&lt;li>No one can prove it&lt;/li>
&lt;li>It&amp;rsquo;s a huge problem in the math community&lt;/li>
&lt;/ul>
&lt;p>It&amp;rsquo;s an existence like a cluster of mysteries.&lt;/p>
&lt;p>Even programming beginners can try it, so please definitely play with it~!&lt;/p>
&lt;hr>
&lt;h2 id="recommended-links-for-interested-people">Recommended Links (For interested people)
&lt;/h2>&lt;ul>
&lt;li>&lt;a class="link" href="https://en.wikipedia.org/wiki/Collatz_conjecture" target="_blank" rel="noopener"
>Wikipedia: Collatz conjecture&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://arxiv.org/abs/1909.03562" target="_blank" rel="noopener"
>Terence Tao Paper (English)&lt;/a>&lt;/li>
&lt;li>It&amp;rsquo;s also fun to try making a visualizer in Python! (I&amp;rsquo;ll make one if there&amp;rsquo;s a request)&lt;/li>
&lt;/ul>
&lt;hr>
&lt;p>If you want to know more about this kind of &amp;ldquo;mysterious math x programming&amp;rdquo; topics,
please feel free to request &amp;ldquo;tell me more&amp;rdquo;.
Eventually, I&amp;rsquo;ll introduce various things like the Riemann hypothesis and prime numbers!&lt;/p>
&lt;hr>
&lt;p>📮 The End!&lt;/p>
&lt;hr></description></item></channel></rss>