Author 
Message 
tupac

Posted: Tue Nov 14, 2006 3:05 pm Post subject: Reverse factorial function 


k, so i cant post big posts cuz the server wont let me (even though i've seen posts a million times bigger). but anyway, ill have to post this in sections:
k, so the story is, my discrete teacher said that she's never seen a calculator do the reverse function of factorial (ie. 5! = 120 calculators CAN do, 120 = 5! calculators CAN'T do). so i was bored one day and made a little program in c++ that does just that (reverse function of factorial).






Sponsor Sponsor



tupac

Posted: Tue Nov 14, 2006 3:06 pm Post subject: (No subject) 


this was done in DevC++ (so idk if some syntax will be different in MSVC++
code: 
#include <cstdlib>
#include <iostream>
using namespace std;
//declaring variables needed later on:
int inp;
int num = 1;
int temp;
int j = 1;
int i;







tupac

Posted: Tue Nov 14, 2006 3:09 pm Post subject: (No subject) 


code: 
int main(int argc, char *argv[])
{
cin >> inp;
for(i=1;i<=inp;i++){
temp = j + i;
num = num * temp;
if (num==inp){
cout << "Answer: "<<temp<<"!"<<endl;
cin.get();//or 'system("PAUSE")'
}
}







tupac

Posted: Tue Nov 14, 2006 3:12 pm Post subject: (No subject) 


it says"service temporarily unavailable".... but obviously when i type this in, it is available.... only when i want to type something i need its unavailable






Andy

Posted: Tue Nov 14, 2006 3:19 pm Post subject: (No subject) 


why are you using global variables?






md

Posted: Tue Nov 14, 2006 3:51 pm Post subject: (No subject) 


Also from the looks of it all your code does is calculate factorials until it finds the right one to give the inputted answer. That's hardly taking 120 and getting 5!, that's comparing X! to the answer and then incrementing X if it's not the same.
I'm not entirely sure mathematically reversing a factorial is even possible.






Andy

Posted: Tue Nov 14, 2006 4:24 pm Post subject: (No subject) 


well, there isnt even a shortcut to get the factorial, without multiplying it out, or use an pascals triangle.
i really doubt there is a way to do reverse factorial






bugzpodder

Posted: Tue Nov 14, 2006 7:00 pm Post subject: (No subject) 


factorials can be approximated using stirling's approximation fairly efficiently. inverse factorials are next to useless for integers (what, 12! overflows the int?, so just do a loop from 1 to 12 which someone already did), and for arbitrary reals you may want to figure out inverse gamma function on [2,inf)






Sponsor Sponsor



richcash

Posted: Wed Nov 15, 2006 12:04 am Post subject: (No subject) 


Yeah, I don't think it's possible to get the reverse of the factorial function. Here is an impoved version of Sterling's factorial approximation :
n! = (n / e) ^ n * sqrt (2 * PI * n) * (1 + 1 / (12 * n)), where obviously e = 2.71828
In order to go the other way you have to solve that equation for n. I don't think that's possible, even if you exclude the bold part.






Catalyst

Posted: Thu Nov 16, 2006 4:03 pm Post subject: (No subject) 


note: liberal use of 'numerical recipes' in the code below
Description: 

Download 
Filename: 
invgamma.txt 
Filesize: 
1.29 KB 
Downloaded: 
727 Time(s) 






md

Posted: Thu Nov 16, 2006 7:07 pm Post subject: (No subject) 


Catalyst wrote: note: liberal use of 'numerical recipes' in the code below
If I only knew the math behind that... so many magic numbers...






ramubaba

Posted: Mon Nov 25, 2013 9:47 am Post subject: RE:Reverse factorial function 


#include<stdio.h>
#define MAX 1024 //the maximum number used as a limit
int revFact(unsigned long int fact_no) //function to calculate reverse of a number and returns 0 if fails
{
int i=2;
for(;i<MAX;i++)
{
if(fact_no % i) //if not a perfect divisor
return 0; //just exit from here
else
{
fact_no /= i; //shorten the number itself
if(fact_no==1) return i; //if the number is finally reduced to 1
}
}
}
main()
{
unsigned long int fact_no;
int revF;
printf("****This program calculates reverse factorial of a number.****\n\n\n");
//Prompt for INPUT
printf("Enter a number\n");
scanf("%ld",&fact_no); //stoing input in LONG INTEGER DATATYPE. ask yourself ,why?
//if 1 is given as input to check
if(fact_no==1){ printf("\nfactorial of 1 is 1 itself\n"); exit(0);}
if(!(revF=revFact(fact_no))){ //if the revFact function returns 0, i.e. NOTHING
printf("\n%ld is Not a perfect Fcatorial\n",fact_no);
exit(1);}
//if the control could reach here then the result would be here
printf("\n%ld is the factorial of %d\n\n", fact_no, revF);
return 0;
} //exiting main function






Insectoid

Posted: Mon Nov 25, 2013 2:50 pm Post subject: RE:Reverse factorial function 


That's very nice, except that A) you are not permitted to post complete solutions, B) this thread is seven years old.






