CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: How to solve this factorial

1. Junior Member
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. Elite Member Power Poster
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.

3. ## 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?

4. Junior Member
Join Date
Sep 2014
Posts
2

## Re: How to solve this factorial

Originally Posted by 2kaud
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. ## 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.

6. Member +
Join Date
Jul 2013
Posts
576

## Re: How to solve this factorial

Originally Posted by nesstle
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. ## Re: How to solve this factorial

Originally Posted by razzle
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"?

Originally Posted by razzle
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?

8. Elite Member Power Poster
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. Member +
Join Date
Jul 2013
Posts
576

## Re: How to solve this factorial

Originally Posted by monarch_dodra
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. ## Re: How to solve this factorial

Originally Posted by razzle
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...

11. Elite Member Power Poster
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).

12. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: How to solve this factorial

Originally Posted by razzle
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. Elite Member Power Poster
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. Senior Member
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. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: How to solve this factorial

Originally Posted by superbonzo
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

#### Posting Permissions

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

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)