A number that equals twice the sum of its decimal digits

| | Comments (0)
Riffing on "dix-huit ans aujoud'hui = 18 pounds!" from Frankarr, an australian blogger.

In the entry he mentions that "One fact about 18 which I really love is that 18, aside from 0, is the only number that equals twice the sum of its decimal digits - cool huh?". Now, being a geek I had to write a scratch program t prove that one way or the other, without further ado here it is:

            for (int i = 0; i < int.MaxValue -1; i++)
            {
                string intValue = i.ToString();
                int sumValue = 0;
                foreach (char digit in intValue.ToCharArray())
                {
                    sumValue += ((Convert.ToInt32(digit.ToString()))* 2);
                }
                if (sumValue == i)
                {
                    Console.WriteLine("Value found: {0}", i);
                }
                int modulus = i % 100000;
                if (modulus == 0)
                {
                    Console.WriteLine(string.Format("{0}", i));
                }
            }

The second Console.WriteLine is just so that when running, I can see that the program hasn't frozen/crashed. And it's true, in all the numbers between 0 and int.MaxValue -1, there are no numbers other than 0 and 18 where his fact holds true. Geeky goodnesss!

Leave a comment

About this Entry

This page contains a single entry by Rob published on May 21, 2009 8:30 AM.

Miscellanea 15 was the previous entry in this blog.

Sorting the results of DirectoryInfo.GetFiles is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 5.02