Category Archives: Math

POW Answer – Circle, Square, Circle

Last week’s problem involved using your geometry/trigonometry skills to find areas of shapes. We start off with a large (black) circle which has a (black) square inscribed in it which in turn has a (red) circle inscribed in it:
Circle Inscribed In A Square Inscribed In Another Circle

  1. Let’s start off with the equation for the area of a circle: A = π × r2 (where r is the radius)
  2. Now let’s think about the equation for area of a square: A = s2 (where s is the length of one of the sides)
  3. Next we can say that the radius of the inner circle is r1.
  4. After that let’s find the area of the red inner circle relative to r1: A1 = π × r12
  5. Now let’s find the length of the diagonal of the square (d) in which the red circle is inscribed relative to r1. This will also be the diameter of the outer circle
    1. s = r1 + r1 = 2 × r1
    2. The above is true because the red circle is inscribed in the square.
    3. d2 = s2 + s2
    4. d2 = 2 × s2
    5. d = (2 × s2)1/2
    6. d = s × 21/2
    7. d = 2 × r1 × 21/2
  6. Next we should find the area of the black circle (A2), in which the black square is inscribed, relative to r1.
    1. We will use r2 to represent the radius of the outer circle:
      1. r2 = d / 2
      2. r2 = (2 × r1 × 21/2) / 2
      3. r2 = r1 × 21/2
    2. A2 = π × r22
    3. A2 = π × (r1 × 21/2)2
    4. A2 = π × r12 × 2
  7. Lastly we should find the area of the black outer circle while excluding the area covered by the red inner circle:
    1. Let’s make the area of the black doughnut shape be represented by A3.
    2. A3 = A2 - A1
    3. A3 = (π × r12 × 2) - (π × r12)
    4. A3 = (π × r12) + (π × r12) - (π × r12)
    5. A3 = π × r12

So after doing all of the math by using a little geometry and a little algebra we end up with the A1 (red area) being equal to A3 (black area).
Filled Circles

Even though the image makes it look like there is more red than black, there really isn’t. Interesting stuff, huh? :cool:

POW – Circle, Square, Circle

Recently a friend asked why I haven’t put up any new POWs (Problems of the Week). Part of the reason is because I got lazy but the other part is because the show Numb3rs is what inspired me before and I haven’t been watching any reruns lately :razz:. Yesterday I started watching a new show called Scorpion which has a little math in it and the second episode got me thinking about inscribed shapes. Take the following three shapes for example:
Circle Inscribed In A Square Inscribed In Another Circle

Above is a picture of a circle inscribed in a square which itself is inscribed in another circle. Now if I make the inner circle red and the remaining area black the image ends up looking as follows:
Filled Circles

Using the information given, is the red area bigger or the black area? If you remember a few things from trigonometry/geometry I’m sure this math problem will be a breeze :cool:. As usual the answer to this problem of the week became available a week after the POW was published and can be found here.

JavaScript – Fraction Part of a Number

One thing that is common knowledge about JavaScript and other languages is that floating point numbers are not always stored the way you think. Also doing math with these numbers is even more iffy. One thing I recently wanted to do was effectively the opposite of parseInt(). In other words, I just wanted the fractional part of the passed number to be returned. Thusly the following function was born:

/**
 * @license Copyright 2014 - Chris West - MIT Licensed
 */
(function(RGX) {
    frac = function(num) {
        return +(+num).toExponential().replace(RGX, function(m, neg, num, dot, offset) {
            var zeroes = Array(Math.abs(offset) + 2).join('0');
            num = (zeroes + num + (dot ? '' : '.') + zeroes).split('.');
            return +(neg + '.' + num.join('').slice(+offset + num[0].length));
        });
    };
})(/(-?)(\d+(\.?)\d*)e(.+)/);

I really got the idea from the first programming language I ever used: that of the Casio graphing calculators. The following are example calls and results for the function:

var a = frac(1.234);     // 0.234
var b = frac(56789e-3);  // 0.789
var c = frac(12345678);  // 0
var d = frac(-34.5697);  // -0.5697
var e = frac('-.9');     // -0.9
var f = frac(null);      // 0
var g = frac(undefined); // NaN
var h = frac('sdfa');    // NaN

Personally I thought it was strange that this function isn’t natively provided in most languages but there are some alternatives. One that works in some languages but not in JavaScript (nor in Python interestingly enough) is the following:

a = 34.5697;
fracPart = a % 1;
alert(fracPart);  // 0.5696999999999974

Another alternative method that seems like it should work but doesn’t in JavaScript is as follows:

a = 34.5697;
fracPart = a - parseInt(a);
alert(fracPart);  // 0.5696999999999974

For this reason, if you have a need to pull the decimal portion of a number out, feel free to use the frac function defined at the beginning of this post. 8-)