Programming And Mathematics or Programming Mathematics
Well, I’m trying to ease my way back into programming after my break over the summer. I decided to try something easy, but I didn’t think that it would be as easy as it was. I wanted to try to program an application that could calculate Narcissistic Numbers. Now, how in the world can a number be classified as “Narcissistic?”
A simple google “define:narcissistic” brings this up:
“characteristic of those having an inflated idea of their own importance”
So, How can this apply to numbers? Well, it is a very simple idea. You have a number, let’s say 153. How can this be narcissistic? Let me explain. First you have to split the number up digit by digit, so you end up with 1, 5, and 3. Then there are three (3) digits, so we keep that in mind. Then to test for narcissisticity (i don’t actually know if that’s a word), you take 1^3 + 5^3 + 3^3 (the “^” means “to the power of”) which equals 153, which makes this number (153) narcissistic, so it loops back on itself. The three is that you raise all of the digits to is the length of the original number, so lets test another number now that we get the concept. Lets try 243. 2^3 + 4^3 + 3^3 = (8) + (64) + (27) = 99. Since 243 does not equal 99, 243 is not a narcissistic number. Does that make sense?
Well, back to the programming, I wanted to come up with something that could calculate some of these out. I came up with this: http://tech-works.info/narcissistic_numbers.exe. It calculates all of the narcissistic numbers up to 100000000. Due to the limitations of Visual Studio, I can not go any higher, and as it is, it takes a little while to do all of the calculations, but with some optimization, it could get better, but this was a ten minute program. Here is the main function of the entire program in its entirety:
Private Sub Main
ListBox1.Items.Clear()
Dim number As Integer = 0
Dim number1 As String
Dim sum As Integer = 0
Dim length As Integer
While number < 100000000
number1 = number.ToString
length = number1.Length
Dim i As Integer = 0
While i < length
sum = sum + Val(number1.Substring(i, 1)) ^ length
i = i + 1
End While
If number = sum Then
ListBox1.Items.Add(number)
End If
sum = 0
length = 0
number = number + 1
End While
End Sub
(Note: You need Microsoft’s .NET Framework 2.0 to run the program download)
