Tuesday, March 24, 2009

Hey, now the Smith Numbers...

OK, So I am back, probably after sitting idle(just on the blogging front) for more than 6 months. I know that there is not even 1 chance in a million that someone would be waiting out there, he he he he. But that doesn't matter at all, I write just for personal satisfaction, as is the case with most of the bolggers, I guess... So, this time again I am going to write on another interesting series of numbers called the Smith Numbers. I just participated in a coding event and sadly this was the only coding question, I could manage through :(. Anyway, the numbers quite filled me with wonders and coding to generate them has been a joy for the evening. Smith numbers are those whose sum of digits is exactly same as the sum of the digits of their prime factors any prime factor appears more tan once, it counted as many number of times. Having said that, can you guess the smallest Smith number??? (Now don't say 1, as it is neither prime nor composite).

Well, its 4 = 2 x 2, the second smallest is 22 and so the list does on to an infinite limit (this was actually proved by a mathematician in 1987)and many maths freaks have found out interesting concepts such as Smith twins , i.e. consecutive Smith numbers (like 728, 729), Smith triplets and so on going up to the recently discovered Smith septets (A string of 7 consecutive smith numbers all of which are 12 digits long )

One interesting observation can be the fact that the beast number 666, is also a Smith Number.
OK so lets keep it brief and I should wrap up (Too much academic load at this time of the year). Below I have presented my code for testing whether a number is a Smith number or not (OK, I will admit that I am a lousy coder and no genius with all my hardly commented code, but that's it...). If u feel like going through more stuff on these numbers, make sure that you visit these links...

http://en.wikipedia.org/wiki/Smith_number

http://www.shyamsundergupta.com/smith.htm

And now the code :P

#include
#include
#include
#include

using namespace std;
typedef unsigned long int lint;
struct nod{
lint fact;
struct nod *n_ptr;
};
typedef struct nod Node;

Node Head;
Node *HEAD = &Head;

void prime_fact(lint);
void print_fact(lint);
void test_smith(lint);

int main(int argc, char *argv[])
{
lint num;
printf("Enter the number: \t");
cin >> num;
Head.fact = 0;
Head.n_ptr = NULL;
prime_fact(num);
print_fact(num);
test_smith(num);
free(HEAD);
cout << "\nMemory List Cleared!!!\n";
system("PAUSE");
return EXIT_SUCCESS;
}

void prime_fact(lint num)
{
lint i;
Node *head = &Head;
for(i=2;i {
if(num % i == 0)
{
num = num / i;
head->n_ptr = (Node *)malloc(sizeof(Node));
Head.fact = Head.fact + 1;
head = head->n_ptr;
head->fact = i;
head->n_ptr = NULL;
i = 2;
}
}
head->n_ptr = (Node *)malloc(sizeof(Node));
Head.fact = Head.fact + 1;
head = head->n_ptr;
head->fact = num;
head->n_ptr = NULL;
}

void print_fact(lint num)
{
int i;
Node *temp = &Head;
temp = temp->n_ptr;
cout << num << " = ";
for(i=0;i<(Head.fact);i++)
{
cout << temp->fact << " * ";
if(i==(Head.fact-1))
cout << "\b\b.";
temp = temp->n_ptr;
}
}

void test_smith(lint num)
{
int sum1; int sum2;
lint i = 0;
Node *tem = Head.n_ptr;
sum1 = sum2 =0;
do{
sum1 = sum1 + (num % 10);
num = num / 10;
}while(num>0);
//cout << "\n" << sum1 << "\t";
for(i=0;i {
do{
sum2 = sum2 + (tem->fact % 10);
tem->fact = tem->fact / 10;
}while((tem->fact)>0);
tem = tem->n_ptr;
}
//cout << sum2 << "\n";
if(sum1 == sum2)
cout << "\nCongo!!!We just discovered a Smith number.\n" ;
else
cout << "Not a Smith number dude!!!\n";
}