Question:
Any code in C? Need a little help with a factorian check.?
N e v i u m
2014-02-19 12:56:06 UTC
Heres some code. I've already gotten the code working to check for sum product numbers, dudeney numbers and armstrong numbers but I'm completely lost when it comes to factorians.
Like 145 = 1! + 4! + 5!


#include
#include
void armCheck(int a);
void dudeneyCheck(int a);
void sumproductCheck(int a);
void factCheck(int a);
int fact(int x);
int main(int argc, char *argv[])
{

int i;
for (i=0;i<100000;i++)
factCheck(i);
system("PAUSE");
return 0;
}

int fact(int x)
{
int prod =1;

if (x==0)
return 0;

while (x>0)
{
prod=prod*x;
x--;
}
return prod;
}





void factCheck(int a)
{
int digits[10], idx=0, i, sum=0;
int copy=a;
while (a>0)
{
digits[idx]=a%10;
idx++;
a=a/10;
}

for(i=1;i

if (copy==sum)
printf("%i is a factorian\n",copy);
}



I'll add the sum product one just to show how I was doing those.

void sumproductCheck(int a)
{
int digits[10], idx=0, i, sum=0, product=1;
int copy=a;
while (a>0)
{
digits[idx]=a%10;
idx++;
a=a/10;
}

for(i=0;i {
sum = sum+digits[i];
product = product*digits[i];
}

sum = sum*product;

if (copy==sum)
printf("%i is a sum product number\n",copy);



}


I've tried looking it up but there literally isn't anything. It's the factCheck function that I just don't what to put there.
Thanks in advance for help ^ ^
Three answers:
Jonathan
2014-02-19 16:06:39 UTC
Just to make husoski's comments clear through the use of a concrete and complete example:



int factCheck( unsigned int a ) {

    static const unsigned int f[]= { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };

    unsigned int sum, save_a= a;

    for ( sum= 0; a != 0; a /= 10 )

        sum += f[a % 10];

    return sum == save_a;

}



The above code returns 0 if it's not a factorian, 1 if it is. It's not the best of practices to combine two different purposes (testing AND printing, for example) into a single function. If you want to combine them, then write another function, as in:



void factPrint( unsigned int a ) {

    if ( factCheck( a ) ) printf( "%u is a factorian\n", a );

}



That way, you can have your cake and eat it, too. But separate the functions, because it's almost certain you will want to be able to test a value without being forced to print it. If not today, then tomorrow. It's pretty much a given for re-usable code design.
husoski
2014-02-19 22:14:06 UTC
I'd use a table. Define the array:



static const int factab={ 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };



...and then the "computation" of i! is simply factab[i].



You can extend that through 12! to handle bases up to 13 with 32-bit integers, but anything larger will require double (up to 17!) or long long (up to 20!) on most implementations. (Assuming 64-bit long long and 64-bit IEEE doubles.)



Of course, you could write the obvious function:



long factorial( int n )

{

long result=1;

for (i=2; i<=n; result *= i++) {}

return result;

}



...and call factorial(i) instead of factab[i], but the table will save a lot of multiplications and function calls.
?
2014-02-19 22:01:26 UTC
#include

//here is a better function for factorial

int fact(int n){

if (n<1) return 1;

return n*fact(n-1);

}





//here is a fact check

int FactCheck(int n){

int total=0;

while(n){

total+=fact(n%10);

n/=10;



}

return total;

}



int main(void){

int j;



for(j=1;j<200;j++)//printf("%d %d\n",FactCheck(j),j);

if(FactCheck(j)==j)

printf(" %d %d\n",fact(j),j);

return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...