{"id":4462,"date":"2017-03-04T13:01:55","date_gmt":"2017-03-04T12:01:55","guid":{"rendered":"http:\/\/www.valcon.it\/python\/?p=4462"},"modified":"2026-10-14T21:21:19","modified_gmt":"2026-10-14T19:21:19","slug":"congettura-di-collatz","status":"publish","type":"post","link":"https:\/\/www.valcon.it\/python\/congettura-di-collatz\/","title":{"rendered":"Congettura di Collatz"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.valcon.it\/pp\/congettura-di-collatz\/\">Vedi la discussione<\/a><\/p>\n\n\n\n<p>Quanti passi sono necessari per arrivare a 1? <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nn = int(input(&quot;n = &quot;))\n \nlunghezza = 1\nwhile(n != 1):\n    lunghezza += 1   # conta un nuovo passo\n\n    if(n%2 == 0):    # se n \u00e8 pari si divide per 2\n        n = n \/\/ 2\n    else:            # se n \u00e8 dispari...\n        n = 3*n + 1\n\nprint(lunghezza)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">2<\/h2>\n\n\n\n<p>Facoltativo: visualizza la sequenza da n a 1, compresi<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nn = int(input(&quot;n = &quot;))\n\nprint(n, end=' ')       # Visualizza il numero di partenza\nlunghezza = 1\nwhile(n != 1):\n    lunghezza += 1\n\n    if(n%2 == 0):\n        n = n \/\/ 2\n    else:\n        n = 3*n + 1\n\n    print(n, end=' ')  # visualizza il risultato dell'algoritmo\n\nprint()\nprint(lunghezza)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Versione OII<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nf = open(&quot;input.txt&quot;, &quot;r&quot;)\nn = int(f.read())\nf.close()\n\nlunghezza = 1;\nwhile(n != 1):\n    lunghezza += 1\n\n    if(n%2 == 0):\n        n = n \/\/ 2\n    else:\n        n = 3*n + 1\n \nf = open(&quot;output.txt&quot;, &quot;w&quot;)\nf.write(str(lunghezza))\nf.close()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Problema: da 10 a 20<\/h2>\n\n\n\n<p>NUM \u00e8 il numero da 10 a 20 con lunghezza minima LUN<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef lunghezza(n):\n    conto = 1\n    while(n != 1):\n        conto += 1\n        if(n%2 == 0):\n            n = n \/\/ 2\n        else: \n            n = 3*n + 1\n    return conto\n\nPRIMO  = 10\nULTIMO = 20\nNUM    = 0\nLUN    = 1000  # esagerato...\n \nfor num in range(PRIMO, ULTIMO+1):\n    lun = lunghezza(num)\n    if(lun &lt; LUN):\n        NUM = num\n        LUN = lun\n    print(num,lun)  # facoltativo\n \nprint(NUM, ':', LUN)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">matplotlib<\/h2>\n\n\n\n<p>La sequenza per n=<strong>167<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"http:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_167.png\" alt=\"\" class=\"wp-image-10951\" srcset=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_167.png 640w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_167-150x113.png 150w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_167-360x270.png 360w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\n  \nn = 167\nN = &#x5B;]\nN.append(n)\nwhile(n != 1):\n    if(n%2 == 0):\n        n = n \/\/ 2\n    else: \n        n = 3*n + 1\n    N.append(n)\n \nplt.grid(which=&quot;major&quot;)\nplt.plot(N)\nplt.title(&quot;Congettura di Collatz, n=&quot; + str(n))\nplt.show()\n<\/pre><\/div>\n\n\n<p>Lunghezze per n da 10 a 20<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"http:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_10_20.png\" alt=\"\" class=\"wp-image-10953\" srcset=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_10_20.png 640w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_10_20-150x113.png 150w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_10_20-360x270.png 360w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Problema: da 48 a 52<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/oii-2021-02-23-18.png\" alt=\"\" class=\"wp-image-13655\" srcset=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/oii-2021-02-23-18.png 640w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/oii-2021-02-23-18-360x270.png 360w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/oii-2021-02-23-18-150x113.png 150w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport math\n  \ndef lunghezza(n): \n    conto = 1\n    while(n != 1): \n        if(n%2 == 0): \n            n = n \/\/ 2\n        else: \n            n = 3*n + 1\n        conto += 1\n    return conto \n \nPRIMO  = 10  # 48\nULTIMO = 20  # 52\n \nx = range(PRIMO,ULTIMO+1)\ny = &#x5B;]\n \nNUM = 0\nLUN = math.inf\nfor num in x:\n    lun = lunghezza(num)\n    print(num, lun)\n    y.append(lun)\n    if(lun &lt; LUN):\n        NUM = num\n        LUN = lun        \n \nprint(&quot;--&gt;&quot;, NUM, LUN)\n     \nplt.bar(x, y)\nplt.title(&quot;Congettura di Collatz, n=10..20&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Problema: da 1 a 100 (1000)<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"http:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_1_100.png\" alt=\"\" class=\"wp-image-10955\" srcset=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_1_100.png 640w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_1_100-150x113.png 150w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/plt_collatz_1_100-360x270.png 360w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1034\" height=\"480\" src=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/collatz_1_1000.png\" alt=\"\" class=\"wp-image-31550\" srcset=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/collatz_1_1000.png 1034w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/collatz_1_1000-360x167.png 360w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/collatz_1_1000-1024x475.png 1024w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/collatz_1_1000-150x70.png 150w\" sizes=\"(max-width: 1034px) 100vw, 1034px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\n  \ndef lunghezza(n):\n    conto = 1\n    while(n != 1):\n        if(n%2 == 0):\n            n = n \/\/ 2\n        else: \n            n = 3*n + 1\n        conto += 1\n    return conto\n \nPRIMO  = 1\nULTIMO = 100                # 1000 \nx = range(PRIMO, ULTIMO+1)\ny = &#x5B;]\nfor n in x:\n    lun = lunghezza(n)\n    print(n, lun)           # facoltativo\n    y.append(lun)\n     \nplt.bar(x, y)\nplt.title(&quot;Congettura di Collatz, n=&quot; + str(PRIMO) + &quot;..&quot; + str(ULTIMO))\nplt.show()\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Ripeti tutti gli esercizi ma analizza l&#8217;altezza massima delle sequenze<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vedi la discussione Quanti passi sono necessari per arrivare a 1? 1 2 Facoltativo: visualizza la sequenza da n a 1, compresi Versione OII Problema: da 10 a 20 NUM \u00e8 il numero da 10 a 20 con lunghezza minima LUN matplotlib La sequenza per n=167 Lunghezze per n da 10 a 20 Problema: da &#8230; <a title=\"Congettura di Collatz\" class=\"read-more\" href=\"https:\/\/www.valcon.it\/python\/congettura-di-collatz\/\" aria-label=\"Leggi di pi\u00f9 su Congettura di Collatz\">Leggi tutto<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[32],"_links":{"self":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/4462"}],"collection":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/comments?post=4462"}],"version-history":[{"count":70,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/4462\/revisions"}],"predecessor-version":[{"id":31551,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/4462\/revisions\/31551"}],"wp:attachment":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/media?parent=4462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/categories?post=4462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/tags?post=4462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}