POW – JavaScript Self Inequality

Assuming a normal JavaScript engine, what could you possibly pass into the following mystery() function that would not thrown an error?

function mystery(a) {
  if (a === a) {
    throw new Error('The first argument has self equality.');
  }
}

As is normal, this answer will be shown a week from when it was posted and will be linked to from here.The answer to this POW can now be found here.

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

POW – Multiple Choice Question?

This Problem of the Week will make you question whether or not multiple choice questions really are easier. Here is the question:

Assuming the answer can be selected from the following list, if you were to choose one of the following at random, what is the chance that you would be right in some way?

  1. 25
  2. 20%
  3. Zero
  4. 40%
  5. 1/5

Post your answers as comments below. The actual answer to this will be posted next Monday.

POW Answer – Explain That SQL #1

To answer last week’s POW, the purpose of the SQL was to generate a random string of letters and numbers. I actually wrote two different JavaScript function that can produce the same results. The following is the slightly more straight-forward solution:

function randomChars(len) {
  for(var n, s = ""; --len;) {
    n = parseInt(Math.random() * 62);
    s = String.fromCharCode(n + (n < 26 ? 65 : n < 52 ? 71 : -4));
  }
  return s;
}

The next version is a bit harder to follow because it involves recursion:

function recRandomChars(len) {
  if(!len){ return ""; }
  var n = parseInt(Math.random() * 62);
  return recRandomChars(--len)
    + String.fromCharCode(n + (n < 26 ? 65 : n < 52 ? 71 : -4));
}

One practical application of this is to generate a password with random characters.