JavaScript – Degree & Radian Conversion

Two very simple operations that you may have to deal with if writing a JavaScript that deals with trigonometry are Math.degrees() and Math.radians(). These function can be easily defined as follows:

// Converts from degrees to radians.
Math.radians = function(degrees) {
  return degrees * Math.PI / 180;
};

// Converts from radians to degrees.
Math.degrees = function(radians) {
  return radians * 180 / Math.PI;
};

The name of these functions indicates the end result. Using the above definitions, you could run code such as the following to get the results indicated in the comments:

alert(Math.radians(90));  // 1.5707963267948966
alert(Math.radians(180)); // 3.141592653589793

alert(Math.degrees(1.5707963267948966)); // 90
alert(Math.degrees(3.141592653589793));  // 180

Personally, I don’t have any use for this at the moment, but if you do, feel free to take it and use it in your code. I figured if it is available in a language like Python, it might as well be available in the best scripting language, JavaScript!!! 8)

JavaScript – Rounding with Precision

One of the things that bothers me about the native rounding functions is the fact that there is no way to specify precision. If this is something that you need to do you can use the following code to define Math.$round(...), Math.$ceil(...), and Math.$floor(...):

(function(arr,i,name) {
  while(name = arr[i++]) {
    Math["$"+name] = Function("a","b","return Math."+name+"(a*(b=Math.pow(10,b||0)))/b");
  }
})(["floor","ceil","round"],0);

This code Makes the following functions available:

  • Math.$ceil(number [, precision]) → Number:
    Returns the specified number rounded up just as Math.ceil(number) does, but also gives the ability to specify the precision of the rounded number.

  • Math.$floor(number [, precision]) → Number:
    Returns the specified number rounded down just as Math.floor(number) does, but also gives the ability to specify the precision of the rounded number.

  • Math.$round(number [, precision]) → Number:
    Returns the specified number rounded up or down just as Math.round(number) does, but also gives the ability to specify the precision of the rounded number.

Specifying a positive precision will round according to that digit to the right of the decimal point. Specifying a negative precision will round according to that digit to the left of the decimal point.

POW Answer – Appending = Adding Squares

This post gives the answer to last week’s Problem of the Week.

First of all, I have to admit that this problem actually came from the Quicker Maths blog. This blog actually pulled the problem from IBM’s Ponder this section.

First of all, in order to write the equation algebraicly, we could express the relationship as follows:
x2 + y2 = x + y × 1000

Unfortunately, the difficulty with solving the problem with just pencil and paper is the fact that we have to solve for the variables when they are both integers. For this reason, let’s do it with a programming language such as Python:

arr = []
for x in range(1000, 10000):
    for y in range(x, 10000):
        sum = x * x + y * y
        if sum == x * 10000 + y:
            arr.append("x = {0}, y = {1} => {2}".format(x, y, sum))
        elif sum == y * 10000 + x:
            arr.append("x = {0}, y = {1} => {2}".format(y, x, sum))

print("x * x + y * y = x * 10000 + y where:")
print("\n".join(arr))

Using this code will result in the following being printed out:

x * x + y * y = x * 10000 + y where:
x = 9412, y = 2353 => 94122353

As you can see, this was the brute-force way of solving this problem, but sometimes the brute-force way is the fastest way to come to the correct answer. 8)

POW – Appending = Adding Squares

Did you know that 882 + 332 = 8833? How about the fact that 9902 + 1002 = 990100? That is pretty interesting, huh? My question for you all is, are there any two 4-digit numbers with this same relationship? In other words, are there two 4-digit numbers which when appended to each other will be equal to the sum of the squares of both numbers?

The answer for this Problem of the Week will become available next Monday.

POW Answer – Multiple Choice Question?

If you remember the problem, the key is the fact that the problem never said to choose one of the answers from the multiple choice list. Also, the question never said that that question was the one that applied to the multiple choice questions. Having those two facts in mind, you now have to do the math and figure out what the odds actually are that you at least got the question “right in some way“. Since that phrase is used, “right in some way“, we can assume that more than one of the options are the answer. Knowing this, what are all of the possible answer permutations?

  1. a
  2. b
  3. c
  4. d
  5. e
  6. ab
  7. ac
  8. ad
  9. ae
  10. bc
  11. bd
  12. be
  13. cd
  14. ce
  15. de
  16. abc
  17. abd
  18. abe
  19. acd
  20. ace
  21. ade
  22. bcd
  23. bce
  24. bde
  25. cde
  26. abcd
  27. abce
  28. abde
  29. acde
  30. bcde
  31. abcde

As shown above, we have 31 different answers that could be right. Since we have been limited to picking one letter at random, to determine the odds that your answer at least partially matches one of the answers listed above, you will have to multiply 31 by 5. That comes to 155 total possible scenarios. Now the final part of the problem is to determine how many of those times the random answer that is chosen would actually be correct. Since we can only pick one letter at random and it has to be one of the ones listed above, the number of characters that appear above is the same as the total amount of times that you will be correct. There are 80 letters shown above. In conclusion, the chance that you will at least be partially correct is 80 out of 155 or about 52%.

I know that many people probably disagree with me on this one, but in the end, since I wrote the problem, I am not sure I can be wrong on this one. ;) Still, thanks for the feedback in advanced.