How to to calculate the factorial of a given integer WITHOUT using recursion.

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

Join Date: Sep 2008
Posts: 9
Reputation: cerb63 is an unknown quantity at this point 
Solved Threads: 0
cerb63 cerb63 is offline Offline
Newbie Poster

How to to calculate the factorial of a given integer WITHOUT using recursion.

 
0
  #1
Oct 16th, 2008
I need a little help, I'm still new to the C++ stuff and this is for my class. Any help anyone could give me would be great.
the code I have is as follows:
____________________________________________________________________________
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int find_fib(int);
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8. int n=0, result=0;
  9. cout << "Enter the position in the sequence to find: ";
  10. cin >> n;
  11. result = find_fib( n );
  12. cout << result << " is the " << n << "th Fibonacci sequence number" << endl;
  13. system("pause");
  14. return 0;
  15. }
  16.  
  17. int find_fib( int n )
  18. {
  19. if(n < 3)
  20. return 1;
  21. else
  22. return( find_fib(n - 2) + find_fib( n - 1 ) );
  23. }
Last edited by Ancient Dragon; Oct 17th, 2008 at 8:00 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: How to to calculate the factorial of a given integer WITHOUT using recursion.

 
0
  #2
Oct 16th, 2008
1! = 1
2! = 1 x 2 = 2
3! = 1 x 2 x 3 = 2! x 3 = 6
N! = 1 x 2 x 3 x .... (N-2) x (N-1) x N = (N-1)! x N

Could this help?

A guess:
  1. int theFact = 1;
  2. for(int i=N; i>0; i--)
  3. {
  4. theFact *= i;
  5. }
  6.  
  7. return theFact;
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: How to to calculate the factorial of a given integer WITHOUT using recursion.

 
0
  #3
Oct 16th, 2008
Did you want to try and do a non-recursive factorial or a non-recursive way to compute the fibonacci ? Please put tags around your code.

And for a non-recursive fibonacci. You could do something like this

  1. int fib(int n){
  2.  
  3. int previous = -1;
  4. int result = 1;
  5. int sum;
  6.  
  7. for(int i = 0; i <= n; i++){
  8. sum = previous + result;
  9. previous = result;
  10. result = sum;
  11. }
  12.  
  13. return result;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: How to to calculate the factorial of a given integer WITHOUT using recursion.

 
0
  #4
Oct 17th, 2008
Hallo
I post u a solution to your problem divided into three parts:

Part A: Includes your solution exactly the way you posted it. However it is an inefficient recursive solution, because it has an exponential performance.
Part B: Includes an optimized recursive solution using the help of a wrapper function which results into a O(n) performance.
Part C: Includes an non-recursive (iterative) solution.

In addition i measure the execution time for all three of them.
U can notice like expected that Part A is a bad solution.....
see here the results for finding the 42th Fibonacci number:

Enter the position in the sequence to find: 42
A: Your function gives: 267914296 is the 42th Fibonacci sequence number
The execution time of the PART A is = 4563 msecs
B: Optimized function gives: 267914296 is the 42th Fibonacci sequence number
The execution time of the PART B is = 0 msecs
C: Iterative function gives: 267914296 is the 42th Fibonacci sequence number
The execution time of the PART C is = 0 msecs
So here is the code (no errors or warnings):
  1. #include <iostream>
  2. #include <ctime> //for calculating execution time
  3.  
  4. double diffclock(time_t ,time_t ); //for calculating execution time
  5. int find_fib(int);
  6. unsigned long double fibWrapper(int);
  7. unsigned long double fibo_find_2 (unsigned long double, unsigned long double, int);
  8. unsigned long double fibo_find_3_NonRecursive(int);
  9.  
  10. int main(int argc, char* argv[])
  11. {
  12. //Required for measuring execution perfomance
  13. time_t startTime1,endTime1,startTime2;
  14. time_t endTime2,startTime3,endTime3;
  15. double elapsedTime1,elapsedTime2,elapsedTime3;
  16.  
  17. int n=0;
  18. unsigned long double result=0;
  19. std::cout << "Enter the position in the sequence to find: ";
  20. std::cin >> n;
  21.  
  22. //PART A:
  23. startTime1 = clock(); //Start measuring execution time
  24. result = find_fib(n);
  25. endTime1 = clock(); //Stop measuring execution time
  26. //Get total execution time in msec
  27. elapsedTime1 = diffclock(endTime1,startTime1);
  28. std::cout <<"A: Your function gives: "<< result << " is the "
  29. << n << "th Fibonacci sequence number" << std::endl;
  30. std::cout<<"The execution time of the PART A is = "<<elapsedTime1
  31. <<" msecs"<<std::endl;
  32. result = 0;
  33.  
  34. //PART B:
  35. startTime2 = clock(); //Start measuring execution time
  36. result = fibWrapper(n);
  37. endTime2 = clock(); //Stop measuring execution time
  38. //Get total execution time in msec
  39. elapsedTime2 = diffclock(endTime2,startTime2);
  40. std::cout <<"B: Optimized function gives: " <<result << " is the "
  41. << n << "th Fibonacci sequence number" << std::endl;
  42. std::cout<<"The execution time of the PART B is = "<<elapsedTime2
  43. <<" msecs"<<std::endl;
  44. result = 0;
  45.  
  46. //PART C:
  47. startTime3 = clock(); //Start measuring execution time
  48. result = fibo_find_3_NonRecursive(n);
  49. endTime3 = clock(); //Stop measuring execution time
  50. //Get total execution time in msec
  51. elapsedTime3 = diffclock(endTime3,startTime3);
  52. std::cout <<"C: Iterative function gives: " <<result << " is the "
  53. << n << "th Fibonacci sequence number" << std::endl;
  54. std::cout<<"The execution time of the PART C is = "<<elapsedTime3
  55. <<" msecs"<<std::endl;
  56.  
  57. return 0;
  58. }
  59. // PART A:
  60. //Your recursive solution: Correct BUT VERY INEFFICIENT exponential performance
  61. unsigned long double find_fib( int n )
  62. {
  63. if(n < 3)
  64. return 1;
  65. else
  66. return( find_fib(n - 2) + find_fib( n - 1 ) );
  67. }
  68.  
  69. //PART B:
  70. //Optimized recursive solution: Correct and with O(n) performance
  71. unsigned long double fibo_find_2(unsigned long double fibCurr,unsigned long double fibPrev, int n) //is called indirectly throuth the wrapper function
  72. {
  73. //fibCurr: the current fibonacci Number
  74. //fibPrev: the previous fibonacci number
  75. //n: the number of the fibonacci numbers left to calculate
  76. if(n == 1)
  77. return fibCurr;
  78. else
  79. return fibo_find_2(fibCurr+fibPrev,fibCurr, n-1);
  80. }
  81. //The wrapper function for calculating fibonacci numbers
  82. unsigned long double fibWrapper (int n)
  83. {
  84. //returns the value of the nth fibonacci number
  85. return fibo_find_2(1, 0, n);
  86. }
  87.  
  88. //PART C:
  89. ////Non recursive==Iterative solution
  90. unsigned long double fibo_find_3_NonRecursive (int n)
  91. {
  92. unsigned long double localresult = 1;
  93. unsigned long double temp1 = 1;
  94. unsigned long double temp2 = 0;
  95.  
  96. for (int k=3;k<=n;k++)
  97. {
  98. temp2 = localresult;
  99. localresult += temp1;
  100. temp1 = temp2;
  101. }
  102. return localresult;
  103. }
  104. //Nedded for calculating the execution time
  105. double diffclock(time_t clock1,time_t clock2) //Using time_t & difftime
  106. {
  107. double diffticks=difftime(clock1,clock2);
  108. double diffms=(diffticks)/(CLOCKS_PER_SEC/1000);
  109. return diffms;
  110. }

NOTE: if sbdy thinks that the way I calculate the execution time is inaccurate please provide an optimized and tested alternative. I tested on my machine with ACE timers but I think its not an solution available for all people.
Last edited by sidatra79; Oct 17th, 2008 at 10:16 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to to calculate the factorial of a given integer WITHOUT using recursion.

 
1
  #5
Oct 17th, 2008
Which compiler allows the nonsense "unsigned long double" as a declaration?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: How to to calculate the factorial of a given integer WITHOUT using recursion.

 
0
  #6
Oct 17th, 2008
Strangely MSVC++ 2008 does allow it .. although gcc does not.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: How to to calculate the factorial of a given integer WITHOUT using recursion.

 
0
  #7
Oct 17th, 2008
Come on guys
It was "long Double" just used the "replace all" replacing "long" with "unsigned long" having not noticed the double there...
and like u correctly said in VS 2008 gave me the compiler no clue to notice that...

But what about the rest
Is it also nonsense ????
Last edited by sidatra79; Oct 17th, 2008 at 5:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to to calculate the factorial of a given integer WITHOUT using recursion.

 
0
  #8
Oct 17th, 2008
Rest seems fine.
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