<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jonathan Landrum</title>
	<atom:link href="http://jonlandrum.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonlandrum.com</link>
	<description>Code examples from my time in college</description>
	<lastBuildDate>Sun, 05 Feb 2012 06:07:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Fun With Diamonds</title>
		<link>http://jonlandrum.com/2012/02/05/fun-with-diamonds/</link>
		<comments>http://jonlandrum.com/2012/02/05/fun-with-diamonds/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 06:07:10 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=202</guid>
		<description><![CDATA[I was googling for programming challenges today and ran across one that was more than twenty years old. It was an old scan of an assignment a fellow posted about his days in college taking Pascal. The idea behind the &#8230; <a href="http://jonlandrum.com/2012/02/05/fun-with-diamonds/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was googling for programming challenges today and ran across one that was more than twenty years old. It was an <a href="http://www.craigmurphy.com/blog/?p=1417" title="Programming Challenge!">old scan of an assignment a fellow posted about his days in college taking Pascal</a>. The idea behind the challenge is to write a program that takes user input for size, and builds a diamond out of characters to the size specified.</p>
<blockquote><p>Write a program which draws a diamond of the form illustrated below. The letter which is to appear at the widest point of the figure (E in the example) is to be specified as input data.</p></blockquote>
<p><img src="http://jonlandrum.com/wp-content/uploads/2012/02/diamond.jpg" alt="Diamond Challenge" title="Diamond Challenge" width="640" height="195" class="alignnone size-full wp-image-203" /></p>
<p>Problem is, I&#8217;d never made a diamond program before. The closest I&#8217;ve come was a Christmas tree program, which replaces the bottom of the diamond with a trunk. It&#8217;s a bit easier to make because you don&#8217;t have to think about reversing your algorithm.</p>
<p>So to start this project, I first built a standard diamond program. These types of programs ask for an integer, and build a diamond out of asterisks with the middle row containing the specified number of asterisks.</p>
<p>I then modified the program to be a &#8220;hollow&#8221; diamond, with the middle asterisks being replaced by space characters. After mastering that, I felt confident in my algorithm to take on the character diamond. It proved only slightly more challenging than the hollow diamond, really just replacing the calls to print an asterisk with a call to print a character from an array which stored all the letters. Here&#8217;s a screen capture of an example output:</p>
<p><img src="http://jonlandrum.com/wp-content/uploads/2012/02/char-diamond.png" alt="Character Diamond" title="Character Diamond" width="388" height="503" class="alignnone size-full wp-image-206" /></p>
<p>All in all, I am very well pleased with the outcome of this program. I tested it with every letter, and it performs flawlessly every time. It may not be the <em>shortest</em> algorithm to produce the character diamond, but it&#8217;s mine.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        4 February 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     char-diamond.cpp</span>
<span style="color: #666666;">// Purpose:     Takes user input of one character and</span>
<span style="color: #666666;">//              returns a diamond of that size.</span>
<span style="color: #666666;">// Assumptions: 1.) Input is a single letter (char).</span>
<span style="color: #666666;">//                  Any other input will return un-</span>
<span style="color: #666666;">//                  expected results.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// Global Variables</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">char</span> letter<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">26</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">'A'</span>, <span style="color: #FF0000;">'B'</span>, <span style="color: #FF0000;">'C'</span>, <span style="color: #FF0000;">'D'</span>, <span style="color: #FF0000;">'E'</span>, <span style="color: #FF0000;">'F'</span>, <span style="color: #FF0000;">'G'</span>, <span style="color: #FF0000;">'H'</span>, <span style="color: #FF0000;">'I'</span>, <span style="color: #FF0000;">'J'</span>, <span style="color: #FF0000;">'K'</span>, <span style="color: #FF0000;">'L'</span>, <span style="color: #FF0000;">'M'</span>,
	           <span style="color: #FF0000;">'N'</span>, <span style="color: #FF0000;">'O'</span>, <span style="color: #FF0000;">'P'</span>, <span style="color: #FF0000;">'Q'</span>, <span style="color: #FF0000;">'R'</span>, <span style="color: #FF0000;">'S'</span>, <span style="color: #FF0000;">'T'</span>, <span style="color: #FF0000;">'U'</span>, <span style="color: #FF0000;">'V'</span>, <span style="color: #FF0000;">'W'</span>, <span style="color: #FF0000;">'X'</span>, <span style="color: #FF0000;">'Y'</span>, <span style="color: #FF0000;">'Z'</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Does ALL the things!</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Declare variables, not war</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000ff;">char</span> in<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  counter<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  input<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  level<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span>  spaces<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;---------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-    Character Diamond    -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;---------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;             A&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;            B B&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;           C   C&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;          D     D&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;         E       E&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;          D     D&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;           C   C&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;            B B&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;             A&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program asks for a&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;character, and returns a&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;diamond of that size.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Main processing loop</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter a single letter for the size of your diamond: &quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span>  <span style="color: #000080;">&gt;&gt;</span> in<span style="color: #008080;">;</span>
&nbsp;
	input <span style="color: #000080;">=</span> <span style="color: #0000dd;">toupper</span><span style="color: #008000;">&#40;</span>in<span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">64</span><span style="color: #008080;">;</span>
	input <span style="color: #000080;">=</span> input <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	counter <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	level <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>counter <span style="color: #000080;">&lt;</span> input<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>input <span style="color: #000040;">%</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
			spaces <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>input <span style="color: #000040;">-</span> counter<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">else</span>
			spaces <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>input <span style="color: #000040;">-</span> counter<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> c <span style="color: #000080;">&lt;</span> spaces<span style="color: #008080;">;</span> c<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> letter<span style="color: #008000;">&#91;</span>level<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>counter <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
			spaces <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">else</span>
			spaces <span style="color: #000080;">=</span> counter <span style="color: #000040;">-</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> spaces<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>counter <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> letter<span style="color: #008000;">&#91;</span>level<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">else</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
		counter <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
		level<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End while</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>counter <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>input <span style="color: #000040;">%</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
			spaces <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>input <span style="color: #000040;">-</span> counter<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">else</span>
			spaces <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>input <span style="color: #000040;">-</span> counter<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j <span style="color: #000080;">&lt;</span> spaces<span style="color: #008080;">;</span> j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> letter<span style="color: #008000;">&#91;</span>level<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>counter <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
			spaces <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">else</span>
			spaces <span style="color: #000080;">=</span> counter <span style="color: #000040;">-</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> n <span style="color: #000080;">&lt;</span> spaces<span style="color: #008080;">;</span> n<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>counter <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> letter<span style="color: #008000;">&#91;</span>level<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">else</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
		counter <span style="color: #000040;">-</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
		level<span style="color: #000040;">--</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End while</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/02/05/fun-with-diamonds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding the Difference of Sums using C++</title>
		<link>http://jonlandrum.com/2012/02/03/finding-the-difference-of-sums-using-cpp/</link>
		<comments>http://jonlandrum.com/2012/02/03/finding-the-difference-of-sums-using-cpp/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 20:13:27 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=178</guid>
		<description><![CDATA[Someone asked online what the difference was between the sum of the squares and the square of the sums. For instance, if you were to add all the natural numbers from 1 to 10, and then square it, would it &#8230; <a href="http://jonlandrum.com/2012/02/03/finding-the-difference-of-sums-using-cpp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Someone asked online what the difference was between the sum of the squares and the square of the sums. For instance, if you were to add all the natural numbers from 1 to 10, and then square it, would it be larger, smaller, or the same size if you squared each integer first and then added them?</p>
<pre>
1<small><sup>2</sup></small> + 2<small><sup>2</sup></small> + 3<small><sup>2</sup></small> + &hellip; + 8<small><sup>2</sup></small> + 9<small><sup>2</sup></small> + 10<small><sup>2</sup></small> (Sum of the squares)
(1 + 2 + 3 + &hellip; + 8 + 9 + 10)<small><sup>2</sup></small> (Square of the sums)</pre>
<p>At first glance, this seems like a fine job for a couple loops, or perhaps recursion. But there&#8217;s actually a much more efficient way to go about this problem, and the answer is brought to us by a fine mathematician named <a href="http://en.wikipedia.org/wiki/Leonhard_Euler" title="Article on Wikipedia">Leonhard Euler</a>.</p>
<p>Euler is one of the men responsible for bringing us Calculus (he actually is responsible for more of Calculus than Newton) and one of the topics discussed in the second semester of Single-variable Calculus is <em>Summation</em>. Using these summation algorithms, it is possible to do some very intriguing calculations without going through the rote process of cycling through the list.</p>
<p>For a short set such as the first ten integers, it&#8217;s not a problem. But suppose the question was of the first ten <em>million</em> integers? That would take a looping algorithm a long time. But it would take almost exactly the same time as doing the first 10 using Euler&#8217;s algorithms.</p>
<p>Algorithm for summing integers:<br />
<img alt="Algorithm for summing integers" src="http://jonlandrum.com/images/posts/sumIntegers.png" title="Algorithm for summing integers" width="136" height="50" /></p>
<p>Algorithm for summing squares of integers:<br />
<img alt="Algorithm for summing squares of integers" src="http://jonlandrum.com/images/posts/sumSquares.png" title="Algorithm for summing squares of integers" width="219" height="49" /></p>
<p>With these algorithms, it&#8217;s trivial to find the answer to the problem at hand. Nevertheless, I&#8217;ve written a program anyway, if for no other reason than to show how to use these algorithms.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        3 February 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     difference.cpp</span>
<span style="color: #666666;">// Purpose:     Finds the difference between the sum</span>
<span style="color: #666666;">//              of the squares and the square of the</span>
<span style="color: #666666;">//              sums of the first 100 integers.</span>
<span style="color: #666666;">// Assumptions: None.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Does ALL the things!</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Declare variables, not war</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000ff;">int</span> sumSquares<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> squareSums<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> difference<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-    Finding the Difference    -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;--------------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program seeks to find the&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;difference between the sum of&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;squares and the square of the&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;sums.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Main processing loop</span>
	<span style="color: #666666;">// ------------------------------------------</span>
&nbsp;
	<span style="color: #ff0000; font-style: italic;">/* ******************************************
	   Yes, you can use loops here, but why would
	   you want to? There are already algorithms
	   for finding these answers.
	   ****************************************** */</span>
&nbsp;
	sumSquares <span style="color: #000080;">=</span> <span style="color: #0000dd;">100</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">101</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">201</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">6</span><span style="color: #008080;">;</span>     <span style="color: #666666;">// Algorithm for summing squares:  n(n+1)(2n+1)/6</span>
	squareSums <span style="color: #000080;">=</span> <span style="color: #0000dd;">100</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">101</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>           <span style="color: #666666;">// Algorithm for summing integers: n(n+1)/2</span>
	squareSums <span style="color: #000080;">=</span> squareSums <span style="color: #000040;">*</span> squareSums<span style="color: #008080;">;</span> <span style="color: #666666;">// Now squaring it</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>sumSquares <span style="color: #000040;">-</span> squareSums <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		difference <span style="color: #000080;">=</span> sumSquares <span style="color: #000040;">-</span> squareSums<span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;sumSquares is larger. (&quot;</span> <span style="color: #000080;">&lt;&lt;</span> sumSquares <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;)&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
		difference <span style="color: #000080;">=</span> squareSums <span style="color: #000040;">-</span> sumSquares<span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;squareSums is larger. (&quot;</span> <span style="color: #000080;">&lt;&lt;</span> squareSums <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;)&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End if</span>
&nbsp;
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Return the results</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> difference <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; is the difference between &quot;</span> <span style="color: #000080;">&lt;&lt;</span> squareSums <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and &quot;</span> <span style="color: #000080;">&lt;&lt;</span> sumSquares <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>	
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/02/03/finding-the-difference-of-sums-using-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding the Smallest Integer Evenly Divisible by all the Numbers from 1 to 20</title>
		<link>http://jonlandrum.com/2012/02/02/finding-the-smallest-integer-evenly-divisible-by-all-the-numbers-from-1-to-20/</link>
		<comments>http://jonlandrum.com/2012/02/02/finding-the-smallest-integer-evenly-divisible-by-all-the-numbers-from-1-to-20/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 15:22:24 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=172</guid>
		<description><![CDATA[I found this programming assignment online today. It posits that 2,520 is the smallest number evenly divisible by all the natural numbers from 1 to 10. The question is, what is the smallest number evenly divisible by all the natural &#8230; <a href="http://jonlandrum.com/2012/02/02/finding-the-smallest-integer-evenly-divisible-by-all-the-numbers-from-1-to-20/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I found this programming assignment online today. It posits that 2,520 is the smallest number evenly divisible by all the natural numbers from 1 to 10. The question is, what is the smallest number evenly divisible by all the natural numbers from 1 to 20? It seemed like the best idea to me to save the answers in a boolean array, one cell for each of the numbers from 1 to 20, and if every answer is true, then we have our number. I tested it, got the correct result, and the answer is surprisingly large. Suffice it to say, it took a minute to get there.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        2 February 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     evenlyDivisible.cpp</span>
<span style="color: #666666;">// Purpose:     Finds the smallest number evenly</span>
<span style="color: #666666;">//              divisible by all the natural numbers</span>
<span style="color: #666666;">//              from 1 to 20.</span>
<span style="color: #666666;">// Assumptions: None.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Does ALL the things!</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Declare variables, not war</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000ff;">bool</span> answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">bool</span> divisible <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Not true for 20</span>
	<span style="color: #0000ff;">int</span>  number    <span style="color: #000080;">=</span> <span style="color: #0000dd;">20</span><span style="color: #008080;">;</span>    <span style="color: #666666;">// But logical place to start</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;----------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-     Evenly Divisible     -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;----------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program checks to find&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;the smallest number that is&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;evenly divisible by all the&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;integers from 1 to 20.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Processing...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Main processing loop</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>divisible <span style="color: #000080;">==</span> <span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> counter <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> counter <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">20</span><span style="color: #008080;">;</span> counter<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>number <span style="color: #000040;">%</span> counter <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				answers<span style="color: #008000;">&#91;</span>counter <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
				answers<span style="color: #008000;">&#91;</span>counter <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End if</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End for</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">7</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">8</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">9</span><span style="color: #008000;">&#93;</span>  <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">11</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">12</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">13</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">14</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">15</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">16</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">17</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">18</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span> <span style="color: #000040;">&amp;&amp;</span>
		    answers<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">19</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			divisible <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
			number<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End if</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End while</span>
&nbsp;
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #666666;">// Return the results</span>
	<span style="color: #666666;">// ------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The answer is &quot;</span> <span style="color: #000080;">&lt;&lt;</span> number <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/02/02/finding-the-smallest-integer-evenly-divisible-by-all-the-numbers-from-1-to-20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding the Even Numbers of the Fibonacci Sequence</title>
		<link>http://jonlandrum.com/2012/02/01/adding-the-even-numbers-of-the-fibonacci-sequence/</link>
		<comments>http://jonlandrum.com/2012/02/01/adding-the-even-numbers-of-the-fibonacci-sequence/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 14:39:49 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=165</guid>
		<description><![CDATA[Suppose you want to find all the even numbers in the Fibonacci Sequence less than, say, 4,000,000, and you want to add them together. This can easily be done with a couple of methods in C++ or the language of &#8230; <a href="http://jonlandrum.com/2012/02/01/adding-the-even-numbers-of-the-fibonacci-sequence/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Suppose you want to find all the even numbers in the <a href="http://en.wikipedia.org/wiki/Fibonacci_number" title="Article on Wikipedia">Fibonacci Sequence</a> less than, say, 4,000,000, and you want to add them together. This can easily be done with a couple of methods in <a href="/category/c/" title="Code I've written in C or C++ (or some other derivative of C)">C++</a> or the language of your choice.</p>
<p>The idea behind the Fibonacci sequence is to add the previous two numbers together to get the current number. So you get 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on, <i>ad infinitum</i>. If we were to take only the even ones and add them, what would you get? In the case of the previous list, only 2 and 8 are even, so you would get 10. This program runs the computation to 4,000,000 and it gave me the answer in less than one second.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        1 February 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     evenFibonacci.cpp</span>
<span style="color: #666666;">// Purpose:     Summs the even numbers in the</span>
<span style="color: #666666;">//              Fibonacci sequence that are less</span>
<span style="color: #666666;">//              than 4,000,000.</span>
<span style="color: #666666;">// Assumptions:	None.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// Function prototypes</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> fib<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Does ALL the things!</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #666666;">// Declare variables, not war</span>
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> number<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-----------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-      Even Fibonaccis      -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-----------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;1, 2, 3, 5, 8, 13, 21, 34,...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program calculates the&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Fibonacci sequence and adds&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;the even ones less than&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;4,000,000 together.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #666666;">// Main processing loop</span>
	<span style="color: #666666;">//</span>
	<span style="color: #666666;">// There are a few different ways to accomplish</span>
	<span style="color: #666666;">// this. I chose this algorithm because it made</span>
	<span style="color: #666666;">// sense to me. You can also get rid of the first</span>
	<span style="color: #666666;">// &quot;if&quot; by putting &quot;number &lt; 4000000&quot; in the</span>
	<span style="color: #666666;">// while test, and moving &quot;number = fib(c)&quot; to</span>
	<span style="color: #666666;">// after &quot;c++&quot;.</span>
	<span style="color: #666666;">//</span>
	<span style="color: #666666;">// This algorithm returns the answer in &lt;1 second</span>
	<span style="color: #666666;">// on my laptop.</span>
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>                 <span style="color: #666666;">// Need to keep it going</span>
		number <span style="color: #000080;">=</span> fib<span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>number <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">4000000</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// until we can break</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>number <span style="color: #000040;">%</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
			sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> number<span style="color: #008080;">;</span>
		c<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End while</span>
&nbsp;
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #666666;">// Return the results</span>
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-----------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>//_ Live long and prosper.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// fib():</span>
<span style="color: #666666;">// Calculates the integer in the Fibonacci sequence</span>
<span style="color: #666666;">// at the input space. 2 returns 1, 7 returns 13, ...</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> fib <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> in<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> out<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>in <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #000040;">||</span> in <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		out <span style="color: #000080;">=</span> in<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
		out <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>fib<span style="color: #008000;">&#40;</span>in<span style="color: #000040;">-</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fib<span style="color: #008000;">&#40;</span>in<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End if</span>
&nbsp;
	<span style="color: #0000ff;">return</span> out<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End fib</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/02/01/adding-the-even-numbers-of-the-fibonacci-sequence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sum of Threes and Fives</title>
		<link>http://jonlandrum.com/2012/01/31/sum-of-threes-and-fives/</link>
		<comments>http://jonlandrum.com/2012/01/31/sum-of-threes-and-fives/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 00:07:21 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=145</guid>
		<description><![CDATA[I enjoy finding problems to solve with code. I found a page on the internet this afternoon that asked for the sum of all integers between 1 and 1,000 that are divisible by either 3 or 5. I immediately thought &#8230; <a href="http://jonlandrum.com/2012/01/31/sum-of-threes-and-fives/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I enjoy finding problems to solve with code. I found a page on the internet this afternoon that asked for the sum of all integers between 1 and 1,000 that are divisible by either 3 or 5. I immediately thought of the <a href="http://jonlandrum.com/2012/01/23/fizzbuzz/" title="FizzBuzz">FizzBuzz</a> program I wrote a while back, and realized the algorithm is actually quite similar. It&#8217;s just what you <em>do</em> with those threes and fives that&#8217;s different in this case. Either way, it was a fun couple of minutes putting to code what I&#8217;d been mulling over.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Programmer:  Jonathan Landrum</span>
<span style="color: #666666;">// Date:        31 January 2012</span>
<span style="color: #666666;">// ==================================================</span>
<span style="color: #666666;">// Program:     threeOrFive.cpp</span>
<span style="color: #666666;">// Purpose:     Adds the integers between 1 and 1,000</span>
<span style="color: #666666;">//              that are divisible by either 3 or 5.</span>
<span style="color: #666666;">// Assumptions: None.</span>
<span style="color: #666666;">// ==================================================</span>
&nbsp;
<span style="color: #339900;">#include &amp;lt;iostream&gt;</span>
<span style="color: #339900;">#include &amp;lt;string&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #666666;">// main():</span>
<span style="color: #666666;">// Does ALL the things!</span>
<span style="color: #666666;">// --------------------------------------------------</span>
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #666666;">// Declare variables, not war</span>
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #666666;">// Introduce the program</span>
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;----------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;-    Sum of 3's and 5's    -&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;----------------------------&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;This program adds together the&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;integers between 1 and 1,000&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;that are divisible by 3 or 5.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #666666;">// Main processing loop</span>
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>c <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">1000</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>c <span style="color: #000040;">%</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #000040;">||</span> c <span style="color: #000040;">%</span> <span style="color: #0000dd;">5</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> c<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> c <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>c <span style="color: #000040;">%</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> c<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> c <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>c <span style="color: #000040;">%</span> <span style="color: #0000dd;">5</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> c<span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> c <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// end if</span>
		c<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// end while</span>
&nbsp;
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #666666;">// Return the answer</span>
	<span style="color: #666666;">// ----------------------------------------------</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> sum <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End main</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/01/31/sum-of-threes-and-fives/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fortran Prime Number Calculator</title>
		<link>http://jonlandrum.com/2012/01/30/fortran-prime-number-calculator/</link>
		<comments>http://jonlandrum.com/2012/01/30/fortran-prime-number-calculator/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 00:13:15 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Fortran]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=135</guid>
		<description><![CDATA[This program is one of my favorites I&#8217;ve written so far. It takes input from the user for the lower and upper bounds of a range of integers, calculates which numbers in that range are prime numbers, and outputs a &#8230; <a href="http://jonlandrum.com/2012/01/30/fortran-prime-number-calculator/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This program is one of my favorites I&#8217;ve written so far. It takes input from the user for the lower and upper bounds of a range of integers, calculates which numbers in that range are <a href="http://en.wikipedia.org/wiki/Prime_number" title="Article on Wikipedia">prime numbers</a>, and outputs a list of the primes and the total number of primes found.</p>
<p>I tested the output of this program against <a href="http://primes.utm.edu/lists/small/100000.txt" title="The Prime Pages at UT Martin">this list of primes</a>, and it checked out perfectly. It&#8217;s a fairly fast program, returning the primes between 1 and 1,000,000 in about 6 seconds on my machine. Only assumptions are the input must be in Real form. Text input will cause the program to throw an exception.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
</pre></td><td class="code"><pre class="fortran" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! Programmer:   Jonathan Landrum</span>
<span style="color: #666666; font-style: italic;">! Date:         21 September 2011</span>
<span style="color: #666666; font-style: italic;">! Class:        CSC 204</span>
<span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! Program:      primes.exe</span>
<span style="color: #666666; font-style: italic;">! Purpose:      Calculates the number of prime numbers</span>
<span style="color: #666666; font-style: italic;">!               there are in a given range, and returns</span>
<span style="color: #666666; font-style: italic;">!               them in a list.</span>
<span style="color: #666666; font-style: italic;">! Assumptions:  1.) Input from the user is in integer</span>
<span style="color: #666666; font-style: italic;">!                   form. Text input will cause the</span>
<span style="color: #666666; font-style: italic;">!                   program to fail. Real number input</span>
<span style="color: #666666; font-style: italic;">!                   will be truncated into integers.</span>
<span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
&nbsp;
<span style="color: #b1b100;">PROGRAM</span> primes
&nbsp;
	<span style="color: #000066;">IMPLICIT</span> <span style="color: #000066;">NONE</span>
&nbsp;
	<span style="color: #000066;">INTEGER</span>   <span style="color: #339933;">::</span> <span style="color: #202020;">total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>, counter, lower, upper
	<span style="color: #000066;">CHARACTER</span> <span style="color: #339933;">::</span> <span style="color: #202020;">response</span>
	<span style="color: #000066;">LOGICAL</span>   <span style="color: #339933;">::</span> <span style="color: #202020;">prime</span>
&nbsp;
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'******************************************'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*                                        *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*     FORTRAN PRIME NUMBER CALCULATOR    *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*                                        *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'******************************************'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Copyright (c) 2011 Jonathan Landrum'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Calculates how many prime numbers are'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'between the lower and upper bound given by'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'the user, and returns the list of those'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'prime numbers.'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'I wanted to use the Sieve of Eratosthenes'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'algorithm, but could not figure out how to'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'make it work. Perhaps later.'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'The output of this program has been checked'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'against this list:'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'http://primes.utm.edu/lists/small/100000.txt'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'--------------------------------------------------'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Would you like to continue? [Y/N]'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> response
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">! Any response beginning with the letter &quot;y&quot; will work.</span>
	<span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>response <span style="color: #339933;">==</span> <span style="color: #ff0000;">'y'</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">OR</span>.</span> response <span style="color: #339933;">==</span> <span style="color: #ff0000;">'Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">DO</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'What is the lower bound of numbers you'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'would like to check?'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> lower
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'What is the upper bound of numbers you'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'would like to check?'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> upper
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">! If the user inputs a smaller number for the upper</span>
		<span style="color: #666666; font-style: italic;">! bound, or if they input the same number, ask again</span>
		<span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>upper <span style="color: #339933;">-</span> lower<span style="color: #009900;">&#41;</span> &lt;<span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">DO</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'ERROR: Upper bound not higher than'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'       lower bound'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'What is the lower bound of numbers you'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'would like to check?'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> lower
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'What is the upper bound of numbers you'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'would like to check?'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> upper
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span> <span style="color: #666666; font-style: italic;">! END while upper is smaller</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">! We have legitimate numbers</span>
		counter <span style="color: #339933;">=</span> lower
		total <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>
		<span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>counter &lt; upper<span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">AND</span>.</span><span style="color: #009900;">&#40;</span>upper &gt; <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">DO</span>
			<span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>Prime<span style="color: #009900;">&#40;</span>counter<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>
				WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>, <span style="color: #ff0000;">'(i0,1x)'</span>, <span style="color: #b1b100;">ADVANCE</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'no'</span><span style="color: #009900;">&#41;</span> counter
				total <span style="color: #339933;">=</span> total <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
			<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">IF</span> <span style="color: #666666; font-style: italic;">! Outputs the number</span>
			counter <span style="color: #339933;">=</span> counter <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span> <span style="color: #666666; font-style: italic;">! Processing loop</span>
&nbsp;
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'The number of primes between '</span>,lower,<span style="color: #ff0000;">' and '</span>,upper,<span style="color: #ff0000;">' is '</span>,total,<span style="color: #ff0000;">'.'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'--------------------------------------------------'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Would you like to continue? [Y/N]'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> response
	<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span> <span style="color: #666666; font-style: italic;">! End Continue loop</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span>//_ Live long and prosper.'</span>
<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">PROGRAM</span> primes
&nbsp;
<span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! FUNCTION Prime:</span>
<span style="color: #666666; font-style: italic;">! Determines if the input is a prime number. Returns T or F.</span>
<span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
<span style="color: #000066;">LOGICAL</span> <span style="color: #b1b100;">FUNCTION</span> Prime<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>
	<span style="color: #000066;">IMPLICIT</span> <span style="color: #000066;">NONE</span>
&nbsp;
	<span style="color: #000066;">INTEGER</span> <span style="color: #339933;">::</span> <span style="color: #202020;">n</span>, i
&nbsp;
	<span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>n &lt;<span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>                             <span style="color: #666666; font-style: italic;">! 1 is not prime (Really, it's not)</span>
		Prime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">FALSE</span>.</span>
	<span style="color: #b1b100;">ELSE</span> <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">OR</span>.</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>          <span style="color: #666666; font-style: italic;">! Hardcode 2 and 3</span>
		Prime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">TRUE</span>.</span>
	<span style="color: #b1b100;">ELSE</span> <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">MOD</span><span style="color: #009900;">&#40;</span>n,<span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>                 <span style="color: #666666; font-style: italic;">! Get rid of evens</span>
		Prime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">FALSE</span>.</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">! All other cases are out, so now we check to see if</span>
	<span style="color: #666666; font-style: italic;">! n is divisible by the odd numbers from 3 on.</span>
	<span style="color: #b1b100;">ELSE</span>
		i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span>
		Prime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">TRUE</span>.</span>                           <span style="color: #666666; font-style: italic;">! Assume it's prime, then prove</span>
		<span style="color: #b1b100;">DO</span>                                       <span style="color: #666666; font-style: italic;">! If i^2 is not a root of n, or if n%i = 0</span>
			<span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">*</span>i &gt; n <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">OR</span>.</span> <span style="color: #993333;">MOD</span><span style="color: #009900;">&#40;</span>n,i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">EXIT</span> <span style="color: #666666; font-style: italic;">!(Won't be larger than the square)</span>
			i <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span>                            <span style="color: #666666; font-style: italic;">! Iterate the counter by 2 to get odds</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">! Record the answer</span>
		<span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">*</span>i &gt; n<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>
			Prime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">TRUE</span>.</span>
		<span style="color: #b1b100;">ELSE</span>
			Prime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">FALSE</span>.</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">IF</span>
	<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">IF</span> <span style="color: #666666; font-style: italic;">! We have our answer</span>
<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">FUNCTION</span> Prime</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/01/30/fortran-prime-number-calculator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automating Terminal Commands in Mac OS X with Bash</title>
		<link>http://jonlandrum.com/2012/01/28/automating-terminal-commands-in-mac-os-x-with-bash/</link>
		<comments>http://jonlandrum.com/2012/01/28/automating-terminal-commands-in-mac-os-x-with-bash/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 22:28:38 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=82</guid>
		<description><![CDATA[Coming from the Linux world, Mac was somewhat familiar to me, yet somewhat foreign. One such example is the system hides most of the files and directories I was accustomed to seeing and using as a Linux user. Also, the &#8230; <a href="http://jonlandrum.com/2012/01/28/automating-terminal-commands-in-mac-os-x-with-bash/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Coming from the Linux world, Mac was somewhat familiar to me, yet somewhat foreign. One such example is the system hides most of the files and directories I was accustomed to seeing and using as a Linux user. Also, the terminal commands to show and re-hide these directories don&#8217;t follow standard Unix terminal syntax. So I had to do some researching to find what the commands even are, and then I could put them in a <a href="http://en.wikipedia.org/wiki/Bash_(Unix_shell)" title="Article on Wikipedia">Bash</a> script that could be run with a single click. But first, the commands:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">defaults <span style="color: #c20cb9; font-weight: bold;">write</span> com.apple.finder AppleShowAllFiles TRUE
<span style="color: #c20cb9; font-weight: bold;">killall</span> Finder</pre></div></div>

<p>The first line is the business end, the second one restarts Finder with the new setting. And you can type these in a Terminal window right now and get the desired results. But what about automating this? Instead of typing all this out &#8212; and possibly misspelling it a time or two (ask me how I know) &#8212; let&#8217;s make a shell script that contains the commands, and keep it in our user directory for easy access:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
defaults <span style="color: #c20cb9; font-weight: bold;">write</span> com.apple.finder AppleShowAllFiles TRUE
<span style="color: #c20cb9; font-weight: bold;">killall</span> Finder</pre></td></tr></table></div>

<p>The only difference this time is the new first line. That specifies which shell we&#8217;re using, in this case Bash. It&#8217;s called the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)" title="Article on Wikipedia">shebang line</a>, and it tells Terminal which program to use to interpret the commands. Copy and paste those three lines into TextEdit or your favorite text editor, and save it as <code>show.sh</code> or something similar. Then change the &#8220;TRUE&#8221; to &#8220;FALSE&#8221; at the end of the <em>second line</em>, like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
defaults <span style="color: #c20cb9; font-weight: bold;">write</span> com.apple.finder AppleShowAllFiles FALSE
<span style="color: #c20cb9; font-weight: bold;">killall</span> Finder</pre></td></tr></table></div>

<p>and save that as <code>hide.sh</code> or something similar. Put these two files in your <strong>user root directory</strong>, in the same place where your Documents, Desktop, Music, and Pictures folders are.</p>
<div id="attachment_90" class="wp-caption alignnone" style="width: 191px"><img src="http://jonlandrum.com/wp-content/uploads/2012/01/directory.png" alt="Example Directory Layout" title="Example Directory Layout" width="181" height="248" class="size-full wp-image-90" /><p class="wp-caption-text">An example directory layout. Put show.sh and hide.sh in your user root like this.</p></div>
<p>Next we have to make them <em>executable</em>. Open Terminal (it will already be <code>cd</code>&rsquo;d to your user root directory) and enter this command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x show.sh <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x hide.sh</pre></div></div>

<p>This will make our two files executable. Now give them a whirl:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>show.sh
$ .<span style="color: #000000; font-weight: bold;">/</span>hide.sh</pre></div></div>

<p>The <code>./</code> tells Terminal to run that program. This should open your Finder window with hidden files and directories showing, but faded to show you the difference. And when you run <code>hide.sh</code> Finder will close and reopen, with those files and directories hidden. If that&#8217;s what you got, then continue to the final step to make them clickable. If not, go back through this tutorial and start from scratch. Make sure you&#8217;ve typed everything correctly.</p>
<p>The last step is open a Finder window and select <code>show.sh</code>, then type Command-i to bring up the information menu. Under the &#8220;Open with:&#8221; dropdown, select &#8220;Other&hellip;&#8221;, and in the window that pops up, scroll to the bottom to the &#8220;Utilities&#8221; directory, open that, and then scroll to Terminal and select it.</p>
<p>You may have to change the &#8220;Enable:&#8221; menu from &#8220;Recommended Applications&#8221; to &#8220;All Applications&#8221; in order to select it. Tick the &#8220;Always Open With&#8221; checkbox, and click &#8220;Add&#8221;. Repeat this last step for <code>hide.sh</code>.</p>
<p>Double-click <code>show.sh</code> and confirm it works, then do the same for <code>hide.sh</code>. If everything works as expected, you&#8217;re all set! You can access these programs from the command line or from Finder the way you&#8217;ve done just now.</p>
<p>If you have other commands you&#8217;d like to automate, you can simply replace what&#8217;s in either of these scripts with that command, save it as a new <code>.sh</code> file, make it executable, set it to open with Terminal, and you&#8217;re golden. For timed automation, you can even have these programs run with <a href="http://en.wikipedia.org/wiki/Cron" title="Article on Wikipedia">cron</a> so that they&#8217;re executed for you on a regular schedule. How&#8217;s <em>that</em> for automation?</p>
<p>~Jonathan</p>
]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/01/28/automating-terminal-commands-in-mac-os-x-with-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solving the Quadratic Equation with Fortran</title>
		<link>http://jonlandrum.com/2012/01/27/solving-the-quadratic-equation-with-fortran/</link>
		<comments>http://jonlandrum.com/2012/01/27/solving-the-quadratic-equation-with-fortran/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 04:29:40 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Fortran]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=77</guid>
		<description><![CDATA[An interesting assignment given last semester was to write a program that returned the results of the Quadratic Equation to find the roots of a quadratic polynomial. What&#8217;s so interesting about that, you ask? The professor wanted the imaginary roots. &#8230; <a href="http://jonlandrum.com/2012/01/27/solving-the-quadratic-equation-with-fortran/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>An interesting assignment given last semester was to write a program that returned the results of the Quadratic Equation to find the roots of a quadratic polynomial. What&#8217;s so interesting about that, you ask? The professor wanted the imaginary roots. In the end, it only took an &#8220;if&#8221; block to get it, but it did take some thought to get it right. At the time, this was one of my more proud moments as a programmer.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
</pre></td><td class="code"><pre class="fortran" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">! ---------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! Programmer:   Jonathan Landrum</span>
<span style="color: #666666; font-style: italic;">! Date:         15 September 2011</span>
<span style="color: #666666; font-style: italic;">! Class:        CSC 204</span>
<span style="color: #666666; font-style: italic;">! ---------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! Program Name: quadraticEquation.exe</span>
<span style="color: #666666; font-style: italic;">! Purpose:      Calculates the roots of a quadratic</span>
<span style="color: #666666; font-style: italic;">!               equation, returning whether they are real</span>
<span style="color: #666666; font-style: italic;">!               roots or imaginary roots.</span>
<span style="color: #666666; font-style: italic;">! Assumptions:  1.) The input is in the form of a real</span>
<span style="color: #666666; font-style: italic;">!                   number, not text. Text will cause the</span>
<span style="color: #666666; font-style: italic;">!                   program to return an error.</span>
<span style="color: #666666; font-style: italic;">! ---------------------------------------------------------</span>
&nbsp;
<span style="color: #b1b100;">PROGRAM</span> quadraticEquation
&nbsp;
	<span style="color: #000066;">IMPLICIT</span> <span style="color: #000066;">NONE</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">! ------------------------------------------------------</span>
	<span style="color: #666666; font-style: italic;">! Variables:</span>
	<span style="color: #666666; font-style: italic;">! The response variable is a single character variable,</span>
	<span style="color: #666666; font-style: italic;">! and input longer than one character will be truncated.</span>
	<span style="color: #666666; font-style: italic;">! So technically, any word starting with the letter &quot;y&quot;</span>
	<span style="color: #666666; font-style: italic;">! will keep the program running. Feature, not a bug(TM).</span>
	<span style="color: #666666; font-style: italic;">! ------------------------------------------------------</span>
	<span style="color: #000066;">REAL</span>		<span style="color: #339933;">::</span> <span style="color: #202020;">a</span>, b, c, d <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>, <span style="color: #993333;">fraction</span>, radical
	<span style="color: #000066;">CHARACTER</span>	<span style="color: #339933;">::</span> <span style="color: #202020;">response</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">! Introduce the program and ask for initial response</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*****************************************'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*                                       *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'* FORTRAN Quadratic Equation Calculator *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*                                       *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*****************************************'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Copyright (c) 2011 Jonathan Landrum'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'This program calculates the roots of the'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'quadratic equation, and returns whether'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'the roots are real or imaginary.'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'The calculation is in the form:'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'(-B +/- (SQRT(B^2 - 4*A*C))'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'---------------------------'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'            2A'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Do you want to continue? [Y/N]'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
&nbsp;
	READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> response <span style="color: #666666; font-style: italic;">! Anything besides Y terminates</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">! -----------------------------------------------------</span>
	<span style="color: #666666; font-style: italic;">! Main Loop:</span>
	<span style="color: #666666; font-style: italic;">! Keeps the user in the program until they choose to</span>
	<span style="color: #666666; font-style: italic;">! close it.</span>
	<span style="color: #666666; font-style: italic;">! -----------------------------------------------------</span>
	<span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>response <span style="color: #339933;">==</span> <span style="color: #ff0000;">'Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">OR</span>.</span> <span style="color: #009900;">&#40;</span>response <span style="color: #339933;">==</span> <span style="color: #ff0000;">'y'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">DO</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">! -------------------------------------------------</span>
		<span style="color: #666666; font-style: italic;">! Input Loop:</span>
		<span style="color: #666666; font-style: italic;">! Takes in three real numbers for calculation.</span>
		<span style="color: #666666; font-style: italic;">! -------------------------------------------------</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Enter a value for &quot;A&quot;:'</span>
		READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> a
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Enter a value for &quot;B&quot;:'</span>
		READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> b
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Enter a value for &quot;C&quot;:'</span>
		READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> c
&nbsp;
		d <span style="color: #339933;">=</span> b<span style="color: #339933;">*</span>b <span style="color: #339933;">-</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">*</span>a<span style="color: #339933;">*</span>c
&nbsp;
		<span style="color: #666666; font-style: italic;">! -------------------------------------------------</span>
		<span style="color: #666666; font-style: italic;">! Computation Loop:</span>
		<span style="color: #666666; font-style: italic;">! Determines if the discriminant is negative, and</span>
		<span style="color: #666666; font-style: italic;">! computes the results.</span>
		<span style="color: #666666; font-style: italic;">! -------------------------------------------------</span>
		<span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>d &lt; <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>
			<span style="color: #993333;">fraction</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>b<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2.0</span><span style="color: #339933;">*</span>a<span style="color: #009900;">&#41;</span>
			radical  <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">SQRT</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">ABS</span><span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2.0</span><span style="color: #339933;">*</span>a<span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'IMAGINARY ROOTS'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #993333;">fraction</span>, <span style="color: #ff0000;">' + '</span>, radical, <span style="color: #ff0000;">'i'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #993333;">fraction</span>, <span style="color: #ff0000;">' - '</span>, radical, <span style="color: #ff0000;">'i'</span>
		<span style="color: #b1b100;">ELSE</span> <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>d <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">EQ</span>.</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'REPEATED REAL ROOTS'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span>b<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2.0</span><span style="color: #339933;">*</span>a<span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">ELSE</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'REAL ROOTS'</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>b <span style="color: #339933;">+</span> <span style="color: #993333;">SQRT</span><span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2.0</span><span style="color: #339933;">*</span>a<span style="color: #009900;">&#41;</span>
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>b <span style="color: #339933;">-</span> <span style="color: #993333;">SQRT</span><span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2.0</span><span style="color: #339933;">*</span>a<span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">IF</span> <span style="color: #666666; font-style: italic;">! End computation Loop</span>
&nbsp;
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Do you want to convert another set of numbers? [Y/N]'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> response <span style="color: #666666; font-style: italic;">! Anything besides Y terminates</span>
	<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span> <span style="color: #666666; font-style: italic;">! End Main Program Loop</span>
&nbsp;
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span>//_ Live long and prosper.'</span>
<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">PROGRAM</span> quadraticEquation</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/01/27/solving-the-quadratic-equation-with-fortran/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plotting Radioactive Decay with Fortran and ES-Plot</title>
		<link>http://jonlandrum.com/2012/01/26/plotting-radioactive-decay-with-fortran-and-es-plot/</link>
		<comments>http://jonlandrum.com/2012/01/26/plotting-radioactive-decay-with-fortran-and-es-plot/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 00:51:08 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Fortran]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=66</guid>
		<description><![CDATA[One of the assignments in Fortran last semester was to plot the exponential population growth of the United States between the years 1790 and 1860. Since I had recently taken Chemistry that summer, I also wrote a program to model &#8230; <a href="http://jonlandrum.com/2012/01/26/plotting-radioactive-decay-with-fortran-and-es-plot/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the assignments in Fortran last semester was to plot the exponential population growth of the United States between the years 1790 and 1860. Since I had recently taken Chemistry that summer, I also wrote a program to model the decay of the radioactive isotope Carbon-14, the isotope used in <a href="http://en.wikipedia.org/wiki/Radiocarbon_dating" title="Article on Wikipedia">Radiocarbon Dating</a>.</p>
<p>This program calculates the data, exports it to a <abbr title="Comma-separated values">CSV</abbr> file, and finally calls the <a href="http://www.neng.usu.edu/mae/faculty/stevef/prg/ESPlot/index.html" title="Download ES-Plot">ES-Plot</a> program and feeds it the CSV for data. An example output is below:</p>
<img src="http://jonlandrum.com/wp-content/uploads/2012/01/decay.png" alt="Carbon-14 Decay Model" title="Carbon-14 Decay Model" width="616" height="458" class="size-full wp-image-67" />
<p>In the graphic above, the vertical axis is number of moles present, and the horizontal axis is the number of years since the start of decay. It is difficult to read the numbers, but the first labeled tick mark is &ldquo;1E+4&rdquo;, 10,000 years. The average half-life of Carbon-14 is 5,730&#177;40 years, not labeled here. The last tick mark is 40,000 years.</p>
<p>The user can input any quantity in moles for the program to calculate, but calculation will stop at 0.01% of the calculated mass because anything less than that is trivial in this case and is difficult to see in the resulting graphic. You will notice, therefore, that subsequent calculations result in the same graphic. That is because the decay rate of Carbon-14 is the same, regardless of quantity.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</pre></td><td class="code"><pre class="fortran" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! Programmer:   Jonathan Landrum</span>
<span style="color: #666666; font-style: italic;">! Date:         22 October 2011</span>
<span style="color: #666666; font-style: italic;">! Class:        CSC 204</span>
<span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! Program:      decay.f95</span>
<span style="color: #666666; font-style: italic;">! Purpose:      Calculates and plots the radioactive decay</span>
<span style="color: #666666; font-style: italic;">!               of Carbon-14. The half-life of Carbon-14 is</span>
<span style="color: #666666; font-style: italic;">!               5,730 +/- 40 years. I have chosen 5,730 for</span>
<span style="color: #666666; font-style: italic;">!               this model's lambda, as it is the mean.</span>
<span style="color: #666666; font-style: italic;">! Assumptions:  1.) ES-Plot is installed.</span>
<span style="color: #666666; font-style: italic;">!                   http://goo.gl/wmhZw</span>
<span style="color: #666666; font-style: italic;">! ----------------------------------------------------------</span>
&nbsp;
<span style="color: #b1b100;">PROGRAM</span> decay
&nbsp;
    <span style="color: #000066;">IMPLICIT</span> <span style="color: #000066;">NONE</span>
&nbsp;
    <span style="color: #000066;">CHARACTER</span>                   <span style="color: #339933;">::</span> <span style="color: #202020;">response</span>
    <span style="color: #000066;">INTEGER</span>                     <span style="color: #339933;">::</span> <span style="color: #202020;">t</span>
    <span style="color: #000066;">REAL</span>                        <span style="color: #339933;">::</span> <span style="color: #202020;">p0</span>, mass
    <span style="color: #000066;">DOUBLE</span> <span style="color: #993333;">PRECISION</span>, <span style="color: #000066;">PARAMETER</span> <span style="color: #339933;">::</span> <span style="color: #202020;">e</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2.71828182845904523536</span> <span style="color: #666666; font-style: italic;">!     ln 0.5</span>
    <span style="color: #000066;">DOUBLE</span> <span style="color: #993333;">PRECISION</span>, <span style="color: #000066;">PARAMETER</span> <span style="color: #339933;">::</span> <span style="color: #202020;">k</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">0.0001209680943385594</span> <span style="color: #666666; font-style: italic;">! k = ------</span>
                                                              <span style="color: #666666; font-style: italic;">!     lambda</span>
&nbsp;
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*******************************************'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*                                         *'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*FORTRAN CARBON RADIOACTIVE DECAY MODELLER*'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*                                         *'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*******************************************'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Copyright (c) 2011 Jonathan Landrum'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Calculates and plots the radioactive decay'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'of Carbon-14 to the stable Nitrogen isotope'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Nitrogen-14, its most common isotope.'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'The half-life of Carbon-14 is 5,730 +/- 40'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'years. I have chosen 5,730 for this model,'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'as it is the mean.'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Assumptions: 1.) ES-Plot is installed.'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'                 http://goo.gl/wmhZw'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'------------------------------------------'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Would you like to continue? [Y/N]'</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
    READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> response
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">! Any response beginning with the letter &quot;y&quot; will work.</span>
    <span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>response <span style="color: #339933;">==</span> <span style="color: #ff0000;">'y'</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">OR</span>.</span> response <span style="color: #339933;">==</span> <span style="color: #ff0000;">'Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">DO</span>
&nbsp;
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'How many moles of Carbon-14 do you want'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'to test?'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
        READ   <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> p0
&nbsp;
        t      <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>
        mass   <span style="color: #339933;">=</span> p0
&nbsp;
        OPEN   <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">UNIT</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">8</span>, <span style="color: #b1b100;">FILE</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'decay.csv'</span>, <span style="color: #b1b100;">status</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'unknown'</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #cc66cc;">1</span>       FORMAT <span style="color: #009900;">&#40;</span>1X,I6,A2,F6.3<span style="color: #009900;">&#41;</span>
&nbsp;
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Year,   Mass'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'------------'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'$ Carbon-14 Decay Model'</span>
&nbsp;
        <span style="color: #b1b100;">WHILE</span>  <span style="color: #009900;">&#40;</span>mass &gt;<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>p0 <span style="color: #339933;">*</span> <span style="color: #cc66cc;">0.01</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">DO</span>
            mass  <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>p0 <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>e <span style="color: #339933;">**</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">*</span> t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            WRITE <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> t, <span style="color: #ff0000;">', '</span>, mass
            WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> t, <span style="color: #ff0000;">', '</span>, mass
            t     <span style="color: #339933;">=</span> t <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
        <span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span>
&nbsp;
        CLOSE  <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span>
&nbsp;
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'------------'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'End of list.'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'File &quot;decay.csv&quot; successfully created.'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Calling ESPlot...'</span>
&nbsp;
        <span style="color: #b1b100;">CALL</span> SYSTEM <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'&quot;C:<span style="color: #000099; font-weight: bold;">\P</span>rogram Files (x86)<span style="color: #000099; font-weight: bold;">\E</span>SPlot<span style="color: #000099; font-weight: bold;">\e</span>splot.exe&quot; decay.csv'</span><span style="color: #009900;">&#41;</span>
&nbsp;
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Process Complete'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'------------------------------------------'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Would you calculate another mass? [Y/N]'</span>
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
        READ   <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> response
        WRITE  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span> <span style="color: #666666; font-style: italic;">! Continue loop</span>
    WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span>//_ Live long and prosper.'</span>
<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">PROGRAM</span> decay</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/01/26/plotting-radioactive-decay-with-fortran-and-es-plot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Birthday Problem</title>
		<link>http://jonlandrum.com/2012/01/25/birthday-problem/</link>
		<comments>http://jonlandrum.com/2012/01/25/birthday-problem/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 18:33:49 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Fortran]]></category>

		<guid isPermaLink="false">http://jonlandrum.com/?p=54</guid>
		<description><![CDATA[In Probability and Stats last semester, my professor assigned us the project of determining how many students there would need to be in a class in order to have a 50/50 chance of having a repeat birth date. It&#8217;s called &#8230; <a href="http://jonlandrum.com/2012/01/25/birthday-problem/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In Probability and Stats last semester, my professor assigned us the project of determining how many students there would need to be in a class in order to have a 50/50 chance of having a repeat birth date. It&#8217;s called the <a href="http://en.wikipedia.org/wiki/Birthday_problem" title="Article on Wikipedia about the paradox">Birthday Problem</a> or Birthday Paradox. He mentioned that it&#8217;s quite a popular problem in probability classes, and that we cannot look online for the answer.</p>
<p>He wanted us to use our intuition to form a method for finding the answer. Obviously, since I&#8217;m a <abbr title="Computer Science">CS</abbr> major, I turned to the computer for my answer. I was taking Fortran that same semester, so it was the freshest language on my mind. Any programming language would probably do, but I chose this one. A lot of what I&#8217;ve seen on the web with this problem is written in <a href="/category/c/" title="Code I&rsquo;ve written in C/C++">C or C++</a>, so I feel I have a slight advantage here. ;~] Upon compiling and running the program, I came to my answer in less than one second: 23.</p>
<p>~Jonathan</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="fortran" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">! ---------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! Programmer:      Jonathan Landrum</span>
<span style="color: #666666; font-style: italic;">! Date:            20 September 2011</span>
<span style="color: #666666; font-style: italic;">! Class:           MAT 353</span>
<span style="color: #666666; font-style: italic;">! ---------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">! Program:         birthdays.exe</span>
<span style="color: #666666; font-style: italic;">! Purpose:         Calculates the number of people you</span>
<span style="color: #666666; font-style: italic;">!                  would need to question in order to have</span>
<span style="color: #666666; font-style: italic;">!                  a 50% chance in getting two or more</span>
<span style="color: #666666; font-style: italic;">!                  duplicate birth dates among the set.</span>
<span style="color: #666666; font-style: italic;">! Assumptions:     None.</span>
<span style="color: #666666; font-style: italic;">! ---------------------------------------------------------</span>
&nbsp;
<span style="color: #b1b100;">PROGRAM</span> BIRTHDAYS
	<span style="color: #000066;">IMPLICIT</span> <span style="color: #000066;">NONE</span>
&nbsp;
	<span style="color: #000066;">REAL</span>      <span style="color: #339933;">::</span> <span style="color: #202020;">probability</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span>, compliment <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1.0</span>, days <span style="color: #339933;">=</span> <span style="color: #cc66cc;">365</span>
	<span style="color: #000066;">INTEGER</span>   <span style="color: #339933;">::</span> <span style="color: #202020;">counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>
	<span style="color: #000066;">CHARACTER</span> <span style="color: #339933;">::</span> <span style="color: #202020;">response</span>
&nbsp;
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'******************************************'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*                                        *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'* FORTRAN DUPLICATE BIRTHDATE CALCULATOR *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'*                                        *'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'******************************************'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Calculates the number of people you would'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'need to question in order to have a 50/50'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'chance in having a duplicate birthdate.'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'This program takes no arguments from the'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'user, except to continue after this line.'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Would you like to continue? [Y/N]'</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> response
&nbsp;
	<span style="color: #666666; font-style: italic;">! Any response beginning with the letter &quot;y&quot; will do.</span>
	<span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>response <span style="color: #339933;">==</span> <span style="color: #ff0000;">'y'</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">OR</span>.</span> response <span style="color: #339933;">==</span> <span style="color: #ff0000;">'Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">DO</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">! -------------------------------------------------</span>
		<span style="color: #666666; font-style: italic;">! Main:</span>
		<span style="color: #666666; font-style: italic;">! This loop begins at 1 with the calculation and</span>
		<span style="color: #666666; font-style: italic;">! continues until probability is greater than 50%.</span>
		<span style="color: #666666; font-style: italic;">! -------------------------------------------------</span>
		<span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>probability &lt; <span style="color: #cc66cc;">0.51</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">DO</span>
			compliment  <span style="color: #339933;">=</span> compliment <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>days<span style="color: #339933;">/</span><span style="color: #cc66cc;">365</span><span style="color: #009900;">&#41;</span>
			probability <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #339933;">-</span> compliment
&nbsp;
			WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'| '</span>, counter, <span style="color: #ff0000;">' | '</span>, probability, <span style="color: #ff0000;">' |'</span>
&nbsp;
			counter <span style="color: #339933;">=</span> counter <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
			days    <span style="color: #339933;">=</span> days <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span> <span style="color: #666666; font-style: italic;">! Main loop</span>
&nbsp;
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'Would you like to continue? [Y/N]'</span>
		WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
		READ  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> response
	<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span> <span style="color: #666666; font-style: italic;">! Continue loop</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>
	WRITE <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>,<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span>//_ Live long and prosper.'</span>
<span style="color: #b1b100;">END</span> <span style="color: #b1b100;">PROGRAM</span> BIRTHDAYS</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonlandrum.com/2012/01/25/birthday-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.846 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-05 00:19:12 -->
<!-- Compression = gzip -->
