Categories

# 1729 – Saturday Night Programming before the bars: Natural Numbers…

Ok, so this afternoon I watched the movie “Proof” – really good movie. In the movie, they talk about the number 1729, about how it is the smallest number expressible as the sum of two cubes in two different ways. It also is a natural number – when its digits are added together, produces a sum which, when multiplied by its reversed self, yields the original number.

Just for the helluva it, I decided to write a little program in C# to do this. It would be nice if there was an easier way to reverse strings in .NET, maybe there is and I just don’t know. Anyways, I love how movies can get you into things you never thought you would get into.. now I only wonder what I will code up when I get back from the bars..

```using System;
using System.Collections.Generic;
using System.Text;

namespace NaturalNumbers
{
class Program
{
static void Main(string[] args)
{
// when its digits are added together, produces a sum which,
// when multiplied by its reversed self, yields the original number:

for (System.UInt64 i = 1; i < 9223372036854775808; i++)
{
string nums = i.ToString();

System.UInt64 sum = 0;
System.UInt64 product = 0;

// get the sum of each digit of the number
for (int b = 0; b < nums.Length; b++)
{
sum += System.Convert.ToUInt64(nums[b].ToString());
}

string nums2 = sum.ToString();

// reverse the sum
char[] temp = nums2.ToCharArray();
Array.Reverse(temp);
nums2 = new string(temp);

// multiply the sum times the reversed sum
product = sum * System.Convert.ToUInt64(nums2);

// if they equal we hit the jackpot
if (product == i)
{
Console.WriteLine(i.ToString() + " is a natural number");
}

if (i % 10000000 == 0)
{
Console.WriteLine(i.ToString());
}
}
}
}
}```
` `

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Technorati tags: , , , , ## By Steve Novoselac

Director of Digital Technology @TrekBikes, Father, Musician, Cyclist, Homebrewer

This site uses Akismet to reduce spam. Learn how your comment data is processed.