Factorial of any length

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Factorial of any length

 
0
  #1
Nov 29th, 2004
i have this assignment to compute the factorial of an integer of any length. I have done it using array of char and then overloading the * operator accordingly. It works fine but i wanted to know if there were other efficient algorithms for counting factorials of any length. Give me some links if u find some. My search in google did not return anything exciting. If urs does then plz give me the links.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Factorial of any length

 
0
  #2
Nov 30th, 2004
This is a snippet i got from the one of the sites:
  1. #include <cmath>
  2. #include <iostream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. unsigned int d;//number of digits
  10. unsigned char *a;
  11. unsigned int j, n, q, z, t;
  12. int i;
  13. double p;
  14.  
  15. cout << "\nEnter the No: ";
  16. cin >> n;
  17.  
  18. p = 0.0; //calculate the number of digits required
  19. for(j = 2; j <= n; j++)
  20. p += log10(j);
  21.  
  22. d = (int)p + 1;
  23. a = new unsigned char[d];
  24.  
  25. if (!a)
  26. {
  27. cout << "Could not allocate memory!!!";
  28. exit(0);
  29. }
  30.  
  31. for (i = 1; i < d; i++)
  32. a[i] = 0; //initialize
  33. a[0] = 1;
  34.  
  35. p = 0.0;
  36. for (j = 2; j <= n; j++)
  37. {
  38. q = 0;//initialize remainder to be 0
  39. p += log10(j);
  40. z = (int)p + 1;
  41. for (i = 0; i <= z/*NUMDIGITS*/; i++)
  42. {
  43. t = (a[i] * j) + q;
  44. q = (t / 10);
  45. a[i] = (char)(t % 10);
  46. }
  47. }
  48.  
  49. cout << "\nThe Factorial is: ";
  50. // the fact is stored in reverse..
  51. for( i = d -1; i >= 0; i--)
  52. cout << (int)a[i];
  53.  
  54. delete []a;
  55.  
  56. return 0;
  57.  
  58. }

But the problem is i dont understand what its doin and why, especially here:
  1. for(j = 2; j <= n; j++)
  2. p += log10(j);
I guess its counting the number of digits required to hold the factorial, but where did this formula come from?

And MOST importantly whats goin on in here:

  1. for (i = 1; i < d; i++)
  2. a[i] = 0; //initialize
  3. a[0] = 1;
  4.  
  5. p = 0.0;
  6. for (j = 2; j <= n; j++)
  7. {
  8. q = 0;//initialize remainder to be 0
  9. p += log10(j);
  10. z = (int)p + 1;
  11. for (i = 0; i <= z/*NUMDIGITS*/; i++)
  12. {
  13. t = (a[i] * j) + q;
  14. q = (t / 10);
  15. a[i] = (char)(t % 10);
  16. }
  17. }

Could anyone explain me whats goin on inside the loop?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 41
Reputation: varunrathi is an unknown quantity at this point 
Solved Threads: 1
varunrathi's Avatar
varunrathi varunrathi is offline Offline
Light Poster

Re: Factorial of any length

 
0
  #3
Dec 8th, 2004
Can u please, post ur solution
If u can`t post the soln. then can u please explain how to find the factorial of a number of any length.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Factorial of any length

 
0
  #4
Dec 8th, 2004
That's the problem when you pull code from some website or book and submit it as your own... You learn nothing from it.
Learn to make your own code, learn to understand the code of others.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Factorial of any length

 
0
  #5
Dec 11th, 2004
okay in my own solutions i defined a class named BigInt. The class could hold an integer of any length, and the operator + was overloaded for that class so that two BigInt could be added. I did this assignment a semester ago. So when i was asked to find factorials of any length i just new that the basic problem is to do multiplication like this:
72172817218122121 x 7812

But i could do additions of numbers like this 7128712821812+1282817281718 with my BigInt. And because multiplication is just repeated addition i overloaded the * operator and defined multiplication for my BigInt class. That's how i solved the factorial of any length problem.

But implementing multiplication with repeated addintion is an exponential process and it gets slower for larger values exponentially. THATS WHY I was trying to know if there were other efficient methods and the code i posted here was a result of my first attempt to find a solution made by others.

And i really did not stop there. I did some further studies on the topic and came to know that the problem of finding factorials of arbitrary length is just a instance of not having an arbitrary-precision-arithmetic. So if i could implement a arbitrary-precision-arithmetic i could find factorials of any length and could do more.

First thing i learned when trying to implement APA is that i should convert the input(say the integer) into a number of higher base. The base should be the square root of the largest integer that can be handled by the machine. So then i will need less memory and most importantly number of digits to add or multiply would be significantly fewer.

The next thing i learned about implementing multiplication is that "i should avoid doing multiplication by repeated addition" since its an O(n^2). Then i could do multiplication like the way we multiply in our real life. Well that was sufficient for me. But i also came to know that this multiplication can be implemented with even faster algorithms namely

1. Karatsuba's divide-and-conquer algorithm
2. Fourier transforms
3. Chinese remainder theorem and modular arithmetic.

I possess little or no knowledge on this three process. So i guess thats as far as i could go.

And one more thing... i tried to convert the input into a number of higher base. I can do this by simple division and remainder process that we all know. But that works for normal integers. But while working wit big sized integers i have to convert the input and put that digit by digit into an array of characters. But then i cannot divide the whole number --bcos there is no number and only discrete digits. Can anyone suggest how can i convert an integer of any length into a higher base.

Thank u.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Factorial of any length

 
0
  #6
Dec 11th, 2004
Originally Posted by jwenting
learn to understand the code of others.
I did try and now i know that

  1. for(j = 2; j <= n; j++)
  2. p += log10(j);

this piece of code calculates the number of digits required to hold the factorial of that number. Suppose i have a decimal number 100, then the number of digits required to hold 100 in decimal system is log10(100) + 1.
Same is true for number of any base.

  1. for (i = 1; i < d; i++)
  2. a[i] = 0; //initialize
  3. a[0] = 1;
  4. p = 0.0;
  5. for (j = 2; j <= n; j++)
  6. {
  7. q = 0;//initialize remainder to be 0
  8. p += log10(j);
  9. z = (int)p + 1;
  10. for (i = 0; i <= z/*NUMDIGITS*/; i++)
  11. {
  12. t = (a[i] * j) + q;
  13. q = (t / 10);
  14. a[i] = (char)(t % 10);
  15. }
  16. }

And this part multiplies the contents in the array first with 2 and then puts the result into the array. Then goes to the next number 3 and multiplies content of the array with 3 and then to the next number and so on. Thus if we have 5 as input it calculates 1*2->2*3 ->6*4->24*5->120 and thus we get the factorial.

The assignment was given just two days before the submission date and i had no time to study further and learn more. Even though i now understand what the code does , i did not know what it was doin at the time i posted it and asked for help. I m new to prgramming and have only learned the syntax of C and C++ and that is also not exhaustive. I did not have the oppurtunity to play with different algorihtmic tricks involved in programming. Therefore, i face a hard time devising my own algorithms to mathematical problems and also to learn from others code, simply bcos i am not familiar with different mathematical tricks involved in the code. I hoped i could overcome it by asking help from people who already know about them and that would shorten my learning curve.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Factorial of any length

 
0
  #7
Dec 11th, 2004
Here is the way I figured it out:

Let's say 5! = 1 * 2 * 3 * 4 * 5
the 1 drops out then 5! = 2 * 3 * 4 * 5
apply the log operator log(5!) = log(2) + log(3) + log(4) + log(5)

for(j = 2; j <= n; j++)
p += log10(j);

p is just log10(n!), by the way you could use exp10(p) to get an approximation of n!

then build up a numeric string using an array of characters with 2 nested for loops, the size of the array is p + 1

the way the nested for loops work the numeric string is in reverse, so spell it from the back
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 41
Reputation: varunrathi is an unknown quantity at this point 
Solved Threads: 1
varunrathi's Avatar
varunrathi varunrathi is offline Offline
Light Poster

Re: Factorial of any length

 
0
  #8
Dec 13th, 2004
how to define a class which can "hold an integer of any length". my problem is that i am unable to define an integer of any length".
"Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Factorial of any length

 
0
  #9
Dec 13th, 2004
NO-ONE has used the simple loop method! im not sure how it compares but the posted snippet a while back certainly seems overkill for a simple factorial function.

Heres my code:

long Factorial (int base) // Factorial(4) = 4!
{
long result = (long)base;
for(int i = base - 1; i > 1; i--) // i > 1 avoids x 0 error if i = 0
{
result *= (long)i;
}
return result;
}

someone please tell me how to display my code properly as my posts arent looking as they should!

if you want a string then log10 the result, add 2 (string has '0' char at end) and divide by 1 then 10 then 100 ect, in powers of 10 to get digits and add to string.... im not sure where (or if) the log10 function is or exactly how to build the string, but im sure someone else could do it on this forum...
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 7
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: Factorial of any length

 
0
  #10
Dec 14th, 2004
Originally Posted by jwenting
That's the problem when you pull code from some website or book and submit it as your own... You learn nothing from it.
Learn to make your own code, learn to understand the code of others.
Copy Paste Find Replace! Hahah stay away from that philosphy and you will do alright.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC