GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Jun-2004, 18:02
cior cior is offline
New Member
 
Join Date: Jun 2004
Posts: 3
cior is on a distinguished road

Factorial of numbers


I've written a program to find all numbers where the factorial of the individual digits is the same as the number itself.
Example.

145 - 1!+4!+5! = 1+24+120 = 145

The program looks like this but it only seems to find numbers 1,2 & 145 but I'm sure there are more.

CPP / C++ / C Code:
#include <iostream>
#include <conio>
#include <math>
#include <stdlib>

int check(int number);
int main()
{
int i=1,summ=0;

while(i<9999999)
{

summ=check(i);
if(summ==i)
	cout << " " << i << " ";
i++;
}
cout << "DONE";

getch();
}


int check(int number)
{
int i=1,result=1,summ=0,digit1=0,digit2=0,digit3=0,digit4=0,digit5=0,digit6=0,digit7=0,digit8=0;
digit1=number/10000000;
digit2=number/1000000-(10*digit1);
digit3=number/100000-(100*digit1)-(10*digit2);
digit4=number/10000-(1000*digit1)-(100*digit2)-(10*digit3);
digit5=number/1000-(10000*digit1)-(1000*digit2)-(100*digit3)-(10*digit4);
digit6=number/100-(100000*digit1)-(10000*digit2)-(1000*digit3)-(100*digit4)-(10*digit5);
digit7=number/10-(1000000*digit1)-(100000*digit2)-(10000*digit3)-(1000*digit4)-(100*digit5)-(10*digit6);
digit8=number/1-(10000000*digit1)-(1000000*digit2)-(100000*digit3)-(10000*digit4)-(1000*digit5)-(100*digit6)-(10*digit7);

if(digit1!=0)
{
for (result=1, i=1; digit1>=i; i++)
	result=result*=i;
summ+=result;
}

if(digit2!=0)
{
for (result=1, i=1; digit2>=i; i++)
	result=result*=i;
summ+=result;
}

if(digit3!=0)
{
for (result=1, i=1; digit3>=i; i++)
	result=result*=i;
summ+=result;
}

if(digit4!=0)
{
for (result=1, i=1; digit4>=i; i++)
	result=result*=i;
summ+=result;
}

if(digit5!=0)
{
for (result=1, i=1; digit5>=i; i++)
	result=result*=i;
summ+=result;
}

if(digit6!=0)
{
for (result=1, i=1; digit6>=i; i++)
	result=result*=i;
summ+=result;
}

if(digit7!=0)
{
for (result=1, i=1; digit7>=i; i++)
	result=result*=i;
summ+=result;
}

if(digit8!=0)
{
for (result=1, i=1; digit8>=i; i++)
	result=result*=i;
summ+=result;
}

return summ;
}

I'm not really good at coding so I'm sure it's "ugly" but does anyone see anything wrong with the code.
Last edited by dsmith : 09-Jun-2004 at 05:10. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 08-Jun-2004, 21:04
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by cior
I've written a program to find all numbers where the factorial of the individual digits is the same as the number itself.
Example.

145 - 1!+4!+5! = 1+24+120 = 145

The program looks like this but it only seems to find numbers 1,2 & 145 but I'm sure there are more.
Why are you sure there are more? I'll write a version and see what I come up with.
__________________

Age is unimportant -- except in cheese
  #3  
Old 08-Jun-2004, 21:20
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by WaltP
Why are you sure there are more? I'll write a version and see what I come up with.

Hi,
I just found out that they are unique factorials and here is all of those that exists:

1 = 1!
2 = 2!
145 = 1! + 4! + 5!
40585 = 4! + 0! + 5! + 8! + 5!

there is no other integers like it (I think, I'm not a math guy) according to this:
unique Factorials
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #4  
Old 08-Jun-2004, 21:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Max Payne
Hi,
I just found out that they are unique factorials and here is all of those that exists:

1 = 1!
2 = 2!
145 = 1! + 4! + 5!
40585 = 4! + 0! + 5! + 8! + 5!

there is no other integers like it (I think, I'm not a math guy) according to this:
unique Factorials
I just tested it for numbers up to 2000 and there were only the 3 mentioned.

I also just confirmed 40585 too.

I just tested thru 9999999 and those are the only 4.
__________________

Age is unimportant -- except in cheese
  #5  
Old 08-Jun-2004, 21:50
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Well that means you don't need a program then...

anyway, cior, your program works well and it displays all the 4 numbers although it took some time to get the last number (40585). Maybe we should focus on efficiency to get the numbers real fast.

still, good work cior for able to code it..
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #6  
Old 09-Jun-2004, 08:15
cior cior is offline
New Member
 
Join Date: Jun 2004
Posts: 3
cior is on a distinguished road
Thanks, I found out why the code doesn't work for me, 0! is actually 1 not 0. But the problem is that the I had to skip the inital zeroes and because of that I accidentally also skipped the zeroes within the number.
  #7  
Old 09-Jun-2004, 08:36
cior cior is offline
New Member
 
Join Date: Jun 2004
Posts: 3
cior is on a distinguished road
Ok, I managed to rewrite the code to check if the next digit is higher than zero, if that's true 0! will be counted as 1 otherwise as 0.

CPP / C++ / C Code:
#include <iostream>
#include <conio>
#include <math>
#include <stdlib>

int check(int number);
int main()
{
int i=1,summ=0;

while(i<9999999)
{

summ=check(i);
if(summ==i)
	cout << " " << i << " ";
i++;
}
cout << "DONE";

getch();

}


int check(int number)
{
int i=1,result=1,summ=0,digit1=0,digit2=0,digit3=0,digit4=0,digit5=0,digit6=0,digit7=0,digit8=0;
digit1=number/10000000;
digit2=number/1000000-(10*digit1);
digit3=number/100000-(100*digit1)-(10*digit2);
digit4=number/10000-(1000*digit1)-(100*digit2)-(10*digit3);
digit5=number/1000-(10000*digit1)-(1000*digit2)-(100*digit3)-(10*digit4);
digit6=number/100-(100000*digit1)-(10000*digit2)-(1000*digit3)-(100*digit4)-(10*digit5);
digit7=number/10-(1000000*digit1)-(100000*digit2)-(10000*digit3)-(1000*digit4)-(100*digit5)-(10*digit6);
digit8=number/1-(10000000*digit1)-(1000000*digit2)-(100000*digit3)-(10000*digit4)-(1000*digit5)-(100*digit6)-(10*digit7);

//1
if(digit1==0)
{

}
else
{
for (result=1, i=1; digit1>=i; i++)
	result=result*=i;
summ+=result;
}
//2
if(digit2==0 && digit1!=0)
{
for (result=1, i=1; digit2>=i; i++)
	result=result*=i;
summ+=result;
}
else if(digit2==0)
{

}
else
{
for (result=1, i=1; digit2>=i; i++)
	result=result*=i;
summ+=result;
}
//3
if(digit3==0 && digit2!=0)
{
for (result=1, i=1; digit3>=i; i++)
	result=result*=i;
summ+=result;
}
else if(digit3==0)
{

}
else
{
for (result=1, i=1; digit3>=i; i++)
	result=result*=i;
summ+=result;
}
//4
if(digit4==0 && digit3!=0)
{
for (result=1, i=1; digit4>=i; i++)
	result=result*=i;
summ+=result;
}
else if(digit4==0)
{

}
else
{
for (result=1, i=1; digit4>=i; i++)
	result=result*=i;
summ+=result;
}
//5
if(digit5==0 && digit4!=0)
{
for (result=1, i=1; digit5>=i; i++)
	result=result*=i;
summ+=result;
}
else if(digit5==0)
{

}
else
{
for (result=1, i=1; digit5>=i; i++)
	result=result*=i;
summ+=result;
}
//6
if(digit6==0 && digit5!=0)
{
for (result=1, i=1; digit2>=i; i++)
	result=result*=i;
summ+=result;
}
else if(digit6==0)
{

}
else
{
for (result=1, i=1; digit6>=i; i++)
	result=result*=i;
summ+=result;
}
//7
if(digit7==0 && digit6!=0)
{
for (result=1, i=1; digit7>=i; i++)
	result=result*=i;
summ+=result;
}
else if(digit7==0)
{

}
else
{
for (result=1, i=1; digit7>=i; i++)
	result=result*=i;
summ+=result;
}
//8
if(digit8==0 && digit7!=0)
{
for (result=1, i=1; digit8>=i; i++)
	result=result*=i;
summ+=result;
}
else if(digit8==0)
{

}
else
{
for (result=1, i=1; digit8>=i; i++)
	result=result*=i;
summ+=result;
}


return summ;
}
Last edited by dsmith : 09-Jun-2004 at 16:18. Reason: Please use [c] & [/c] for syntax highlighting
  #8  
Old 09-Jun-2004, 20:08
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Now that you got it working, here's my solution:
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main()
{
    int num;    // number being tested
    int nnum;   // temporary version of num
    int val;    // the total value of the factorials
    int fact;   // a factorial
    int n;      // loop counter to compute the factorial
    int nn;     //  don't remember....

    for (num = 1; num < 9999999; num++)
    {
        nnum = num;             // Save the value in the temp
        val  = 0;               // Initialize the total
        while (nnum > 0)        // While theres still some number left...
        {
            n = nnum % 10;      // Get the ones digit
            nn = n;
            nnum /= 10;         // Remove that digit
            fact = 1;           // Factorials start at 1  (0! = 1)
            while (n > 0)       // Loop until the digit is gone
            {
                fact = fact * n;    // Compute the factorial
                n--;                // next digit
            }
            val += fact;        // Add this factorial to the total
        }
        if (num == val)         // If the number and the total are the same...
                printf("%d \n", num);
    }
    
    return 0;
}
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
Random numbers KrissKross C Programming Language 5 13-May-2004 19:31
Problem that doesn't make sense! Please help! Shufty C++ Forum 23 09-May-2004 15:49
c: array comparison jack C Programming Language 7 26-Jan-2004 11:21
Best way to validate numbers (ids)? JdS MySQL / PHP Forum 1 20-Jan-2003 03:55

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.