473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Factorial of 32767

Hi there,
I got the following code.

int f = 1;
int num =32767;

while (num>0){
f = f * num;
num --;
}

Console.WriteLine("32767! = {0}", f);

The result value was not satisfied.
How can I handle factorial which can accept any number?
If it possible?

Many thanks
Nov 16 '05 #1
5 4094
You will need to use or write a class that can handle extremely large
numbers. You're using an int which has a range of roughly -2 billion to +2
billion. Well below 32767 factorial. If you search around with Google, I'm
sure you'll be able to find some C++ if not some C# classes that can handle
numbers of "infinite" length. I say that in quotes because those classes
will still be limited by memory. Also, as the numbers become larger, the
operations will eventually get very slow. My guess is that 32767! will take
quite some time to calculate since the final result is going to probably be
in the vicinity of 100,000 digits long.

Pete

"kids_pro" <ki******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP10.phx.gbl...
Hi there,
I got the following code.

int f = 1;
int num =32767;

while (num>0){
f = f * num;
num --;
}

Console.WriteLine("32767! = {0}", f);

The result value was not satisfied.
How can I handle factorial which can accept any number?
If it possible?

Many thanks

Nov 16 '05 #2
I would suggest that you use either a pre-calculated values or a library
that supports that.
It's not a good idea to calculate that at runtime.
Try to search on the web - there are plenty of articles on this.

Cheers,
Branimir

--
Branimir Giurov
MCSD.NET, MCDBA

"kids_pro" <ki******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP10.phx.gbl...
Hi there,
I got the following code.

int f = 1;
int num =32767;

while (num>0){
f = f * num;
num --;
}

Console.WriteLine("32767! = {0}", f);

The result value was not satisfied.
How can I handle factorial which can accept any number?
If it possible?

Many thanks

Nov 16 '05 #3
You won't go very far with int or even long. You will very quickly overflow.
The decimal type won't bring you much further (28 digits max for the
result). And the double type will only give you a very approximate result,
and will stop when the result has a bit more than 300 digits.

You can write your own "infinite precision integer" library, but you can
also use the J# library for this.
Add a reference to vjslib.dll to your project (you need to install the J#
redistributable if you don't have it)
Then, you will be able to use the java.math.BigInteger class, which does
exactly what you want.

Documentation is on:
http://java.sun.com/j2se/1.4.2/docs/...igInteger.html

Be careful: take a pencil first and try to evaluate the size of your result.
BigInteger can theoretically handle numbers of any size but you will be
limited by the amount of memory (the binary representation of your number
has to fit in memory).

n! ~= n**n * e**-n * sqrt(2*pi*n)

So, 32768! will fit withouth any problem but it will take a long time to
compute. On my fast machine, 1000! already takes more than 1 minute (and it
already has 2568 digits!). So be ready for a few hours of computation and
several pages of digits (more than 100,000) in the end.

Bruno.

"kids_pro" <ki******@yahoo.com> a écrit dans le message de
news:eQ**************@TK2MSFTNGP10.phx.gbl...
Hi there,
I got the following code.

int f = 1;
int num =32767;

while (num>0){
f = f * num;
num --;
}

Console.WriteLine("32767! = {0}", f);

The result value was not satisfied.
How can I handle factorial which can accept any number?
If it possible?

Many thanks

Nov 16 '05 #4
Thank this is very useful input and from that I found an article on
BigInteger:
http://www.codeproject.com/csharp/bi...get=biginteger

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
news:ek**************@TK2MSFTNGP09.phx.gbl...
You won't go very far with int or even long. You will very quickly overflow. The decimal type won't bring you much further (28 digits max for the
result). And the double type will only give you a very approximate result,
and will stop when the result has a bit more than 300 digits.

You can write your own "infinite precision integer" library, but you can
also use the J# library for this.
Add a reference to vjslib.dll to your project (you need to install the J#
redistributable if you don't have it)
Then, you will be able to use the java.math.BigInteger class, which does
exactly what you want.

Documentation is on:
http://java.sun.com/j2se/1.4.2/docs/...igInteger.html

Be careful: take a pencil first and try to evaluate the size of your result. BigInteger can theoretically handle numbers of any size but you will be
limited by the amount of memory (the binary representation of your number
has to fit in memory).

n! ~= n**n * e**-n * sqrt(2*pi*n)

So, 32768! will fit withouth any problem but it will take a long time to
compute. On my fast machine, 1000! already takes more than 1 minute (and it already has 2568 digits!). So be ready for a few hours of computation and
several pages of digits (more than 100,000) in the end.

Bruno.

"kids_pro" <ki******@yahoo.com> a écrit dans le message de
news:eQ**************@TK2MSFTNGP10.phx.gbl...
Hi there,
I got the following code.

int f = 1;
int num =32767;

while (num>0){
f = f * num;
num --;
}

Console.WriteLine("32767! = {0}", f);

The result value was not satisfied.
How can I handle factorial which can accept any number?
If it possible?

Many thanks


Nov 16 '05 #5
The Windows calculator churns for about 10 seconds and finally comes up
with....... wait for it, wait for it :-)

2.7749286793134184196846444043574e+133729

which (as you probably know) is approximately
27749286793134184196846444043574 followed by 133697 zeros.

Best Regards
Julian Nicholls

"kids_pro" <ki******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP10.phx.gbl...
Hi there,
I got the following code.

int f = 1;
int num =32767;

while (num>0){
f = f * num;
num --;
}

Console.WriteLine("32767! = {0}", f);

The result value was not satisfied.
How can I handle factorial which can accept any number?
If it possible?

Many thanks

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
8
by: salman | last post by:
this program is giving compile time error. so plse ge me the logic of factorial # include <iostream.h> # include <math.h> void main() { int f,sum=0,i,j,n; cout<<"\nEnter Number: ";
3
by: naeemulhaq | last post by:
Can anyone suggest a better solution to finding the digit frequencies in factorial of a number, like 3! = 6 (0) 0 (1) 0 (2) 0 (3) 0 (4) 0 (5) 0 (6) 1 ...
59
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
3
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
0
kadghar
by: kadghar | last post by:
Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type. Why is this? You can...
2
by: becky808 | last post by:
Hi, I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong? #include <iostream> #include <cmath> using namespace std; int...
3
by: Blue sky | last post by:
Hi,I am a new C++ learner.The follow prgram produces a wrong result,but I can't find the wrong.Can you help me?Thank you! #include<stdio.h> long factorial( long number); int main() { int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.