A number that equals twice the sum of its decimal digits

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!

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment