-
September 8th, 2014, 11:39 AM
#1
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.
-
September 8th, 2014, 11:42 AM
#2
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.
-
September 8th, 2014, 12:06 PM
#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?
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++23 Compiler: Microsoft VS2022 (17.6.2)
-
September 8th, 2014, 02:10 PM
#4
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
-
September 8th, 2014, 04:03 PM
#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.
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++23 Compiler: Microsoft VS2022 (17.6.2)
-
September 9th, 2014, 01:14 AM
#6
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.
-
September 9th, 2014, 06:25 AM
#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?
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.
-
September 9th, 2014, 07:23 AM
#8
Re: How to solve this factorial
Code:
void your_professor()
{
cout << "no kidding";
}
int main()
{
goto idiot;
idiot:
your_professor();
return 0;
}
-
September 9th, 2014, 10:28 AM
#9
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.
-
September 9th, 2014, 11:13 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...
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.
-
September 9th, 2014, 11:23 AM
#11
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).
-
September 10th, 2014, 09:10 AM
#12
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.
-
September 10th, 2014, 09:21 AM
#13
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).
-
September 10th, 2014, 11:59 AM
#14
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;
}
-
September 10th, 2014, 02:29 PM
#15
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|