Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Reverse factorial function
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).

tupac

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

this was done in Dev-C++ (so idk if some syntax will be different in MSVC++
 code: #include #include 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: "<
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)

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

invgamma.txt
Description:
Filename:  invgamma.txt
Filesize:  1.29 KB

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.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 13 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: