Plotting Radioactive Decay With Fortran and ES-Plot

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 Radiocarbon Dating.

This program calculates the data, exports it to a CSV file, and finally calls the ES-Plot program and feeds it the CSV for data. An example output is below:

Carbon-14 Decay Model

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 “1E+4”, 10,000 years. The average half-life of Carbon-14 is 5,730±40 years, not labeled here. The last tick mark is 40,000 years.

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.

~Jonathan

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
! ----------------------------------------------------------
! Programmer:   Jonathan Landrum
! Date:         22 October 2011
! Class:        CSC 204
! ----------------------------------------------------------
! Program:      decay.f95
! Purpose:      Calculates and plots the radioactive decay
!               of Carbon-14. The half-life of Carbon-14 is
!               5,730 +/- 40 years. I have chosen 5,730 for
!               this model's lambda, as it is the mean.
! Assumptions:  1.) ES-Plot is installed.
!                   http://goo.gl/wmhZw
! ----------------------------------------------------------
 
PROGRAM population
 
    IMPLICIT NONE
 
    CHARACTER                   :: response
    INTEGER                     :: t
    REAL                        :: p0, mass
    DOUBLE PRECISION, PARAMETER :: e = 2.71828182845904523536 !     ln 0.5
    DOUBLE PRECISION, PARAMETER :: k = -0.0001209680943385594 ! k = ------
                                                              !     lambda
 
    WRITE (*,*) '*******************************************'
    WRITE (*,*) '*                                         *'
    WRITE (*,*) '*FORTRAN CARBON RADIOACTIVE DECAY MODELLER*'
    WRITE (*,*) '*                                         *'
    WRITE (*,*) '*******************************************'
    WRITE (*,*)
    WRITE (*,*) 'Copyright (c) 2011 Jonathan Landrum'
    WRITE (*,*)
    WRITE (*,*) 'Calculates and plots the radioactive decay'
    WRITE (*,*) 'of Carbon-14 to the stable Nitrogen isotope'
    WRITE (*,*) 'Nitrogen-14, its most common isotope.'
    WRITE (*,*)
    WRITE (*,*) 'The half-life of Carbon-14 is 5,730 +/- 40'
    WRITE (*,*) 'years. I have chosen 5,730 for this model,'
    WRITE (*,*) 'as it is the mean.'
    WRITE (*,*)
    WRITE (*,*) 'Assumptions: 1.) ES-Plot is installed.'
    WRITE (*,*) '                 http://goo.gl/wmhZw'
    WRITE (*,*)
    WRITE (*,*) '------------------------------------------'
    WRITE (*,*)
    WRITE (*,*) 'Would you like to continue? [Y/N]'
    WRITE (*,*)
    READ  (*,*) response
    WRITE (*,*)
 
    ! Any response beginning with the letter "y" will work.
    WHILE (response == 'y' .OR. response == 'Y') DO
 
        WRITE  (*,*) 'How many moles of Carbon-14 do you want'
        WRITE  (*,*) 'to test?'
        WRITE  (*,*)
        READ   (*,*) p0
 
        t      = 0
        mass   = p0
 
        OPEN   (UNIT=8, FILE='decay.csv', status='unknown')
 
1       FORMAT (1X,I6,A2,F6.3)
 
        WRITE  (*,*) 'Year,   Mass'
        WRITE  (*,*) '------------'
        WRITE  (8,*) '$ Carbon-14 Decay Model'
 
        WHILE  (mass >= (p0 * 0.01)) DO
            mass  = (p0 * (e ** (k * t)))
            WRITE (8,*) t, ', ', mass
            WRITE (*,1) t, ', ', mass
            t     = t + 1
        END DO
 
        CLOSE  (8)
 
        WRITE  (*,*)
        WRITE  (*,*) '------------'
        WRITE  (*,*) 'End of list.'
        WRITE  (*,*)
        WRITE  (*,*) 'File "decay.csv" successfully created.'
        WRITE  (*,*) 'Calling ESPlot...'
 
        CALL SYSTEM ('"C:\Program Files (x86)\ESPlot\esplot.exe" decay.csv')
 
        WRITE  (*,*) 'Process Complete'
        WRITE  (*,*)
        WRITE  (*,*) '------------------------------------------'
        WRITE  (*,*) 'Would you calculate another mass? [Y/N]'
        WRITE  (*,*)
        READ   (*,*) response
        WRITE  (*,*)
    END DO ! Continue loop
    WRITE (*,*) '\\//_ Live long and prosper.'
END PROGRAM population

Birthday Problem

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’s called the Birthday Problem or Birthday Paradox. He mentioned that it’s quite a popular problem in probability classes, and that we cannot look online for the answer.

He wanted us to use our intuition to form a method for finding the answer. Obviously, since I’m a CS 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’ve seen on the web with this problem is written in C or C++, 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.

~Jonathan

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
! ---------------------------------------------------------
! Programmer:      Jonathan Landrum
! Date:            20 September 2011
! Class:           MAT 353
! ---------------------------------------------------------
! Program:         birthdays.exe
! Purpose:         Calculates the number of people you
!                  would need to question in order to have
!                  a 50% chance in getting two or more
!                  duplicate birth dates among the set.
! Assumptions:     None.
! ---------------------------------------------------------
 
PROGRAM BIRTHDAYS
	IMPLICIT NONE
 
	REAL      :: probability = 0.0, compliment = 1.0, days = 365
	INTEGER   :: counter = 1
	CHARACTER :: response
 
	WRITE (*,*) '******************************************'
	WRITE (*,*) '*                                        *'
	WRITE (*,*) '* FORTRAN DUPLICATE BIRTHDATE CALCULATOR *'
	WRITE (*,*) '*                                        *'
	WRITE (*,*) '******************************************'
	WRITE (*,*)
	WRITE (*,*) 'Calculates the number of people you would'
	WRITE (*,*) 'need to question in order to have a 50/50'
	WRITE (*,*) 'chance in having a duplicate birthdate.'
	WRITE (*,*)
	WRITE (*,*) 'This program takes no arguments from the'
	WRITE (*,*) 'user, except to continue after this line.'
	WRITE (*,*)
	WRITE (*,*) 'Would you like to continue? [Y/N]'
	WRITE (*,*)
	READ  (*,*) response
 
	! Any response beginning with the letter "y" will do.
	WHILE (response == 'y' .OR. response == 'Y') DO
 
		! -------------------------------------------------
		! Main:
		! This loop begins at 1 with the calculation and
		! continues until probability is greater than 50%.
		! -------------------------------------------------
		WHILE (probability < 0.51) DO
			compliment	= compliment * (days/365)
			probability	= 1.0 - compliment
 
			WRITE (*,*) '| ', counter, ' | ', probability, ' |'
 
			counter	= counter + 1
			days	= days - 1
		END DO ! Main loop
 
		WRITE (*,*)
		WRITE (*,*) 'Would you like to continue? [Y/N]'
		WRITE (*,*)
		READ  (*,*) response
	END DO ! Continue loop
	WRITE (*,*)
	WRITE (*,*) '\\//_ Live long and prosper.'
END PROGRAM BIRTHDAYS

Collatz Conjecture

I enjoy reading geeky webcomics. It’s one of my guilty pleasures. xkcd is my favorite, by far. A few weeks ago, I was perusing the xkcd store and found the Collatz Conjecture t-shirt (I’m also incredibly partial to geeky shirts.) I wasn’t familiar with the premise of this conjecture, so I hopped over to Wikipedia to see what it’s about.

According to the article, The Collatz Conjecture is the hypothesis that if you follow the algorithm presented, you will always end up at 1. The algorithm is to start with any natural number (a positive integer), and test it: if it’s odd, multiply your number by 3 and add 1; if it’s even, divide it by 2. Continue the algorithm indefinitely, and, according to the conjecture, you will always end up at 1.

That’s the idea, anyway. But it’s never been proven to be true. It’s assumed to be true, but not proven. (It hasn’t been proven because you cannot test every positive integer.) So it’s called a conjecture rather than a rule. And that’s where I started programming. I figured, hey, that’s easy. I’ll just write a program to prove it for me! Well, sort of. There’s an infinite set of positive integers, but a finite amount of memory in my computer. It will eventually run out of memory space on the machine. But it would still theoretically run forever on a perfect computer. What follows is no formal proof, but it’s about as close to a Q.E.D. as we will see for now in this problem. It starts with 2, proves that it goes to 1 (2 / 2 = 1), and then iterates to 3:

  1. 3 * 3 + 1 = 10
  2. 10 / 2 = 5
  3. 5 * 3 + 1 = 16
  4. 16 / 2 = 8
  5. 8 / 2 = 4
  6. 4 / 2 = 2
  7. 2 / 2 = 1

and so on. Now just to find a machine with infinite memory…

~Jonathan

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
// *****************************************************
// Programmer:		Jonathan Landrum
// Date:		16 December 2011
// *****************************************************
// Program:		collatz.java
// Purpose:		Testing the Collatz Conjecture
// Assumptions:		None.
// *****************************************************
 
public class collatz {
 
	// ---------------------------------------------
	// main():
	// Does ALL the things
	// ---------------------------------------------
	public static void main (String[] args) {
 
		// -------------------------------------
		// Variables
		// -------------------------------------
		int n, in = 2, out = 1;
		Scanner scan = new Scanner(System.in);
 
		// -------------------------------------
		// Introduce the program
		// -------------------------------------
		System.out.println ("---------------------------");
		System.out.println ("-                         -");
		System.out.println ("- Collatz Conjecture Test -");
		System.out.println ("-                         -");
		System.out.println ("---------------------------");
		System.out.println ();
		System.out.println ("Takes the positive integers");
		System.out.println ("and tests them against the");
		System.out.println ("Collatz Conjecture.");
		System.out.println ();
 
		// -------------------------------------
		// Main block
		// -------------------------------------
		while (out == 1) {
			n = in;
			while (n > 1) {
				if (n%2 == 0) {
					n = n/2;
				} else {
					n = n * 3 + 1;
				} // End if
			} // End while
			out = n;
			System.out.println (in);
			in++;
		} // End while
	} // End main
} // End collatz

FizzBuzz

I stumbled upon an article on Coding Horror the other day whilst perusing the web, and it made my stomach turn. Go read it, honestly. Right now. The premise of the article is there are people graduating from colleges and universities with degrees in Computer Science who can neither read nor write one single line of code.

And not just Associates or Bachelors, no. Full-fledged Masters and Doctorates who don’t know a lick of code, but they’ve earned the highest attainable degree in the field of computing. Granted, there’s definitely a philosophical side to Computer Science, and there are many Computer Science programs in academia that only focus on that end; it is, after all, a science. But if you, the applicant, are applying for a coding job, at least have the wit to familiarize yourself with the language first. If all you know is the science of computing and not the languages thereof, apply for one of those jobs instead.

To weed these codeless candidates out, hiring managers in many shops have taken to giving simple programming assignments to candidates. The one mentioned in the article above is called “FizzBuzz”. Here is my version, written in Java, that I whipped up in about five minutes. It is, quite literally, a Freshman’s problem. I won’t lie, I was a bit insulted at how easy the assignment is. I know you’re not supposed to post the answer. I understand that it’s not the point, that the point is we shouldn’t need FizzBuzz challenges. But I took that article as a personal affront. I had to prove myself, no matter how trivial the test.

“I am not one of them.”

There’s much more code to follow. FizzBuzz is just the beginning.

~Jonathan

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
// ****************************************************
// Programmer:           Jonathan Landrum
// Date:                 23 January 2012
// Class:                Comp. Sci. 101 (Seriously)
// ****************************************************
// Program:              FizzBuzz.java
// Purpose:              To prove that I can program.
// Assumptions:          A lot, apparently.
// ****************************************************
 
import java.util.Scanner;
 
public class FizzBuzz {
	public static void main (String[] args) {
 
		// --------------------------------------------
		// Variables
		// --------------------------------------------
		int	c = 1;
		String	response;
		Scanner scan = new Scanner(System.in);
 
		// --------------------------------------------
		// Introduce the program
		// --------------------------------------------
		System.out.println ("-------------------------------------");
		System.out.println ("-             Fizz Buzz             -");
		System.out.println ("-------------------------------------");
		System.out.println ();
		System.out.println ("This program reproduces a traditional");
		System.out.println ("British children's counting game, but");
		System.out.println ("more specifically, it represents a");
		System.out.println ("coding challenge.");
		System.out.println ();
		System.out.print   ("Would you like to continue? [Y/N] ");
 
			response   = scan.nextLine();
 
		// --------------------------------------------
		// Main block
		// --------------------------------------------
		while (response.charAt(0) == 'y' || response.charAt(0) == 'Y') {
			while (c <= 100) {
				if (c > 3) {
					if (c % 3 == 0 && c % 5 == 0) {
						System.out.println ("FizzBuzz");
					} else if (c % 3 == 0) {
						System.out.println ("Fizz");
					} else if (c % 5 == 0) {
						System.out.println ("Buzz");
					} else {
						System.out.println (c);
					} // End test if block
				} else {
					System.out.println (c);
				} // End print if block
				c++;
			} // End main while block
 
			System.out.println ("-------------------------------------");
			System.out.println ("Process complete.");
			System.out.print   ("Would you like to continue? [Y/N] ");
 
				response   = scan.nextLine();
 
		} // End while
		System.out.println ();
		System.out.println ("\\\\//_ Live long and prosper.");
		} // End main
} // End FizzBuzz