Perl programming: follow-up from Sept 29

Tutoring math or sciences, you need to remind your students to “show their work.”  The tutor observes the same precedent in computer science.

Back in my last article, I discussed the changes to the Perl compound interest calculator so it could cover all the compounding possibilities. Though it accomplished its objective, that discussion left some loose ends, one of which I’ll attend to now.

Issue 1: Notice the “#” sign in the sixth line of code:

$ppyear=$ARGV[3];#compounding periods per year

Normally, you can’t just add text to a computer instruction and have the program still work. However, in Perl, “#” means “comment”: following the “#” sign, the rest of the line will be ignored by the computer.

Comments are used to tell the reader the functionality of lines of code. In the example above, $ppyear=$ARGV[3]; introduces a new variable. The comment after the “#” tells its purpose.

Even with small programs, the functionality of certain lines may be hard to decipher at a glance – even for the author of the code! If a few weeks have passed since you’ve looked at your program, you might not easily recall what a given line does. Part of the skill of programming is knowing what lines need to be explained, as well as how to explain them concisely with little comments. Even one or two good comments can make a program much easier to understand at a glance.

Putting the discussion into scholastic perspective, comments are how programmers “show their work.” Showing one’s work is really about explaining the ideas behind what’s written down. When the ideas are known, marks can be easier to give – and programs can be easier to fix. When you’re unsure of what you’re doing – that can be the best time to offer explanation:)

Source: Robert Pepper’s Perl Tutorial.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Perl: A compound interest calculator, Part II

Compound interest is probably studied more in high school than it was twenty years ago.  The tutor amends the perl program from September 10 to cover the general case of compound interest.

Back in my September 10 article, I offered a short Perl program that calculates compound interest with annual compounding. However, what about monthly, weekly, daily, or other compounding frequencies? Today, I’ll show the changes needed for the program to cover all possibilities.

For those wanting a refresher on the general formula for compound interest – or else on what compounding means – see my article here. For those curious about the ideas behind the Perl program in the September 10 article, search this blog for perl. Otherwise, click, on right pane, (or right here) the computer science category; you’ll find the perl articles in there.

The new program follows. The changes from the original are in lilac. Those changes enable the program to cover the general case – that is, where the compounding may be more than annually.


#!/usr/bin/perl
$principal =$ARGV[0];
$percent=$ARGV[1];
$rate=$ARGV[1]/100;
$time=$ARGV[2];
$ppyear=$ARGV[3];#compounding periods per year
$futurevalue=$principal*(1+$rate/$ppyear)**($time*$ppyear);
print “The principal amount is $principal\n”;
print “The annual interest rate is $percent percent\n”;
print “The compoundings per year is $ppyear\n”;

print “The time duration of the investment is $time years\n\n”;
print “The future value of the intestment is $futurevalue\n\n”;

Now, when you run this program from the command line, you’ll need to add in one last parameter: the compoundings per year. If the compounding is monthly, the number will be 12; weekly will mean 52.

Example: Imagine the new, improved compound interest calculator is called intcalc1.txt. Let’s further imagine you want to use it to calculate the future value of an investment of $2500, at interest rate 3.2%, compounded monthly, over a term of 5 years. What command would you enter at the terminal to accomplish that calculation?

Solution: Assuming you are in the proper directory (see my article here if you need a primer), you will enter the following:

perl intcalc1.txt 2500 3.2 5 12

If all goes well, you might be greeted with the following response:


The principal amount is 2500
The annual interest rate is 3.2 percent
The compoundings per year is 12
The time duration of the investment is 5 years

The future value of the investment is 2933.152481…

While there is more to say about this program, today’s purpose is satisfied. I’ll be extending the discussion in future posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Calculus: the derivative of a^x

Tutoring calculus, you might be asked about the derivative of a^x from time to time. The math tutor shows the trick to this old favourite.

In most reference frames,

    \[a\^\ x=a^x\]

.

The expression on the left is more often seen connected with calculation devices; the one on the right, more often in textbooks.

Whichever way you see it, how would one go about taking its derivative?

Example: Find the derivative of a^x

Solution:

As in so many cases, the trick here is to rewrite the expression to a form more easily penetrable by derivative techniques. With this example, we’ll realize that e^x and lnx are inverses. Therefore,

(1)   \begin{equation*}a^x=e^{lna^x}\end{equation*}

Using the “bring the exponent down in front of a log” rule, we can proceed to

(2)   \begin{equation*}e^{lna^x}=e^{xlna}\end{equation*}

Now, we recall the derivate rule

(3)   \begin{equation*}\frac{d}{dx}e^{f(x)}=f\'(x)e^{f(x)}\end{equation*}

Since lna is a constant, the derivate of xlna is just lna. Therefore,

(4)   \begin{equation*}\frac{d}{dx}e^{xlna}=(lna)e^{xlna}\end{equation*}

We know, from steps 1) and 2) above, that

(5)   \begin{equation*}a^x=e^{xlna}\end{equation*}

Therefore,

(6)   \begin{equation*}\frac{d}{dx}a^x=\frac{d}{dx}e^{xlna}=(lna)e^{xlna}=(lna)a^x\end{equation*}

I’ll be talking more about derivatives in future posts. Cheers:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Math: Proof that the square root of 2 is irrational

Tutoring math, this idea rarely surfaces.  However, it’s essential to number theory, a favourite of math professors.  The tutor discusses it for interest’s sake.

The square root of n is the number x such that x(x)=n. Perhaps more to the point: if x is the square root of n, then

    \[x^2=n\]

Put in everyday terms, the square root is the number your “multiply by itself” to arrive at the original one. Therefore, 5 is the square root of 25.

I’ve written many posts that concern square roots: here and here are just two examples.

If you key \sqrt{2} into your calculator, you’ll find it’s 1.414213562…., which is an irrational number. An irrational number is a decimal that neither ends, nor follows a repeating pattern. Such a number cannot be written as a fraction of integers; in contrast, a rational number can.

Indirect proof

Let’s imagine we wanted to prove that \sqrt{2} is irrational. We could begin by assuming the opposite: that it is rational. Exploring the implications, we might arrive at a contradiction. Said contradiction will prove that \sqrt{2} can’t be rational. Then, the remaining conclusion will be that it’s irrational. Such an approach – where you assume the opposite, then prove it can’t be true – is called indirect proof.

Let’s begin, therefore, by assuming \sqrt{2} is rational. Then,

(1)   \begin{equation*}\sqrt{2}=\frac{a}{b}\end{equation*}

where a and b are integers.

We’ll assume that \frac{a}{b} is in reduced form (that is, that a and b have no factors in common).

We can follow along with

(2)   \begin{equation*}2=\frac{a^2}{b^2}\end{equation*}

Which leads to

(3)   \begin{equation*}2b^2=a^2\end{equation*}

Since a^2 is 2 times another integer, it must be even. b must then be odd, since a and b are assumed to have no factors in common.

If a^2 is even, then a must be. After all, you can’t get an even number by multiplying two odds. Therefore,

(4)   \begin{equation*}a=2k\end{equation*}

where k is some other integer.

We can proceed to

(5)   \begin{equation*}2=\frac{a^2}{b^2}=\frac{(2k)^2}{b^2}=\frac{4k^2}{b^2}\end{equation*}

However,

(6)   \begin{equation*}2=\frac{4k^2}{b^2}\end{equation*}

leads to

(7)   \begin{equation*}2b^2=4k^2\end{equation*}

which gives, when we divide both sides by two,

(8)   \begin{equation*}b^2=2k^2\end{equation*}

Now we see that b^2 is even, implying that b must also be. Yet, we know a is even and that a and b share no common factors. Therefore, b can’t be even; otherwise, it will have 2 as a common factor with a.

The contradiction that b is proven to be even , while our earlier assumption prohibits its being so, proves that \frac{a}{b} is not rational. Therefore, \sqrt{2} cannot be a rational number; it must, instead, be irrational.

As the school term develops, I’ll no doubt be drawn to more practical topics. Cheers:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Math: short division

Tutoring math, short division is rarely the aim – yet, it’s often a means.  The math tutor introduces it.

 
I used short division back in my May 4, 2013 post about finding square roots and cube roots. I didn’t explain the process then, though I did show it. Now, I’ll give an explanation.

Short division is great, especially for one-digit division. Here’s an example:

Calculate 70101÷3.

Solution: To use short division, you’ll likely rewrite the question as

Now, proceed as follows:

Start by asking yourself, like you would with long division, how many times 3 goes into 7. It goes in twice with one left over. Write the 2 below the 7. Then, put the 1 behind the 7 like so:

Now, the 1 with the zero behind it makes a “10”. Ask yourself how many times 3 goes into 10. It goes three times, with 1 left over. Write the 3 below the zero of the 10. Write the 1 in front of the next number, also (in this case) a 1:

Now, ask yourself how many times 3 goes into 11. It’s three times, with 2 left over. Write the 3 under the ones digit of the 11; write the 2 in front of the next number, the second 0.

We imagine now that the 2 with the zero behind it forms 20. We ask how many times 3 goes into 20? The answer is 6 times, with 2 left over. We write the 6 under the 0, the 2 in front of the next number, which is the 1:

Finally, we ask how many times 3 goes into 21. The answer is 7, with 0 left over. We write the 7 below the 1. Since there are no more numbers left, the remainder is 0:

Observations:

1) It definitely helps to use a different color to work the question from the one it’s written in.

2) Short division might be less prone to errors than long, because there is so much less to write down.

There are still a couple more points to mention about short division. We’ll visit them in a future post:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Calculator usage: a tutor’s perspective

Tutoring math, you are surrounded by calculators.  The math tutor tells a story.

 
Years ago I realized that knowing “how to do” a process in math is being eclipsed by “how to use” a calculator. First, I saw the TI graphing calculator back in 1990. Very few people had one; those did weren’t always allowed to use it on tests. However, they were allowed on some tests and on “for-marks” homework. Back then, that calculator cost around $300, I was told. It was more than I was paying per university course.

One of my friends had one of those big TIs. At the time, I had a little “bare-bones” TI scientific (around $30). Sitting in the theatre seats of a 2nd semester calculus lecture, I challenged him to a race: who could (using our calculators) more quickly find the fifth root of 2. I won, of course; he didn’t know where to find the variable root function on his calculator. It was in a menu which itself he had to find. He did manage to get the answer around five minutes later.

While people were impressed with those big calculators, some were missing the point: having the expensive calculator with way more functions didn’t necessariy mean an advantage. I recall a calculus exam to which you could bring any device you wanted. Once again, for me it was the TI “bare-bones” scientific.

Today, the TI graphing calculator – itself around half the price it was then – is still great for graphing. However, in high school math, you don’t need the graphing function too often. That’s not to say it’s not handy – just that you don’t “need” it very often. Some teachers won’t allow its use on tests. I only use it to show other people how to – or else on rare occasions when I have a math problem of my own to solve outside of tutoring.

The TI “bare-bones” scientific is still great for high school and university math. Similar to the TI grapher, it’s around half the price it was in 1990. It – or its SHARP or CASIO equivalent – is what I still use – as well as most of my students most of the time. (For the last ten or fifteen years, most people prefer the SHARP scientific, which is usually a few dollars more.)

The calculator issue complicates learning math because it gives more choices. I’ll be continuing this vein in coming posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Math: compound interest, general case

Tutoring math 11 and 12, you encounter this formula.  The math tutor discusses the general case of compound interest.

I’ve discussed compound interest and exponential growth in numerous posts:here, here, and here. However, the focus has been on annual compounding, given by

    \[A=P(1+r)^t\]

where A=accumulated amount, P=principal (the amount invested), r=annual interest rate in decimal form, and t=time in years.

Just to clarify: when interest is compounded, it becomes part of the principal. Then it, too, can start earning interest.

What about monthly compounding, which is applied to many charge accounts, etc? How is it calculated? The general formula is then used:

    \[A=P(1+\frac{r}{n})^{(tn)}\]

where n=number of compounding periods per year. (For monthly compounding, n=12.)

Example: Calculate the balance of an investment of $500 left in an account for five years, if the interest rate is 3.1% compounded monthly.

Solution:

Recall that 3.1% means 0.031.

    \[A=500(1+\frac{0.031}{12})^{(5(12))}=583.71\]

Now let’s repeat the example, but with weekly instead of monthly compounding:

    \[A=500(1+\frac{0.031}{52})^{(5(52))}=583.80\]

In today’s world of investing and credit, everyone needs to understand compound interest. I’ll bring more coverage of it in future posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Chemistry: what is a mole?

Tutoring high school chemistry, the concept of a mole is paramount.  The tutor offers this light introduction to pave the way for more….

Phrases you might overhear from high school chemistry are “molar mass” or “How many atoms in a mole of water?” To non-chemists, these ideas might need clarification.

A mole is 6.02 x 1023 particles. The number 6.02 x 1023 is known as Avogadro’s number, in honor of the chemist who focused on the numbers of molecules that react during chemical processes between gases. Some people just call 6.02×1023 the molar number.

In practical terms, Avogadro’s number is the number of atoms that, for each element, yields the mass in grams that you see on the periodic table. The masses are the decimal numbers. For instance, if you look at carbon, you’ll see its mass is 12.0. This means that the mass of one mole of carbon atoms – aka, its molar mass – is 12.0 grams. One mole of carbon atoms is, of course, 6.02×1023 atoms of carbon.

One mole of water – which is, of course, H2O – contains two moles of hydrogen (H) and one of oxygen (O). The total atom count is 3 moles, or 3(6.02×1023) = 1.81×1024 atoms.

In our everyday life, we have our convenient quantities of things – a dozen, for instance. Chemists have their own convenient numbers, one of which is 6.02×1023, the molar number. It’s an inescapable concept for high school chemistry students:)

Source: Mortimer, Charles E. Chemistry. Belmont, California: Wadsworth, 1986.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

English: Parallel structures (parallelism)

Tutoring high school and college English, you mention parallelism.  The tutor introduces it.

Non-parallel:  I like drawing, reading, and to play the piano.

Parallel:         I like drawing, reading, and playing the piano.

Let’s compare the two examples above.  In the Parallel one, all the speaker’s activities are listed in the same form; therefore, parallelism is achieved. In the Non-parallel one, drawing  and reading are in -ing (gerund) form, while to play is in infinitive form.

Parallel structures are parts of a sentence that have similar construction.  What creates parallelism is the way those parts are written rather than their meaning.

In the academic world, parallelism is viewed fondly –  failure to execute it, disapprovingly. Last I heard, English 12 government exams are marked with consideration for parallel structures.  A sentence like the “Non-parallel” example above would likely receive a deduction for “faulty parallelism.”

While English is mainly about content, markers like style as well.  A few nice touches – like parallelism – can elevate a teacher’s perception of a paper from “good” to “very good”, or from “very good” to “great.”  Hopefully, that paper will be yours:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Chemistry: percent composition, intro

Tutoring high school chemistry, this topic is perennial.  The tutor embarks it gently on this warm, late-summer night….

Most people are familiar with the fact that water is H20: a water molecule is made from two hydrogen atoms combined with one oxygen atom. However, perhaps not as many people realize that water is only 11% hydrogen.

The reason that water is only 11% hydrogen, even though it has two hydrogen atoms but only one oxygen: percent composition is calculated from mass rather than number of atoms. Looking at the periodic table, we see that hydrogen (H) has a mass of 1.0 (the mass is the decimal number); oxygen (O) has mass 16.0 (rounded to one decimal place). Percent composition is given by the following:

%comp=(mass of specific element)/total mass

To get water’s total mass, add the masses of the two hydrogen atoms plus the one oxygen atom: 1.0+1.0+16.0=18.0. In water, the percent composition of hydrogen is

%comp hydrogen=(mass H present)/(total mass H2O)=(1.0+1.0)/18.0=11%

The percent composition of oxygen in water is

(mass O present)/(total mass H2O)=16/18=89%

Of course, the percent hydrogen and the percent oxygen must add to 100%, since they are the only two elements present. It’s comforting, therefore, that 11% + 89% = 100%:)

When calculating percent composition, chemists use mass rather than number of atoms. Some measures of concentration are determined from a similar perspective. I’ll pursue this idea in future posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.