Posts

2^(3^(4^(5^(6^(7^(8^9)))))) - Part II

The hint!!!! Hope it sheds some light: let's assume that we want to get the last 10 decimal digits of another big number, such as: 8^1234. We can do it by using the mod operator: (8^1234)%10000000000. Try Bing , it will give you the answer: 4154645504 Now, instead of raising 8 to 1234, let's now assume that we want the last 10 digits of an even larger number: 8^45671234. Same approach, use the mod operator, unfortunately this time the number is too big for either Bing, or Google, or even WolfranAlpha to return an answer, but anyways, I got the answer to (8^45671234)%10000000000: 0273045504 Now, take a look at these two numbers and look at the common digits towards the end: (8^1234)%10000000000          = 41546 45504 (8^(45671234)%10000000000 = 02730 45504 See the "coincidence"? If we're raising a number K to N where N is a number with D digits, the last (D+1) digits of the result will be the same as if we raise the numb...

A non-recursive trick for subsets generation

The problem in this post is related to subsets, and the solution shows a nice trick to avoid mystical recursive solutions. The enumeration for the problem is simple: given a set S of numbers (integers) and a target number N, print all the subsets S' of S such that Summation(S') = N. The trick to generate the subsets lies in the binary representation of a number. Suppose that we want to generate all the subsets of a set with 3 elements. We know from basic set theory that the number of subsets of this set is 2^3, also known as 8. Now let's see the binary representation of all the numbers from 0 to 7: 0 = 0 0 0 1 = 0 0 1 2 = 0 1 0 3 = 0 1 1 4 = 1 0 0 5 = 1 0 1 6 = 1 1 0 7 = 1 1 1 The bits interpretation here should be: " if the bit is 1, then the element at that position belongs to the subset ". Hence take for instance the number 3: 3 = 0 1 1 Since our original set has 3 elements, say {a,b,c}, this third subset should contain only elements b and c sin...

2^(3^(4^(5^(6^(7^(8^9)))))) - Part I

The problem comes from the IBM Ponder This October 2014 Challenge: http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/Challenges/October2014.html What are the last 10 decimal digits of 2^(3^(4^(5^(6^(7^(8^9))))))? This is a big number. If you just take 8^9, you'll end up with 134,217,728. Now try raising, say, 2 to the power of this number, or 2^134,217,728. For comparison sake, the number of atoms in the universe is less than 2^324. The problem is asking for the last 10 decimal digits, so that should have been an important clue. It should have rang a bell for two key ideas behind solving this problem: 1) Modular Arithmetic : to get the last 3 digits of 1234, all I have to do is calculate 1234%(10^3) 2) Big Integers . Try Binging for Big Integers in C# . Solution in the next post. Have fun!

Smells like Dijkstra's Algorithm

If a solution to a problem smells like Dijkstra's Algorithm, chances are it is one. Take a look at the problem described here: http://codercareer.blogspot.com/2014/10/no-56-maximal-value-of-gifts.html . In essence, you're given a matrix (I call it a maze) and with a very strict rule to moving around, you have to find the max path from the top leftmost cell to the bottom rightmost one. The solution given in the link is a very elegant DM (Dynamic Programming) one. There is also a brute-force one which we won't get into. But I actually wanted to use this problem to exemplify the application of Dijkstra's Algorithm. It is a little overkill to use Dijkstra here, but I find this problem the perfect simple one to exemplify Dijkstra's. Dijkstra's Algorithm is a weighted BFS (breadth-first search). One needs to make use of a Priority Queue (PQ) class (down below). The overall idea of Dijkstra's is the following: Determine whether your PQ will work in ascending or ...

A Random Line From A File

The problem at work was the following: given a relatively large text file (with a little over 5,000,000 lines), write a procedure to return a random line out of this file. One naïve implementation that can be thought of would be the following: count the number of lines (O(n)), read off all the lines into an array (O(n)) and finally return a random line by randomly choosing one from the bounded array. That would cost not only O(2n), but O(n)-space which leads to yet extra processing time. A better approach would be the following: as you read the lines off of the file, have an index that indicates the current line that you're processing (line 1, 2, 3, and so on). Call this index the "lineIndex" counter. Then, here is the magical trick: select the current line as the one to be chosen with a probability 1/lineIndex . Suppose that your file only has one line. That line will surely be the one picked by this algorithm since the probability 1/1 means 100% chance. If the file has ...

The Dancing Men

Image
In 1903, in southern London, a serial killer was on the loose, with already five murder cases confirmed and attributed to the perpetrator. Police wasn’t getting any closer to stopping him, or her. The serial killer was codenamed “The Dancing Killer” for the simple fact that he (or she) used to leave funny encoded notes depicting a set of “dancing men” by their victims’ bodies. No one was able to decipher those messages. After the killer’s last attack, the following message was recovered:   A brilliant detective was actually able to crack down this last message, leading to the apprehension of the assassin. In 1903 there was no computers around. Thankfully, that’s no longer the case. We can solve this puzzle with computers now, and that’s what this post is about. We can start by assuming the symbols in the note map to English letters, which would be the simplest mapping possible. We can also assume that the “flags” in the dancing men’s hands are non-interesting artifacts...