Showing posts with label decimals. Show all posts
Showing posts with label decimals. Show all posts

Wednesday, July 6, 2016

The Garden Border Problem

The following problem is in the McDougal Littell Algebra 2 textbook and is a typical word problem for a section on solving quadratic equations: 

You have just planted a rectangular flower bed of red roses in a park near your home.  You want to plant a border of yellow roses around the flower bed as shown.  Since you bought the same number of red and yellow roses, the areas of the border and inner flower bed will be equal.  What should the width x of the border be? 


To solve this problem, you must first write an area equation.  The length of the whole garden is 12 feet plus the unknown widths of the left and right borders, which can be expressed as 2x + 12.  The width of the whole garden is 8 feet plus the unknown widths of the top and bottom borders, which can be expressed as 2x + 8.  The area of the whole garden is the area of the red rose garden (which is 8 feet times 12 feet or 96 feet squared) plus the area of the yellow rose garden (which is the same as the red rose garden or also 96 feet squared), which added together is 192 feet squared.  Since the area is length times width, the equation to solve is (2x + 12)(2x + 8) = 192.

The next step is to solve this area equation.  Multiplying (2x + 12)(2x + 8) gives us 4x2 + 40x + 96, so 4x2 + 40x + 96 = 192, and subtracting 192 to the left side gives us 4x2 + 40x – 96 = 0, and dividing everything by the common factor 4 gives us x2 + 10x – 24 = 0.  At this point, there are several methods for solving this quadratic (such as factoring, completing the square, quadratic equation, and graphing) but we will use the quadratic equation x = -b ± √(b^2 – 4ac) / 2a, where a = 1, b = 10, and c = -24.  Therefore, x = -10 ± √(10^2 – 4·1·-24) / 2·1 = -10 ± √(100 + 96) / 2 = -10 ± √196 / 2 = -10 ± 14 / 2, which means x = -12 or x = 2.  Since x represents a geometrical dimension, it cannot be negative, and therefore the border width x must be 2 feet long.

You will notice that this answer conveniently comes out as an integer, and not as a decimal.  But what would happen if the problem started out with different dimensions for the inner garden?  Would the border width still be an integer?  Let us examine the same problem but with a starting inner garden of 8 feet by 8 feet, as pictured below:

  
This time both the length and the width of the whole garden can be expressed as 2x + 8, and the area of the whole garden is 2 times 8 feet by 8 feet, or 128 feet squared, giving us the equation (2x + 8)(2x + 8) = 128.  Multiplying (2x + 8)(2x + 8) gives us 4x2 + 32x + 64 = 128, subtracting 128 to the left side gives us 4x2 + 32x – 64 = 0, and dividing everything by the common factor 4 gives us x2 + 8x – 16 = 0.  Using the quadratic equation x = -b ± √(b^2 – 4ac) / 2a, where a = 1, b = 8, and c = -16 gives us x = -8 ± √(8^2 – 4·1·-16) / 2·1 = -8 ± √(64 + 64) / 2 = -8 ± √128 / 2 = -8 ± 8√2 / 2 = -4 ± 4√2.  Since x cannot be negative, the border width must be -4 + 4√2 feet long, which is not an integer answer.

Can we come up with different dimensions for the inner garden such that the border width solution comes out as an integer?  We already know one solution set is (8, 12, 2) from the original problem, and using the properties of proportions and dividing each number by two we can also include (4, 6, 1).  In fact, using the same argument we can include all solution sets in the form of (4k, 6k, k) where k is any positive integer.  To simplify things, we will say that (4, 6, 1) is a “garden border triple” that includes all solutions sets in the form (4k, 6k, k), so the garden border triple (4, 6, 1) includes (4, 6, 1), (8, 12, 2), (12, 18, 3), and so on (just like the Pythagorean triple (3, 4, 5) includes all solution sets in the form (3k, 4k, 5k)).

Are there other garden border triples other than (4, 6, 1)?  Just as there are different Pythagorean triple solutions to the formula a2 + b2 = c2 ((3, 4, 5), (5, 12, 13), etc.), there are also different garden border triples.  And just as there are different Pythagorean triple generators (see here), there are different garden border triple generators.  To make one, we must generalize the garden border problem by calling the length of the inner garden b and the width of the inner garden h, as pictured below:


The length of the whole garden can then be expressed as 2x + b, the width of the whole garden can be expressed as 2x + h, and the area of the whole garden can be expressed as 2bh, giving us the equation (2x + b)(2x + h) = 2bh.  This time, however, we are going to solve this equation for b.  Multiplying (2x + b)(2x + h) gives us 4x2 + 2bx + 2hx + bh = 2bh, subtracting bh on both sides gives us 4x2 + 2bx + 2hx = bh, subtracting 2bx on both sides gives us 4x2 + 2hx = bh – 2bx, factoring 2x from the left side and b from the right side gives us 2x(2x + h) = b(h – 2x), and dividing both sides by h – 2x gives us b = 2x(h + 2x)/h – 2x.

We can now use the formula b = 2x(h + 2x)/h – 2x to generate garden border triples.  If we let x = 1, then b = 2(h + 2)/h – 2.  Then if h = 3, b = 2(3 + 2)/3 – 2 = 10, then the garden border triple is (3, 10, 1).  If h = 4, b = 2(4 + 2)/4 – 2 = 6, then the garden border triple is (4, 6, 1) (which is a repeat of a triple we already knew).  If h = 5, b = 2(5 + 2)/5 – 2 = 14/3, then the garden border triple is (5, 14/3, 1), and to eliminate the fraction we can multiply each number by 3 to get (15, 14, 3).  Continuing on in this fashion, we also arrive at (6, 4, 1) (a repeat), (7, 18/5, 1) ≡ (35, 18, 5), (8, 10/3, 1) ≡ (24, 10, 3), (9, 22/7, 1) ≡ (63, 22, 7), (10, 3, 1) (a repeat) and so on.  If we let x = 2, then b = 4(h + 4)/h – 4, and the resulting garden border triples are (5, 36, 2), (6, 20, 2) ≡ (3, 10, 1) (a repeat), (7, 44/3, 2) ≡ (21, 44, 6), (8, 12, 2) ≡ (4, 6, 1) (a repeat), (9, 52/5, 2) ≡ (45, 52, 10), (10, 28/3, 2) ≡ (15, 14, 3) (a repeat), and so on.  If we let x = 3, then b = 6(h + 6)/h – 6, and the resulting garden border triples are (7, 78, 3), (8, 42, 3), (9, 30, 3) ≡ (3, 10, 1) (a repeat), (10, 24, 3), and so on. 


The garden border problem is a common word problem given to students to practice solving quadratic equations.  Most solutions come out as a decimal answer, but there are a few scenarios in which the width, length, and border width are all integers, which we called garden border triples.  Generalizing the problem in terms of b and h and solving for b gave us a garden border triple generator b = 2x(h + 2x)/h – 2x.  The unique garden border triples we generated in this article were (3, 10, 1), (4, 6, 1), (15, 14, 3), (35, 18, 5), (24, 10, 3), (63, 22, 7), (5, 36, 2), (21, 44, 6), (45, 52, 10), (7, 78, 3), (8, 42, 3), and (10, 24, 3); but there are many, many more.


Monday, March 14, 2016

Using Your Computer to Calculate Pi

On March 21, 2015, Rajveer Meena recited 70,000 digits of pi from memory in just under 10 hours in Vellore, India, for the Guinness World Record of most digits of pi memorized.  Apart from utter amazement by this feat of memory, have you ever wondered how mathematicians can even calculate such a long list numbers for pi?


There are actually several methods for calculating pi.  You might recall from your math classes that pi is the ratio between the circumference and diameter of any circle, and so one way to calculate pi is to construct a circle, directly measure its circumference and diameter, and divide the two.  Unfortunately, this is only precise enough to obtain a few decimal places of pi.  Another way to calculate pi is to find the perimeter or areas of inscribed or circumscribed polygons of circles.  This was the technique favored by mathematicians before the invention of calculators and computers, but its precision is still limited to “just” a few hundred digits of pi.

inscribed octagon

Today, computers can be used to calculate millions of digits of pi by using infinite series formulas.  There are several different ways to do this, but one of the most efficient methods that is relatively straightforward is to combine Machin’s formula:


with the arc-cotangent power series formula:


The computer program for this is as follows:

01
# Pi Calculator
02
# Python 2.7.3
03
# After running, type "pi(n)" where n is the number of decimals for pi.  For
04
#  example, if you would like to calculate 100 decimals for pi, type "pi(100)".
05

06
# import python libraries
07
from decimal import Decimal, getcontext
08
from time import time, strftime
09
import datetime
10

11
# arccot function using power formula arccot = 1/x - 1/(3x^3) + 1/(5x^5) ...
12
def arccot(x, digits):
13
    # set precision and starting values
14
    getcontext().prec = digits
15
    total = 0
16
    n = 1
17
    # loop while new term is large enough to actually change the total
18
    while Decimal((2 * n - 1) * x ** (2 * n - 1)) < Decimal(10 ** digits):
19
        # find value of new term
20
        term = ((-1)**(n - 1)) * 1 / Decimal((2 * n - 1) * x ** (2 * n - 1))
21
        # add the new term to the total
22
        total += term
23
        # next n
24
        n += 1
25
    # return the sum
26
    return total
27

28
# pi function
29
def pi(decimals):
30
    # start timer
31
    timestart = time()
32

33
    # find pi using Machin's Formula pi = 4 * (4 * arccot(5) - arccot(239))
34
    #  and the power formula for arccot (see arccot function above)
35
    print "pi = " + str(Decimal(4 * (4 * arccot(5, decimals + 3) - arccot(239,
36
        decimals + 3))).quantize(Decimal(10) ** (-decimals)))
37

38
    # display elapsed time
39
    timeelapsedint = round(time() - timestart, 2)
40
    timeelapsedstr = str(datetime.timedelta(seconds = round(
41
        timeelapsedint, 0)))
42
    print "runtime: " + timeelapsedstr + " or " + str(
43
        timeelapsedint) + " seconds."

Here’s what happens when you use the program to find the first 1,000 decimal places for pi on a dual-core 1.67 GHz processor (a mediocre-speed laptop for 2016):

>> pi(1000)
pi = 3.14159265358979323846264338327950288419716939937510
582097494459230781640628620899862803482534211706798214808
651328230664709384460955058223172535940812848111745028410
270193852110555964462294895493038196442881097566593344612
847564823378678316527120190914564856692346034861045432664
821339360726024914127372458700660631558817488152092096282
925409171536436789259036001133053054882046652138414695194
151160943305727036575959195309218611738193261179310511854
807446237996274956735188575272489122793818301194912983367
336244065664308602139494639522473719070217986094370277053
921717629317675238467481846766940513200056812714526356082
778577134275778960917363717872146844090122495343014654958
537105079227968925892354201995611212902196086403441815981
362977477130996051870721134999999837297804995105973173281
609631859502445945534690830264252230825334468503526193118
817101000313783875288658753320838142061717766914730359825
349042875546873115956286388235378759375195778185778053217
12268066130019278766111959092164201989
runtime: 0:00:04 or 3.73 seconds.
  
So with less than 50 lines of code, you can have your computer calculate 1,000 decimal places of pi in less than 4 seconds!  At this rate, you will have 1,000,000 decimal places of pi in just over an hour!  Not bad, considering the very first computer, the ENIAC, took 70 hours to calculate pi to 2,037 decimal places in 1949.  Computers have come a long way since then!


With better computers and more efficient (but more complicated) infinite series formulas, pi has been calculated to over 13.3 trillion digits!  This number is unfathomable to most of us.  If you were to recite this many digits of pi at the same speed as the 2015 world record holder Meena (about 2 digits per second), it would take you over 200,000 years to finish!  So why bother to find so many digits of pi?   Most mathematicians will give the same reason that mountain climbers use for climbing Mount Everest: because it’s there.


Wednesday, May 6, 2015

Calculating a Square Root

In Terry Pratchett’s book Hogfather, a series of hilarious events leads a professor named Ponder Stibbons to wait up for the Disc World’s version of Santa Claus.  “I’m waiting for the Hogfather, thought Ponder Stibbons.  I’m in the dark waiting for the Hogfather.  Me.  A believer in Natural Philosophy.  I can find the square root of 27.4 in my head. (He’d have to admit that the answer would be ‘five and a bit,’ but at least he could come up with it.)  I shouldn’t be doing this.”


This begs the question: how do you find the square root of any number in your head?  Like Ponder Stibbons, we can easily find the closest square number and get a good estimate: 27.4 is just over 25, which is 52, so the square root of 27.4 is “5 and a bit.”  But is there an algorithm for finding the square root of any number mentally, or with paper and a pencil?  These days it’s so easy to mindlessly plug in the problem into a nearby calculator (on a cell phone, tablet, or computer) that we don’t stop to think about how it can be done.

There are actually several different ways of calculating a square root of a number, but we will focus on one particular method.  First, start by guessing an answer.  (The closer the guess is to the correct answer, the less steps we will need to take in the long run.)  Let’s guess that the square root of 27.4 is 5.  Second, we divide our guess into the number being square-rooted.  In our example, 27.4 divided by 5 is 5.48.  Therefore, we know that the square root of 27.4 is somewhere between 5 and 5.48.  Third, we will take the average of those two numbers (5.24) and repeat the process with 5.24 as our new guess.  Here is the result of a few rounds of calculations:

Guess
Divide
Average
5
27.4 / 5 = 5.48
5.24
5.24
27.4 / 5.24 = 5.229007634
5.234503817
5.234503817
27.4 / 5.234503817 = 5.234498046
5.234500931
5.234500931
27.4 / 5.234500931 = 5.234500931
5.234500931

In this case, after the second round of calculations we have an answer that is accurate to five decimal places, and after the third round of calculations we have an answer that is accurate to at least nine decimal places.

Some of us may be able to do the first round of calculations for finding a square root in our heads, but even Professor Ponder Stibbons would have a difficult time with subsequent rounds.  In our example above, dividing 27.4 by 5.24 is no easy task (although it can be done).  Now the question becomes: how can we program a computer to find a square root using only the basic mathematical functions?

Algebraically, we can express this process as an iterated function.  Let p equal the number to be square rooted, x equal the guess, and y equal the square root.  Then y is the average of p/x and x, or y = (p/x + x) / 2, or y = p + x^2/2x.  In our case above, p = 27.4 and x0 = 5, which means x1 = 27.4 + 5^2/2∙5 = 5.24, x2 = 27.4 + 5.24^2/2∙5.24 = 5.234503817, and so on.  The actual square root would be the fixed point of this iterated function, which can be found by letting y = x.  Therefore, y = p + y^2/2y, and multiplying both sides by 2y, 2y2 = p + y2, and subtracting y2 from both sides, y2 = p or y = √p.  Since we have expressed this process as an iterated function, the first round’s solution can be represented as x1 = (p + x02)/(2x0), the second round’s solution as x2 = (p + x12)/(2x1), the third round’s solution as x3 = (p + x22)/(2x2), and so on.  Using substitution and a little bit of algebra, we can deduce that x2 = (p2 + 6px02 + x04)/(4x0(p + x0^2)) and x3 = (p4 + 28p3x02 + 70p2x04 + 28px06 + x08)/(4x0(p + x02)(p2 + 6px02 + x04)), where each equation is a closer and closer approximation to y = √p.  In fact, using x0 = 5, the graph of x3 and √p are nearly indistinguishable from about 0 < p < 250, and x3 > √p when p > 250.

Graph of x3 when x0 = 5 and y = √p
(0 < p < 900, 0 < y < 30)

The equation for x2 or x3 may give a good approximation of a square root for numbers less than 250, but we ought to have something that works well for all numbers.  Fortunately, iterated functions are loops, and pretty much all computer programs can loop the same set of commands until a certain criteria is met.  In this case, we would want the computer to loop through the formula p + x^2/2x for subsequent terms of x until the same decimal approximation is reached for two subsequent terms.  The following is a program that does exactly that in the computer language Python:

01
#Square Root Function
02
#Python 2.7.3
03

04
#function to determine if a string is a number
05
def is_number (n):
06
    #if the number can successfully change into a float then return true
07
    try:
08
        float(n)
09
        return True
10
    #otherwise return false.
11
    except:
12
        return False
13

14
#function to determine if a number is positive
15
def is_positive (n):
16
    #first check to make sure n is really a number
17
    if (is_number(n)):
18
        #if the number is less than zero then return false
19
        if (float(n) < 0):
20
            return False
21
        #otherwise return true
22
        else:
23
            return True
24
    #if n is not a number return false
25
    else:
26
        return False
27

28
#function to find the square root without using a built-in square root function
29
def square_root (p):
30
    #first check to make sure p is a positive number
31
    if is_positive(p):
32
        #assign variables
33
        p = float(p)
34
        r = 9 #number of decimal places to round to
35
        x_old = 5 #starting guess for x0
36
        #loop values in y = (p + x^2)/(2x) until x_old = x_new (rounded)
37
        while True:
38
            #find x_new using y = (p + x^2)/(2x)
39
            x_new = (p + x_old * x_old) / (2 * x_old)
40
            #round x_old and x_new
41
            x_old_round = "{:.{prec}f}".format(x_old, prec=r)
42
            x_new_round = "{:.{prec}f}".format(x_new, prec=r)
43
            #if x_old and x_new are equal, then stop and return value
44
            if (x_old_round == x_new_round):
45
                return float(x_new_round)
46
                break
47
            #make x_old the x_new value and start the loop over again
48
            x_old = x_new
49
    #if p is not a positive number then return an error message
50
    else:
51
        return "ERROR"

Running the above program and typing in the command to use the square_root function for 27.4 results in the following answer:

>> 
square_root(27.4)
5.234500931

This accurate and quick result only needed about 50 lines of code (and only 30 lines if you take out all the comments).

Therefore, there is an easy algorithm for finding the square root of a number.  If you are good at mental math and can remember the formula p + x^2/2x, you could even impress your friends by finding the square root to the nearest hundredth place (well, at least Ponder Stibbons would be impressed).  But the best approximation for a square root requires several iterations of the formula p + x^2/2x which can be done quickly with a short computer program.