Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,055 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,623 people online right now. Registration is fast and FREE... Join Now!




help me: factorial with c++!

 
Reply to this topicStart new topic

help me: factorial with c++!, i've been ask to build a program that can perform factorial for a

nhmj88
4 Aug, 2008 - 08:14 AM
Post #1

New D.I.C Head
*

Joined: 4 Aug, 2008
Posts: 1

CODE

/*i've given 2 weeks to solve this problem..i've only have 1 week left!
the problems is, i only can get result till factorial 16..
i can get the result for more than that..my lecturer wanted us to solve this problems
so we can calculate any large number entered and get the result */

#include <iostream>
#include <conio.h>

using namespace std;

int faktorial (int);

int main()
{
    cout << faktorial(17);  
    getch();
    return 0;
}
  
int faktorial (int n)
{
   if (n == 0)
      return 1;
   else
       return n * faktorial (n-1);
}


This post has been edited by nhmj88: 4 Aug, 2008 - 08:16 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Help Me: Factorial With C++!
4 Aug, 2008 - 08:44 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
Here is some pseudo code for you (actually written in python)

python

# Factorial function (n!)
def factorial(totalElements):
n = totalElements
f = 1
#0! and 1! = 1
for i in range(2, n+1):
f *= i
return f


edited for typo

This post has been edited by KYA: 4 Aug, 2008 - 08:45 AM
User is online!Profile CardPM
+Quote Post

KYA
RE: Help Me: Factorial With C++!
4 Aug, 2008 - 08:51 AM
Post #3

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
Better yet, I wrote this up in C++:

cpp

#include <iostream>
using namespace std;

// Factorial function (n!)
int factorial(int totalElements)
{
int n = totalElements;
int f = 1;
// 0! and 1! = 1
for (int i = 2; i < n+1; i++)
{
f *= i;
}
return f;
}

int main()
{
int total = factorial(3);
cout << "Total: " << total << endl;
return 0;
}


Enjoy
User is online!Profile CardPM
+Quote Post

red_4900
RE: Help Me: Factorial With C++!
4 Aug, 2008 - 08:57 AM
Post #4

Code Dreamers
****

Joined: 22 Feb, 2008
Posts: 813



Thanked: 11 times
My Contributions
@KYA,

edit : if you want to write pseudo code, write it in C or C++. even if it's just a pseudo code. not everyone here can read python code. (damn, that was fast)

but from what you do, I guess you're thinking of doing this?

cpp
cout<<"insert n : ";
cin>>n;

sum = 1;
for(i=1;i<=n;i++)
sum = sum*i;

cout<<endl<<"factorial for "<<n<<" is "<<sum<<endl;


but I could only get the factorial up till 12. sad.gif

and nhmj88, your code too can only calculate till factorial of 12. as from from you said, you can calculate up till factorial of 16? but when I checked it with my calculator, it can only gives the correct value until factorial of 12.

edit : KYA, your code too can calculate up until factorial of 12 wink2.gif

This post has been edited by red_4900: 4 Aug, 2008 - 09:02 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Help Me: Factorial With C++!
4 Aug, 2008 - 09:02 AM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
Thats because the variable will roll over. You'll have to make it an unsigned integer.

edit: Out of all the languages here, python is one of the easiest to read .

This post has been edited by KYA: 4 Aug, 2008 - 09:02 AM
User is online!Profile CardPM
+Quote Post

red_4900
RE: Help Me: Factorial With C++!
4 Aug, 2008 - 09:07 AM
Post #6

Code Dreamers
****

Joined: 22 Feb, 2008
Posts: 813



Thanked: 11 times
My Contributions
I was thinking of the same thing too. but still, I can't get the correct value for factorial larger than 12.

this is what I've done :

cpp
#include<iostream>
using namespace std;

int main(void){
int i,n;
unsigned int sum;
cout<<"insert n : ";
cin>>n;

sum = 1;
for(i=1;i<=n;i++)
sum = sum*i;

cout<<endl<<"factorial for "<<n<<" is "<<sum<<endl;
return 0;
}


ps : I can't read python lol biggrin.gif
User is offlineProfile CardPM
+Quote Post

KYA
RE: Help Me: Factorial With C++!
4 Aug, 2008 - 09:30 AM
Post #7

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
We could declare a DWORD or higher or have a separate function that divides the number to be factorial ed into sets lower then 12
User is online!Profile CardPM
+Quote Post

red_4900
RE: Help Me: Factorial With C++!
4 Aug, 2008 - 09:45 AM
Post #8

Code Dreamers
****

Joined: 22 Feb, 2008
Posts: 813



Thanked: 11 times
My Contributions
let your code speaks biggrin.gif

(actually I have no idea what you're talking about tongue.gif)
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Help Me: Factorial With C++!
4 Aug, 2008 - 11:01 AM
Post #9

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
There is nothing really wrong with the initial code...

so whats the problem, well lets see

pow(2, 32) = 4294967296; //32 bit unsigned integer's maximum value + 1
pow(2, 64) = 18446744073709551616; //64 bit unsigned integer's max + 1

13! = 6,227,020,800 > 4,294,967,296 -- need something larger than 32 bits.
21! = 51,090,942,171,709,440,000 > 18,446,744,073,709,551,616 -- need more than 64 bits.

In general if we use Mathematica to evaluate the following:
CODE
In[1]:= Fbits[n_]:=N[Floor[Log[2, n!]] + 1]

In[2]:= Range[0, 32] //Fbits

Out[3]= {
1., 1., 2., 3., 5.,
7., 10., 13., 16.,
19., 22., 26., 29.,
33., 37., 41., 45.,
49., 53., 57., 62.,
66., 70., 75., 80.,
84., 89., 94., 98.,
103., 108., 113., 118.}
(Sloans A072831)This tells us how many bits n! will require, n=13 requires 34 bits, and n = 32 requires 118 bits (just in case you want to know n=1024 requires 8770 bits or just over 1Kb).

i.e. The size of the factorial grows quite fast. You can use the double or long double to hold values past n = 12 (or 20), this actually works quite well since the number of trailing zeros increases so initially the error is actually kept quite small -- but of course the the divergence happens pretty fast as well and before too long the error compounds and the value diverges.

So what to do? Well you have to use an arbitrary precision library. or BigInteger library. There are many available on the web.

This post has been edited by NickDMax: 4 Aug, 2008 - 11:22 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month