        {"id":770,"date":"2021-09-18T05:00:39","date_gmt":"2021-09-18T03:00:39","guid":{"rendered":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/?p=770"},"modified":"2021-09-14T14:06:43","modified_gmt":"2021-09-14T12:06:43","slug":"suma-de-los-primeros-numeros-naturales","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/suma-de-los-primeros-numeros-naturales\/","title":{"rendered":"Suma de los primeros n\u00fameros naturales"},"content":{"rendered":"<p>Demostrar que la suma de los primeros n\u00fameros naturales<\/p>\n<pre lang=\"text\">\n   0 + 1 + 2 + 3 + \u00b7\u00b7\u00b7 + n\n<\/pre>\n<p>es n \u00d7 (n + 1)\/2<\/p>\n<p>Para ello, completar la siguiente teor\u00eda de Lean:<\/p>\n<pre lang=\"lean\">\nimport data.nat.basic\nimport tactic\nopen nat\n\nvariable (n : \u2115)\n\ndef suma : \u2115 \u2192 \u2115\n| 0     := 0\n| (n+1) := suma n + (n+1)\n\nexample :\n  2 * suma n = n * (n + 1) :=\nsorry\n<\/pre>\n<p>[expand title=\u00bbSoluciones con Lean\u00bb]<\/p>\n<pre lang=\"lean\">\r\nimport data.nat.basic\r\nimport tactic\r\nopen nat\r\n\r\nvariable (n : \u2115)\r\n\r\nset_option pp.structure_projections false\r\n\r\n@[simp]\r\ndef suma : \u2115 \u2192 \u2115\r\n| 0     := 0\r\n| (n+1) := suma n + (n+1)\r\n\r\n-- 1\u00aa demostraci\u00f3n\r\nexample :\r\n  2 * suma n = n * (n + 1) :=\r\nbegin\r\n  induction n with n HI,\r\n  { calc 2 * suma 0\r\n         = 2 * 0       : congr_arg ((*) 2) suma.equations._eqn_1\r\n     ... = 0           : mul_zero 2\r\n     ... = 0 * (0 + 1) : zero_mul (0 + 1), },\r\n  { calc 2 * suma (n + 1)\r\n         = 2 * (suma n + (n + 1))    : congr_arg ((*) 2) (suma.equations._eqn_2 n)\r\n     ... = 2 * suma n + 2 * (n + 1)  : mul_add 2 (suma n) (n + 1)\r\n     ... = n * (n + 1) + 2 * (n + 1) : congr_arg2 (+) HI rfl\r\n     ... = (n + 2) * (n + 1)         : (add_mul n 2 (n + 1)).symm\r\n     ... = (n + 1) * (n + 2)         : mul_comm (n + 2) (n + 1) },\r\nend\r\n\r\n-- 2\u00aa demostraci\u00f3n\r\nexample :\r\n  2 * suma n = n * (n + 1) :=\r\nbegin\r\n  induction n with n HI,\r\n  { calc 2 * suma 0\r\n         = 2 * 0       : rfl\r\n     ... = 0           : rfl\r\n     ... = 0 * (0 + 1) : rfl, },\r\n  { calc 2 * suma (n + 1)\r\n         = 2 * (suma n + (n + 1))    : rfl\r\n     ... = 2 * suma n + 2 * (n + 1)  : by ring\r\n     ... = n * (n + 1) + 2 * (n + 1) : by simp [HI]\r\n     ... = (n + 2) * (n + 1)         : by ring\r\n     ... = (n + 1) * (n + 2)         : by ring, },\r\nend\r\n\r\n-- 3\u00aa demostraci\u00f3n\r\nexample :\r\n  2 * suma n = n * (n + 1) :=\r\nbegin\r\n  induction n with n HI,\r\n  { simp, },\r\n  { calc 2 * suma (n + 1)\r\n         = 2 * (suma n + (n + 1))    : rfl\r\n     ... = 2 * suma n + 2 * (n + 1)  : by ring\r\n     ... = n * (n + 1) + 2 * (n + 1) : by simp [HI]\r\n     ... = (n + 1) * (n + 2)         : by ring, },\r\nend\r\n<\/pre>\n<p>Se puede interactuar con la prueba anterior en <a href=\"https:\/\/leanprover-community.github.io\/lean-web-editor\/#url=https:\/\/raw.githubusercontent.com\/jaalonso\/Calculemus\/main\/src\/Suma_de_los_primeros_n_numeros_naturales.lean\" rel=\"noopener noreferrer\" target=\"_blank\">esta sesi\u00f3n con Lean<\/a>.<\/p>\n<p>En los comentarios se pueden escribir otras soluciones, escribiendo el c\u00f3digo entre una l\u00ednea con &#60;pre lang=&quot;lean&quot;&#62; y otra con &#60;\/pre&#62;<br \/>\n[\/expand]<\/p>\n<p>[expand title=\u00bbSoluciones con Isabelle\/HOL\u00bb]<\/p>\n<pre lang=\"isar\">\r\ntheory Suma_de_los_primeros_n_numeros_naturales\r\nimports Main\r\nbegin\r\n\r\nfun suma :: \"nat \u21d2 nat\" where\r\n  \"suma 0       = 0\"\r\n| \"suma (Suc n) = suma n + Suc n\"\r\n\r\n(* 1\u00aa demostraci\u00f3n *)\r\nlemma\r\n  \"2 * suma n = n * (n + 1)\"\r\nproof (induct n)\r\n  have \"2 * suma 0 = 2 * 0\"\r\n    by (simp only: suma.simps(1))\r\n  also have \"\u2026 = 0\"\r\n    by (rule mult_0_right)\r\n  also have \"\u2026 = 0 * (0 + 1)\"\r\n    by (rule mult_0 [symmetric])\r\n  finally show \"2 * suma 0 = 0 * (0 + 1)\"\r\n    by this\r\nnext\r\n  fix n\r\n  assume HI : \"2 * suma n = n * (n + 1)\"\r\n  have \"2 * suma (Suc n) = 2 * (suma n + Suc n)\"\r\n    by (simp only: suma.simps(2))\r\n  also have \"\u2026 = 2 * suma n + 2 * Suc n\"\r\n    by (rule add_mult_distrib2)\r\n  also have \"\u2026 = n * (n + 1) + 2 * Suc n\"\r\n    by (simp only: HI)\r\n  also have \"\u2026 = n * (n + Suc 0) + 2 * Suc n\"\r\n    by (simp only: One_nat_def)\r\n  also have \"\u2026 = n * Suc (n + 0) + 2 * Suc n\"\r\n    by (simp only: add_Suc_right)\r\n  also have \"\u2026 = n * Suc n + 2 * Suc n\"\r\n    by (simp only: add_0_right)\r\n  also have \"\u2026 = (n + 2) * Suc n\"\r\n    by (simp only: add_mult_distrib)\r\n  also have \"\u2026 = Suc (Suc n) * Suc n\"\r\n    by (simp only: add_2_eq_Suc')\r\n  also have \"\u2026 = (Suc n + 1) * Suc n\"\r\n    by (simp only: Suc_eq_plus1)\r\n  also have \"\u2026 = Suc n * (Suc n + 1)\"\r\n    by (simp only: mult.commute)\r\n  finally show \"2 * suma (Suc n) = Suc n * (Suc n + 1)\"\r\n    by this\r\nqed\r\n\r\n(* 2\u00aa demostraci\u00f3n *)\r\nlemma\r\n  \"2 * suma n = n * (n + 1)\"\r\nproof (induct n)\r\n  have \"2 * suma 0 = 2 * 0\" by simp\r\n  also have \"\u2026 = 0\" by simp\r\n  also have \"\u2026 = 0 * (0 + 1)\" by simp\r\n  finally show \"2 * suma 0 = 0 * (0 + 1)\" .\r\nnext\r\n  fix n\r\n  assume HI : \"2 * suma n = n * (n + 1)\"\r\n  have \"2 * suma (Suc n) = 2 * (suma n + Suc n)\" by simp\r\n  also have \"\u2026 = 2 * suma n + 2 * Suc n\" by simp\r\n  also have \"\u2026 = n * (n + 1) + 2 * Suc n\" using HI by simp\r\n  also have \"\u2026 = n * (n + Suc 0) + 2 * Suc n\" by simp\r\n  also have \"\u2026 = n * Suc (n + 0) + 2 * Suc n\" by simp\r\n  also have \"\u2026 = n * Suc n + 2 * Suc n\" by simp\r\n  also have \"\u2026 = (n + 2) * Suc n\" by simp\r\n  also have \"\u2026 = Suc (Suc n) * Suc n\" by simp\r\n  also have \"\u2026 = (Suc n + 1) * Suc n\" by simp\r\n  also have \"\u2026 = Suc n * (Suc n + 1)\" by simp\r\n  finally show \"2 * suma (Suc n) = Suc n * (Suc n + 1)\" .\r\nqed\r\n\r\n(* 3\u00aa demostraci\u00f3n *)\r\nlemma\r\n  \"2 * suma n = n * (n + 1)\"\r\nproof (induct n)\r\n  have \"2 * suma 0 = 2 * 0\" by simp\r\n  also have \"\u2026 = 0\" by simp\r\n  also have \"\u2026 = 0 * (0 + 1)\" by simp\r\n  finally show \"2 * suma 0 = 0 * (0 + 1)\" .\r\nnext\r\n  fix n\r\n  assume HI : \"2 * suma n = n * (n + 1)\"\r\n  have \"2 * suma (Suc n) = 2 * (suma n + Suc n)\" by simp\r\n  also have \"\u2026 = n * (n + 1) + 2 * Suc n\" using HI by simp\r\n  also have \"\u2026 = (n + 2) * Suc n\" by simp\r\n  also have \"\u2026 = Suc n * (Suc n + 1)\" by simp\r\n  finally show \"2 * suma (Suc n) = Suc n * (Suc n + 1)\" .\r\nqed\r\n\r\n(* 4\u00aa demostraci\u00f3n *)\r\nlemma\r\n  \"2 * suma n = n * (n + 1)\"\r\nproof (induct n)\r\n  show \"2 * suma 0 = 0 * (0 + 1)\" by simp\r\nnext\r\n  fix n\r\n  assume \"2 * suma n = n * (n + 1)\"\r\n  then show \"2 * suma (Suc n) = Suc n * (Suc n + 1)\" by simp\r\nqed\r\n\r\n(* 5\u00aa demostraci\u00f3n *)\r\nlemma\r\n  \"2 * suma n = n * (n + 1)\"\r\nproof (induct n)\r\n  case 0\r\n  then show ?case by simp\r\nnext\r\n  case (Suc n)\r\n  then show ?case by simp\r\nqed\r\n\r\n(* 6\u00aa demostraci\u00f3n *)\r\nlemma\r\n  \"2 * suma n = n * (n + 1)\"\r\nby (induct n) simp_all\r\n\r\nend\r\n<\/pre>\n<p>En los comentarios se pueden escribir otras soluciones, escribiendo el c\u00f3digo entre una l\u00ednea con &#60;pre lang=&quot;isar&quot;&#62; y otra con &#60;\/pre&#62;<br \/>\n[\/expand]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Demostrar que la suma de los primeros n\u00fameros naturales 0 + 1 + 2 + 3 + \u00b7\u00b7\u00b7 + n es n \u00d7 (n + 1)\/2 Para ello, completar la siguiente teor\u00eda de Lean: import data.nat.basic import tactic open nat variable (n : \u2115) def suma : \u2115 \u2192 \u2115 | 0 := 0 | (n+1) := suma n + (n+1) example : 2 * suma n = n * (n + 1) := sorry [expand title=\u00bbSoluciones con Lean\u00bb] import data.nat.basic import tactic open nat variable (n : \u2115) set_option pp.structure_projections false @[simp] def suma : \u2115 \u2192 \u2115 | 0 := 0 | (n+1) := suma n + (n+1) &#8212; 1\u00aa demostraci\u00f3n example : 2 * suma n = n * (n + 1)&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[105,25],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/posts\/770"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/comments?post=770"}],"version-history":[{"count":1,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/posts\/770\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/posts\/770\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/media?parent=770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/categories?post=770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/calculemus\/wp-json\/wp\/v2\/tags?post=770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}