Ponder This - March 2000

IBM has a neat monthly programming challenge called Ponder This. Usually they are hard, but from time to time there are easy challenges such as the one from March 2000: http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/Challenges/March2000.html, which I copy/paste here for reference:

"Let's borrow notation from LaTeX and let "^" denote exponentiation, so that x^2 means "x squared".

Consider the numbers 1233 = 12^2 + 33^2, and 990100 = 990^2 + 100^2.

Can you find an eight-digit number N with the same property, namely that if you break N into two four-digit numbers B and C, and add their squares, you recover N?
"

If you look at the solution on the site, you'll see that there are clever ways to solve it mathematically. But I like to explore the brute-force of computing. Writing this solution is actually not that hard, a nested-loop dealing with some simple calculation:

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

namespace PonderThisMarch2000
{
    class Program
    {
        static void Main(string[] args)
        {
            bool found = false;
            Random rd = new Random();
            for (int n = 10000000; n < 100000000 && !found; n++)
            {
                if (rd.Next(0, n) < 10)
                {
                    Console.WriteLine("Trying N={0}", n);
                }
                for (int p = 1; n / p > 0; p *= 10)
                {
                    int number1 = n % p;
                    int number2 = n / p;
                    if (number1 * number1 + number2 * number2 == n)
                    {
                        Console.WriteLine("Found: {0} = {1}^2 + {2}^2", n, number1, number2);
                        found = true;
                        break;
                    }
                }
            }
        }
    }
}

In few seconds, the solution pops up:

Comments

Popular posts from this blog

Golang vs. C#: performance characteristics (simple case study)

Claude vs ChatGPT: A Coder's Perspective on LLM Performance

ProjectEuler Problem 719 (some hints, but no spoilers)