Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. /*
  2. * Name: Savio Alphonso
  3. * Student Number: 100256601
  4. * Course: CPSC 1160 - 001 | Assignment 3: Recursive vs Iterative
  5. * Limitations:
  6. * - Program cannot handle text input -- it goes into an endless loop
  7. * - Program is limited to factorials up to 100 digits long
  8. * - Fibonacci Sequences are limited to the limit of a long long
  9. * Sources:
  10. * - https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic
  11. * Function:
  12. * - This program calculates n-th factorial/Fibonacci Sequence using recursive
  13. * and iterative methods.
  14. */
  15.  
  16. #include <iostream>
  17.  
  18. using namespace std;
  19.  
  20. const int DIGIT_LIMIT = 100; // maximum number of digits we can store in the array
  21. const int BASE = 10; // the base we're currently working in
  22.  
  23.  
  24. //Iterative way to find the n-th term in the Fibonacci Sequence.
  25. // Parameters: n (Integer) - n-th value of Fibonacci Sequence to find
  26. long long iterFibo(int n){
  27. long long temp; //temp variable
  28. long long prevFib = 0; // previous sequence number
  29. long long curFib = 1; // current sequence number
  30.  
  31. if (n == 0 || n == 1) {
  32. return n; // if n is equal to 1 or 0 return 1 or 0 respectively
  33. }else{
  34. for (int i = 1; i < n; i++) {
  35. temp = curFib; // store the current value
  36. curFib += prevFib; //add the previous value to the current
  37. prevFib = temp; // set previous value
  38. }
  39. }
  40.  
  41. return curFib;
  42. }
  43.  
  44. // Recursive way of finding n-th term of the Fibonacci Sequence
  45. // Parameters: Integer n - n-th term of the Sequence to find
  46. long long recFibo(int n){
  47. if (n == 0 || n == 1) {
  48. return n; // if n is equal to 1 or 0 return 1 or 0 respectively
  49. }else{
  50. return recFibo(n-1) + recFibo(n-2); // recursive call.
  51. }
  52. }
  53.  
  54.  
  55.  
  56. // Iterative way to find n-th term of a factorial.
  57. // Since long long can only store 19 digits we need to store it the digits in an array
  58. // Parameters: (Integer) n - n-th term to find
  59. void iterFact(int n){
  60.  
  61. /*
  62. Since we want to display arbritary precise number we have store the number in
  63. an array, where each element is one digit, for our purposes we need to store up
  64. to 50!
  65. */
  66.  
  67. int factorialLimit = n; // the factorial we want to find
  68. int digits[DIGIT_LIMIT] = {0}; //fill the arrays with zeros with size of the Limit
  69. int carry, placeVal; //carry variable and current place
  70.  
  71. digits[DIGIT_LIMIT-1] = 1; //set the first factorial.
  72. placeVal = DIGIT_LIMIT - 1; //stores amount of digits
  73.  
  74. for(int f = 1; f <= factorialLimit; f++){
  75. carry = 0;
  76. for(int d = 99; d >= placeVal; --d){
  77. digits[d] = (digits[d]*f) + carry; //find the factorial and the base
  78. carry = digits[d] / BASE; // calculate the carry
  79. digits[d] %= BASE; // store single digit in the array
  80. }
  81.  
  82. //Handles carrying digits & calculating number of digits
  83. while(carry > 0) {
  84. if(placeVal >= DIGIT_LIMIT){
  85. cout << "overflow" <<endl;
  86. }
  87.  
  88. placeVal--;
  89. digits[placeVal] = carry % BASE;
  90. carry /= BASE;
  91. }
  92.  
  93.  
  94. }
  95. //print without leading zero
  96. for (int i = placeVal; i < DIGIT_LIMIT; i++) {
  97. cout << digits[i];
  98. }
  99.  
  100. }
  101.  
  102. // Recursive way to find n-th term of a factorial.
  103. // Since long long can only store 19 digits we need to store it the digits in an array
  104. // Parameters: (Integer) n - n-th term to find
  105. void recFact(int digitArr[], int n){
  106.  
  107. if (n == 0){
  108. return; // return nothing if n = 0, array take care of adding a zero.
  109. }
  110.  
  111.  
  112. //Similar to how the iterative precision works, but modified for recursive
  113. int carry = 0;
  114. for (int i=DIGIT_LIMIT-1; i>=0; --i){
  115. digitArr[i] = (digitArr[i] * n) + carry;
  116. carry = digitArr[i] / BASE;
  117. digitArr[i] %= BASE;
  118. }
  119. recFact(digitArr,n-1);
  120. }
  121.  
  122. //Prints the recursive funtion array
  123. //Parameters: Single Integer Array digitArr - array created by the recursive factorial function
  124. void printRecFact(int digitArr[]){
  125.  
  126. int firstNonZero = false;
  127.  
  128. for (int i = 0; i < DIGIT_LIMIT; i++) {
  129. if (!firstNonZero && digitArr[i] != 0) { //skip printing until we find a non zero number
  130. firstNonZero = true;
  131. }
  132.  
  133. if (firstNonZero) {
  134. cout << digitArr[i];
  135. }
  136. }
  137.  
  138. fill(digitArr, digitArr+(DIGIT_LIMIT),0); // reset the array after printing
  139. digitArr[DIGIT_LIMIT - 1] = 1; // set first factorial value
  140. }
  141.  
  142. int main() {
  143. int digits[DIGIT_LIMIT] = {0}; //set array to zero
  144. digits[DIGIT_LIMIT - 1] = 1; //set first factorial value
  145.  
  146.  
  147. char answer = 'Y';
  148. int n;
  149. do {
  150. cout << endl << endl;
  151. cout <<">===============( New RUN )===============<" <<endl;
  152. cout << "\nPlease enter an integer number: ";
  153. cin >> n;
  154. cout << endl;
  155. cout << "Iterative Factorial(" << n <<") is ";
  156. iterFact(n);
  157. cout << endl << "Recursive Factorial(" << n <<") is ";
  158. recFact(digits, n);
  159. printRecFact(digits);
  160. cout <<endl << "Iterative Fibonacci(" << n <<") is " << iterFibo(n) << endl;
  161. cout << "Recursive Fibonacci(" << n <<") is " << recFibo(n) << endl << endl;
  162. cout << "Do you want to continue? (Y/N)";
  163. cin >> answer;
  164. digits[DIGIT_LIMIT] = {0};
  165. } while (toupper(answer) == 'Y');
  166. cout << "The Program has ended gracefully!" ;
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement