{"id":4620,"date":"2015-08-09T12:27:03","date_gmt":"2015-08-09T20:27:03","guid":{"rendered":"http:\/\/www.rknibbe.com\/Wordpress\/?p=4620"},"modified":"2015-08-09T20:45:15","modified_gmt":"2015-08-10T04:45:15","slug":"trivial-code-unexpected-output","status":"publish","type":"post","link":"https:\/\/www.rknibbe.com\/Wordpress\/archives\/4620","title":{"rendered":"Trivial Code; Unexpected Output"},"content":{"rendered":"<p style=\"text-align: justify;\">I recently read an <a href=\"http:\/\/www.theatlantic.com\/technology\/archive\/2011\/06\/how-i-failed-failed-and-finally-succeeded-at-learning-how-to-code\/239855\/\">old post<\/a> about a guy who taught himself how to write computer programs after failing at it a number of times before. Midway through the article he points to the kind of problem that really got him started on his path to learning:<\/p>\n<p style=\"padding-left: 30px;\"><em>My struggle to become a hacker finally saw a breakthrough late in my freshman year of college, when I stumbled on a simple question:<\/em><\/p>\n<p style=\"padding-left: 30px;\"><strong>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6, and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.<\/strong><\/p>\n<p style=\"padding-left: 30px; text-align: justify;\"><em>What&#8217;s especially neat about it is that someone who has never programmed &#8212; someone who doesn&#8217;t even know what a program is &#8212; can learn to write code that solves this problem in less than three hours. I&#8217;ve seen it happen. All it takes is a little hunger. You just have to want the answer.<\/em><\/p>\n<p style=\"text-align: justify;\">It took me about three minutes, not hours (see below). Then again, I first learned to program computers about thirty years ago. What held my interest in the article, and this question especially, was it got me to thinking how, if I were the teacher and using this example to teach somebody how to program, how I might extend the problem a little by asking the student to make the program more flexible &#8212; extend it to let the user input any two numbers (not just 3 and 5), and any ceiling value (not just 1000). That&#8217;s what I did in the trivial example below.<\/p>\n<p style=\"text-align: justify;\">What I didn&#8217;t expect, however, were the outputs when I repeatedly ran the program with values of <em>ceiling <\/em>10 times greater than the previous one (10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000). It&#8217;s not intuitive to me that each successive sum would be a little over 100 times greater\u00a0(not exactly) than the previous one. Not 10 times greater, which might appeal to your intuition (but still wouldn&#8217;t be obvious to me).<\/p>\n<p style=\"text-align: justify;\">I have an email in to my brainy friend on the east coast asking for an explanation. I was told by a another friend of ours he got a perfect score on the SAT.<\/p>\n<p>public class Quiz {<\/p>\n<p style=\"padding-left: 20px;\">public static void main(String [] args) {<\/p>\n<p style=\"padding-left: 30px;\">int firstNumber;<br \/>\nfirstNumber = Integer.parseInt(args[0]) ;<\/p>\n<p style=\"padding-left: 30px;\">int secondNumber;<br \/>\nsecondNumber = Integer.parseInt(args[1]) ;<\/p>\n<p style=\"padding-left: 30px;\">int ceiling ;<br \/>\nceiling = Integer.parseInt(args[2]) ;<\/p>\n<p style=\"padding-left: 30px;\">long sum = 0 ;<\/p>\n<p style=\"padding-left: 30px;\">for(int i=1; i&lt;ceiling; i++) {<\/p>\n<p style=\"padding-left: 40px;\">if( ( i%firstNumber ) == 0 || ( i%secondNumber ) == 0 ) {<\/p>\n<p style=\"padding-left: 45px;\">sum += i ;<\/p>\n<p style=\"padding-left: 40px;\">}<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">System.out.println( &#8220;Sum: &#8221; + sum ) ;<\/p>\n<p style=\"padding-left: 20px;\">}<\/p>\n<p>}<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;-Output&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&gt;java Quiz 3 5 10<br \/>\nSum: 23<\/p>\n<p>&gt;java Quiz 3 5 100<br \/>\nSum: 2318<\/p>\n<p>&gt;java Quiz 3 5 1000<br \/>\nSum: 233168<\/p>\n<p>&gt;java Quiz 3 5 10000<br \/>\nSum: 23331668<\/p>\n<p>&gt;java Quiz 3 5 100000<br \/>\nSum: 2333316668<\/p>\n<p>&gt;java Quiz 3 5 1000000<br \/>\nSum: 233333166668<\/p>\n<p>&gt;java Quiz 3 5 10000000<br \/>\nSum: 23333331666668<\/p>\n<p>&gt;java Quiz 3 5 100000000<br \/>\nSum: 2333333316666668<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently read an old post about a guy who taught himself how to write computer programs after failing at it a number of times before. Midway through the article he points to the kind of problem that really got him started on his path to learning: My struggle to become a hacker finally saw [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wikipediapreview_detectlinks":true,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4620","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/posts\/4620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/comments?post=4620"}],"version-history":[{"count":42,"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/posts\/4620\/revisions"}],"predecessor-version":[{"id":4663,"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/posts\/4620\/revisions\/4663"}],"wp:attachment":[{"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/media?parent=4620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/categories?post=4620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rknibbe.com\/Wordpress\/wp-json\/wp\/v2\/tags?post=4620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}