473,394 Members | 1,735 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,394 software developers and data experts.

Is there any way to calculate factorials of large no ie of about n<500

occurence of each digit ie number of 0,1,2... in n! have to be calculated
Apr 19 '10 #1
10 1715
Dheeraj Joshi
1,123 Expert 1GB
Have you written any code for this? Or asking us to write the code?
You might face datatype size limitation.

Regards
Dheeraj Joshi

Note: You may try long long int.
Apr 19 '10 #2
newb16
687 512MB
long long int will bork on n=30. So... custom long integer only.
Numbers that divide by 10, like p*10 may be replaced with p, keeping track of 'lost' trailing zeroes.
Apr 19 '10 #3
Banfa
9,065 Expert Mod 8TB
The only way to do this is to write your own long integer that can handle large enough numbers. If you are using C++ the good news is that this is quite a common task so there are already a fair few unlimited precision integer implementations out there.

If you google an appropriate topic you will find lots of code snipets across the web as well as some actual implementations such as BigInt available in SourceForge.
Apr 19 '10 #4
donbock
2,426 Expert 2GB
How accurate does your computation need to be? Can you tolerate the imprecision of floating point?
Apr 19 '10 #5
newb16
687 512MB
As he needs to calculate occurrence count for each digit, floating point won't work.
Apr 19 '10 #6
jkmyoung
2,057 Expert 2GB
To add to newb16's earlier point, do not even multiply the 5's in if possible. Calculate the number of trailing zeroes before hand.

Let fives = n/5; // (rounded down, integer arithmetic)
five2s = fives/5;
five3s = five2s/5; // 125. don't need a five4s, since that would be 625, > 500.
totalFives = fives+five2s+five3s
Then you will have totalFives trailing zeroes.

When multiplying in a number, say i, remove the multiples of 5.
while (i%5 == 0) i /= 5;
Also, keep a 2 count. and don't multiply these in until necessary.
if ((i&1 == 0) && (twoCount < totalFives)) i >>= 1;
Apr 19 '10 #7
Banfa
9,065 Expert Mod 8TB
I can see you equation works jkmyoung from experiment but you you provided a proof/reason or a link to one?
Apr 19 '10 #8
jkmyoung
2,057 Expert 2GB
Do you mean with the multiples of 5, or with the cancelling out with 2's? Or the calculation of the number of trailing zeroes?

As a warning to anshulgupta : Do the problem the simple way first, then optimize.

Note, want one following clarification to the code:
Expand|Select|Wrap|Line Numbers
  1. product = 1;
  2. for (j = 2; j < n; j++){
  3.  i = j;   //set a different variable to the multiplier before 
  4.           // you do the operations on them. eg i/=5 etc...
  5.  do simplifications.
  6.  multiply product by i. 
  7.  
Otherwise you'll constantly be multiplying 2, 3, 4, 1, 2, 3, 4, 1, etc..
Apr 19 '10 #9
Banfa
9,065 Expert Mod 8TB
Sorry I meant the fives.
Apr 20 '10 #10
jkmyoung
2,057 Expert 2GB
Don't know if my explanation will just lead to more confusion.
Prime factorize n!.
1 * 2 * 3 * 4 * 5 * ... * n
= 1 * 2 * 3 * (2*2) * 5 * (3*2) * 7 * (2*2*2) * (3*3) * (2*5) *... * n
Take the first multiple of 5 from each number that has it. (every 5th number)
There will be exactly [fives] of these multiples, where [fives] is calculated above.
= 1 * 2 * 3 * (2*2) * 1 * (3*2) * 7 * (2*2*2) * (3*3) * (2*1) *...

Then take the second multiple of 5 from each number that still has one. (every 25th number still has a multiple of 5)
There will be exactly [five2s] of these.
etc...

Since we are taking a 5 from each factor each time, during each round we can never take a factor of 5 from a number that hasn't had a factor of 5 taken from it in each of the previous rounds; otherwise, the factor of 5 would already have been taken then!
======
To prove all multiples of 5 cancel with some power of 2:
Each factor of 5 corresponds to a factor of 2 like so
Take all natural numbers i, where 5i < n
2i < 5i < n, so is 5i exists as an individual number in the multiplication, so does 2i.
Take the 5 from 5i and the 2 from 2i and put them aside.

But then you say, what about a number that has 2 factors of 5? eg 5*5*i,
Well 2*2*i < 5*5*i.

Notice that 10 is used twice: Once as a multiple of 5, (cancelling against 2*2=4). Once as a multiple of 2, (cancelling against 5*5=-25) .

4 cancels twice as a multiple of 2. Once as a multiple of 2, (against 10) and once as a multiple of 4, (against 25)

25 cancels both multiples of 5. Once against 10, and once against 4.
===
so any number i*5^j, cancels with numbers i*2^j, i*2^(j-1)*5, i*2^(j-2)*5^2... i*2*5^(j-1)
Notice, (if we count by the powers of 2), there are exactly j terms, cancelling out with each of the j multiples of 5 in i*5^j
==========
Apr 20 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
1
by: Adriaan Renting | last post by:
I think the point you want to make is that Python needs vastly less lines of code as a similar application written in C++. I think Python might on average be 50-60% of comparable C++ code, but not...
1
by: John | last post by:
I'm developing an application for medical use that will be used to capture patient background and visit data. The application will have approximately 50 forms, with an average of about 20 fields...
3
by: es22 | last post by:
Hi, I'm trying to decide whether to use one large table or many small tables. I need to gather information from various devices (about 500). Each device has its own Id and some data. Should I...
2
by: JP SIngh | last post by:
Hi All I am using couple of dropdowns on a form which pulls data from one of our tables. The data list is quite large (500+) and it takes users a lot of time to find the correct item. Users...
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
2
by: jdev8080 | last post by:
We are looking at creating large XML files containing binary data (encoded as base64) and passing them to transformers that will parse and transform the data into different formats. Basically,...
10
by: Jonathan Sion | last post by:
Hi, I got a large text string, and a bunch of regular expression patterns i need to find within the string. in other words, to find all the 'tokens' inside it. of course I could use the regexp...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.