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

The trilogy comes to an end! The solution to the problem has been posted on the IBM ponder this page. If you use the hint from the previous post, you'll notice that all you have to do is keep getting the last ~12 digits of the results of the exponentiation (basically one of two more than 10), and keep using those digits to raise the inner numbers, repeating the process all the way from 9 to 2. The ModPow function is your friend here. Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace IBMPonderThisOctober2014
{
    class Program
    {
        static void Main(string[] args)
        {
            BigInteger result = 1;

            for (int i = 9; i >= 2; i--)
            {
                result = BigInteger.ModPow(i, result, 1000000000000);
                Console.WriteLine("{0} => ...{1}", i, result);
            }
            Console.WriteLine("\nSolution: {0}", result % 10000000000);
        }
    }
}


When you run it:

9 => ...9
8 => ...134217728
7 => ...816763596801
6 => ...754204000256
5 => ...918212890625
4 => ...918212890624
3 => ...84919828481
2 => ...88170340352
Solution: 8170340352

Comments

Popular posts from this blog

Changing the root of a binary tree

Prompt Engineering and LeetCode

ProjectEuler Problem 719 (some hints, but no spoilers)