{"id":52,"date":"2013-12-14T00:01:15","date_gmt":"2013-12-14T08:01:15","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=52"},"modified":"2013-12-14T09:03:45","modified_gmt":"2013-12-14T17:03:45","slug":"the-time-i-won-the-programming-contest","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=52","title":{"rendered":"The Time I Won The Programming Contest"},"content":{"rendered":"<p>It was a simple contest: Write code that displays the first 100 prime numbers. The person who wrote the fastest code won.<br \/>\n<!--more--><br \/>\nSuch a challenge today wouldn&#8217;t be worthy; you&#8217;d need to display several thousand primes to detect any speed differences between systems. That&#8217;s because today&#8217;s computers are so fast that 100 primes appear in the blink of an eye (well, providing you didn&#8217;t completely screw up the code). In fact, the main thing slowing down a program of this type is output: It probably takes longer for a computer today to generate the text output for 100 primes than it does for the code to calculate the values.<\/p>\n<p>Back in the early 1980s, processor speed and screen output were both hurdles. So as long as your code ran on the same computer, or two identical systems side-by-side, you could judge which was faster. For the 100 Primes contest, two computers were setup side-by-side. The program name was typed at the command prompt, then both Enter keys pressed simultaneously.<\/p>\n<p>The results were surprising: My code generated 100 primes quickly, scrolling output the screen in a swift cascade of numbers. It looked like a tall ship&#8217;s sail unfurling. It was beautiful.<\/p>\n<p>The other programs, running on the other computer, spurt out one prime number at a time, fast at first and then more slowly. My code&#8217;s output took one third the time. That surprised everyone, most of whom were professional programmers.<\/p>\n<p>&#8220;How did Gookin do it?&#8221; they wondered. Then I showed them the code, which you see below.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    puts(\"2\\t3\\t5\\t7\\t11\\t13\\t17\\t19\\t23\\t29\");\r\n    puts(\"31\\t37\\t41\\t43\\t47\\t53\\t59\\t61\\t67\\t71\");\r\n    puts(\"73\\t79\\t83\\t89\\t97\\t101\\t103\\t107\\t109\\t113\");\r\n    puts(\"127\\t131\\t137\\t139\\t149\\t151\\t157\\t163\\t167\\t173\");\r\n    puts(\"179\\t181\\t191\\t193\\t197\\t199\\t211\\t223\\t227\\t229\");\r\n    puts(\"233\\t239\\t241\\t251\\t257\\t263\\t269\\t271\\t277\\t281\");\r\n    puts(\"283\\t293\\t307\\t311\\t313\\t317\\t331\\t337\\t347\\t349\");\r\n    puts(\"353\\t359\\t367\\t373\\t379\\t383\\t389\\t397\\t401\\t409\");\r\n    puts(\"419\\t421\\t431\\t433\\t439\\t443\\t449\\t457\\t461\\t463\");\r\n    puts(\"467\\t479\\t487\\t491\\t499\\t503\\t509\\t521\\t523\\t541\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code did not cheat! Remember, the program&#8217;s goal was to display the first 100 primes. It wasn&#8217;t to write a routine that first calculates and then displays the primes, although that was the implication and the path the other programmers chose to follow. So although there was some upset, I was congratulated on being clever.<\/p>\n<p>My point is this: Don&#8217;t lose sight of the Big Picture. It&#8217;s very easy as a programmer to over-complicate issues. You can speed hours working on a gorgeous algorithm that works exactly as intended. If you&#8217;re like me, your brain probably loves such puzzles. That&#8217;s not going to help you, however, when an easier, more obvious solution is at hand. Don&#8217;t be afraid to consider that easier solution!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Was it cheating, or just being clever? You be the judge. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=52\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,4],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-main","category-philosophy"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=52"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions\/490"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}