{"id":5053,"date":"2017-03-19T19:22:53","date_gmt":"2017-03-19T18:22:53","guid":{"rendered":"http:\/\/www.valcon.it\/python\/?p=5053"},"modified":"2025-10-06T13:53:49","modified_gmt":"2025-10-06T11:53:49","slug":"congettura-di-goldbach","status":"publish","type":"post","link":"https:\/\/www.valcon.it\/python\/congettura-di-goldbach\/","title":{"rendered":"Congettura di Goldbach"},"content":{"rendered":"\n<p><em>Corso Online per Potenziare le Competenze Digitali<\/em><\/p>\n\n\n\n<p>La <em>congettura di Goldbach<\/em> afferma che: ogni numero pari (<em>maggiore di 2<\/em>) \u00e8 ottenibile&nbsp;come somma di due numeri primi.<br>Per esempio, <strong>16<\/strong> \u00e8 la somma di <strong>11<\/strong> e&nbsp;<strong>5<\/strong>, mentre <strong>20<\/strong> \u00e8 la somma di <strong>17<\/strong> e <strong>3<\/strong>.<br>Il vostro compito \u00e8 quello di scrivere&nbsp;un programma che, letto un numero pari, stampi due numeri primi la cui&nbsp;somma corrisponda al numero letto.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Esempi di input\/output<\/h3>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\"><\/th><th>Input<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">1\u00b0<\/td><td>16<\/td><td>3 13<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><\/td><td><\/td><td>5 11<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2\u00b0<\/td><td>20<\/td><td>3 17<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"><\/td><td><\/td><td>7 13<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Funzione <strong>primo<\/strong>():<\/p>\n\n\n\n<ul>\n<li>se esiste <strong>y<\/strong> in [2, <strong>x<\/strong>-1] che divide <strong>x<\/strong> allora <strong>x<\/strong> non \u00e8 primo<\/li>\n\n\n\n<li>se non esiste <strong>y<\/strong> &#8230; allora <strong>x<\/strong> \u00e8 primo<\/li>\n<\/ul>\n\n\n\n<p>Main:<\/p>\n\n\n\n<ul>\n<li>la prima coppia \u00e8 (2,&nbsp;<strong>n<\/strong>-2)<\/li>\n\n\n\n<li>il ciclo while scorre tutte le coppie (<strong>n1<\/strong>,&nbsp;<strong>n2<\/strong>) con&nbsp;<strong>n1<\/strong>+<strong>n2<\/strong>=<strong>n<\/strong> e&nbsp;<strong>n1<\/strong> &lt;= <strong>n2<\/strong><\/li>\n\n\n\n<li>se <strong>n1<\/strong> \u00e8 primo e <strong>n2<\/strong> \u00e8 primo allora stampa la coppia<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef primo(x):\n    for y in range(2, x):\n        if(x%y == 0):\n            return False\n    return True\n\nn  = 20  # pari e maggiore di 2\nn1 = 2\nn2 = n-2\nwhile(n1 &lt;= n2):\n    if(primo(n1) and primo(n2)):\n        print(n1, n2)\n    n1 += 1\n    n2 -= 1\n<\/pre><\/div>\n\n\n<p>Risultato<\/p>\n\n\n\n<p><code>3 17<br>7 13<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Il ciclo <strong>while<\/strong> si interrompe, con <strong>break<\/strong>, quando viene individuata la prima coppia di numeri primi<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n...\nwhile(n1 &lt;= n2):\n    if(primo(n1) and primo(n2)):\n        print(n1, n2)\n        break\n    n1 += 1\n    n2 -= 1\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>La struttura del ciclo while pu\u00f2 essere semplificata?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n...\nwhile(n1 &lt;= n2) and (not primo(n1)) or (not primo(n2)):\n    n1 += 1\n    n2 -= 1\nif(n1 &lt;= n2):\n    print(n1, n2)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Se la <em>congettura<\/em> \u00e8 vera&#8230;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n...\nwhile(not primo(n1)) or (not primo(n2)):\n    n1 += 1\n    n2 -= 1\nprint(n1, n2)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Si pu\u00f2 rendere ancora pi\u00f9 veloce&#8230;<\/p>\n\n\n\n<ol>\n<li>Dopo aver controllato il <strong>2<\/strong>, scorrere solo i <strong>numeri dispari<\/strong><\/li>\n\n\n\n<li><em>Crivello di Eratostene<\/em><\/li>\n\n\n\n<li>&#8230;<\/li>\n\n\n\n<li>L&#8217;argomento \u00e8 tra i pi\u00f9 studiati della matematica!<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Corso Online per Potenziare le Competenze Digitali La congettura di Goldbach afferma che: ogni numero pari (maggiore di 2) \u00e8 ottenibile&nbsp;come somma di due numeri primi.Per esempio, 16 \u00e8 la somma di 11 e&nbsp;5, mentre 20 \u00e8 la somma di 17 e 3.Il vostro compito \u00e8 quello di scrivere&nbsp;un programma che, letto un numero pari, &#8230; <a title=\"Congettura di Goldbach\" class=\"read-more\" href=\"https:\/\/www.valcon.it\/python\/congettura-di-goldbach\/\" aria-label=\"Leggi di pi\u00f9 su Congettura di Goldbach\">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":[],"_links":{"self":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/5053"}],"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=5053"}],"version-history":[{"count":53,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/5053\/revisions"}],"predecessor-version":[{"id":29671,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/5053\/revisions\/29671"}],"wp:attachment":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/media?parent=5053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/categories?post=5053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/tags?post=5053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}