dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: How to solve this factorial

  1. #1
    Join Date
    Sep 2014
    Posts
    2

    How to solve this factorial

    How to Solve this function F=4(3n+m)!! +6 , if "n" and "m" are given , by using "goto" command.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: How to solve this factorial

    Eh, are you supposed to write a C++ program that computes that double factorial? If so, the requirement to use goto is very strange, unless you already know how to do this without using goto and can show us a program in which you do so.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: How to solve this factorial

    The use of the goto statement is strongly discouraged from use and should only even be considered for use in quite exceptional circumstances.

    Use of the goto statement is definitely not required for computation of factorials.

    Is this a homework exercise?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  4. #4
    Join Date
    Sep 2014
    Posts
    2

    Re: How to solve this factorial

    Quote Originally Posted by 2kaud View Post
    The use of the goto statement is strongly discouraged from use and should only even be considered for use in quite exceptional circumstances.

    Use of the goto statement is definitely not required for computation of factorials.

    Is this a homework exercise?

    no i had this on the test ...my proffesor from the age of stones!

    do u know any book that explains factorials in details ..im strunglin with them

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: How to solve this factorial

    See http://en.wikipedia.org/wiki/Factorial for details

    There are two simple ways to evaluate a factorial - iteration or recursion.

    A possible implementation of these is
    Code:
    #include <iostream>
    using namespace std;
    
    unsigned int ifact(int n)
    {
    unsigned int f = 1;
    
    	for (; n > 1; f *= n--);
    
    	return f;
    }
    
    unsigned int rfact(int n)
    {
    	return (n > 1) ? n * rfact(n - 1) : 1;
    }
    
    int main()
    {
    int f;
    
    	cout << "Enter number for factorial: ";
    	cin >> f;
    	cout << "Factorial " << f << " by iteration is " << ifact(f) << endl;
    	cout << "Factorial " << f << " by recursion is " << rfact(f) << endl;
    
    	return 0;
    }
    See if you can understand how these work. Try stepping through the code using the debugger.

    So from this it is trivial to evaluate your original expression. Note that for large values factorial evaluation can quickly overflow the maximum value that can be contained in a 32 bit int.
    Last edited by 2kaud; September 8th, 2014 at 04:08 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  6. #6
    Join Date
    Jul 2013
    Posts
    576

    Re: How to solve this factorial

    Quote Originally Posted by nesstle View Post
    no i had this on the test ...my proffesor from the age of stones!
    Then please post the whole original question exactly as it was given to you. What's the name of the course and at what educational level?

    My guess is that you are holding back information or have misunderstood something. Not even a stone age professor would require you to write a program consisting of goto's alone because that's not possible.

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to solve this factorial

    Quote Originally Posted by razzle View Post
    Not even a stone age professor would require you to write a program consisting of goto's alone because that's not possible.
    What do you mean, "not possible"?

    Quote Originally Posted by razzle View Post
    Then please post the whole original question exactly as it was given to you. What's the name of the course and at what educational level?

    My guess is that you are holding back information or have misunderstood something.
    Agreed. I'm also wondering if there isn't a typo in there. If both n and m are 1, then you'll overflow your integers (4! => 24, 24! > 2^^64 ).

    Also, what exactly does it mean to "Solve" this function?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to solve this factorial

    Code:
    void your_professor()
    {
        cout << "no kidding";
    }
    
    int main()
    {
    goto idiot;
    
    idiot: 
           your_professor();
    
    return 0;
    }

  9. #9
    Join Date
    Jul 2013
    Posts
    576

    Re: How to solve this factorial

    Quote Originally Posted by monarch_dodra View Post
    What do you mean, "not possible"?
    I mean that restricting a language to the use of goto's only would render it not Turing complete. For Turing completeness an imperative language requires conditional branching. It means in addition to goto's the professor must also allow if's to be used otherwise a program in the language is not possible.
    Last edited by razzle; September 9th, 2014 at 10:44 AM.

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to solve this factorial

    Quote Originally Posted by razzle View Post
    I mean that restricting a language to the use of goto's only would render it not Turing complete. For Turing completeness an imperative language requires conditional branching. It means in addition to goto's the professor must also allow if's to be used otherwise a program in the language is not possible.
    Hum... I took it for granted you could also use ifs. Just not control "structures", eg for, while, do-while...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: How to solve this factorial

    Yeah, it isn't clear if "by using goto command" means that there has to be sensible use of a goto somewhere, or if it means that only goto can be used. However, without a label, one cannot use a goto, so presumably razzle's interpretation was not intended otherwise not even the use of goto would be possible (besides the fact that one would not be allowed to define the global main function, or declare any variables, etc).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to solve this factorial

    Quote Originally Posted by razzle View Post
    I mean that restricting a language to the use of goto's only would render it not Turing complete. For Turing completeness an imperative language requires conditional branching. It means in addition to goto's the professor must also allow if's to be used otherwise a program in the language is not possible.
    In C++, it IS entirely possible to write conditional code without ever using the the keyword if. It just needs some nifty use of what's available. however, it does require being able to evaluate both sides of the branch. but with lambda's you can circumvent even that.


    turing completeness btw, has NOTHING to do with using or not using goto. What you do need is conditional branching commongly described as the language having "if" and "goto" or at the very least, a "branch on zero". (every conditional can be delegated as a branch on zero type test).
    a program is not turing complete or not



    combining conditional-without-if with conditional-goto however... now that's a challenge I'm not sure is solvable.
    can you goto out of a lambda ? would have to try.

  13. #13
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to solve this factorial

    Below you'll find the function to calculate a factorial using goto.
    call as
    int f = fac(7); // calculate 7! which is 5040
    it's 3x as awesome because it uses 3 goto's !!! (yes 3 exclamations there).

    Give this to your professor and watch his head explode.

    Code:
    int fac(int it)			// fac (it)
    {
    int fac(it);			// fac (it) again
    	if (!it)			// if not it ?
    		goto it;		// then it anyway
    fac_it:				// fac it !
    	--it;			// we have too much it
    	fac*=it?			// fac it ?
    it:				// it !
    	1;			// one? yes, one.
    	if (!it)			// if not it ?
    		goto it;		// then it anyway
    	goto fac_it;		// fac it
    it:				// it !
    	return fac;		// fac
    }
    note it uses int, so it'll overflow when you call it to calculate more than 12!
    modify the 3 (yes 3 !!! (yes 3 exclamations)) occurences of int to __int64 to allow to calculate up to 24!

    if you need more than 24!, then you'll beed a big int library of some sorts since no integer types are available. (you could use double, but that'll cause inaccuracies).

  14. #14
    Join Date
    Oct 2008
    Posts
    1,456

    Re: How to solve this factorial

    fac it competition !!!
    possibly overflowing, fully portable, just one goto and no 'if' or '?:' :

    Code:
    int fac( int it )
    {
    	int fac(it);
    	struct fac { virtual void it(){} } again, *and_again_and[] = { &again, 0 };
    
    	try { it: typeid(**( and_again_and + !--it ));
    	    fac*=it;
    	    goto it;
    	} catch(...){}
    
    	return fac;
    }

  15. #15
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to solve this factorial

    Quote Originally Posted by superbonzo View Post
    just one goto and no 'if' or '?:' :
    that was entirely intentional for Obvious (or unobvious) reasons
    also... if they insist on goto's, then more goto's got to (<-intentional) be better too

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)