Question

In: Computer Science

Rewrite your program for finding Pascal's Triangle to use iteration (loops) instead of recursion. Include in...

Rewrite your program for finding Pascal's Triangle to use iteration (loops) instead of recursion. Include in both algorithms code to keep track of the total time used to run the algorithms in milliseconds. Run these algorithms for n = 10, n = 20, and n = 30. Print out both the original output and the time to run for both algorithms. Please make sure you comment your code thoroughly. The code should be nicely formatted and should use proper variables.

Solutions

Expert Solution

CODE

public class Main {

// Function to print first

// n lines of Pascal's Triangle

static void printPascal(int n)

{

// Iterate through every line

// and print entries in it

for (int line = 0; line < n; line++)

{

// Every line has number of

// integers equal to line number

for (int i = 0; i <= line; i++)

System.out.print(binomialCoeff

(line, i)+" ");

System.out.println();

}

}

static int binomialCoeff(int n, int k)

{

int res = 1;

if (k > n - k)

k = n - k;

for (int i = 0; i < k; ++i)

{

res *= (n - i);

res /= (i + 1);

}

return res;

}

// Driver code

public static void main(String args[])

{

long start = System.currentTimeMillis();

int n = 10;

printPascal(n);

long end = System.currentTimeMillis();

System.out.println("Time taken for n = 10: " + (end - start) + " milliseconds." );

start = System.currentTimeMillis();

n = 20;

printPascal(n);

end = System.currentTimeMillis();

System.out.println("Time taken for n = 20: " + (end - start) + " milliseconds." );

start = System.currentTimeMillis();

n = 40;

printPascal(n);

end = System.currentTimeMillis();

System.out.println("Time taken for n = 40: " + (end - start) + " milliseconds." );

}

}

OUTPUT

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

Time taken for n = 10: 1 milliseconds.

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

1 10 45 120 210 252 210 120 45 10 1

1 11 55 165 330 462 462 330 165 55 11 1

1 12 66 220 495 792 924 792 495 220 66 12 1

1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1

1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1

1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1

1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1

1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1

1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1

1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1

Time taken for n = 20: 5 milliseconds.

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

1 10 45 120 210 252 210 120 45 10 1

1 11 55 165 330 462 462 330 165 55 11 1

1 12 66 220 495 792 924 792 495 220 66 12 1

1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1

1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1

1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1

1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1

1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1

1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1

1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1

1 20 190 1140 4845 15504 38760 77520 125970 167960 184756 167960 125970 77520 38760 15504 4845 1140 190 20 1

1 21 210 1330 5985 20349 54264 116280 203490 293930 352716 352716 293930 203490 116280 54264 20349 5985 1330 210 21 1

1 22 231 1540 7315 26334 74613 170544 319770 497420 646646 705432 646646 497420 319770 170544 74613 26334 7315 1540 231 22 1

1 23 253 1771 8855 33649 100947 245157 490314 817190 1144066 1352078 1352078 1144066 817190 490314 245157 100947 33649 8855 1771 253 23 1

1 24 276 2024 10626 42504 134596 346104 735471 1307504 1961256 2496144 2704156 2496144 1961256 1307504 735471 346104 134596 42504 10626 2024 276 24 1

1 25 300 2300 12650 53130 177100 480700 1081575 2042975 3268760 4457400 5200300 5200300 4457400 3268760 2042975 1081575 480700 177100 53130 12650 2300 300 25 1

1 26 325 2600 14950 65780 230230 657800 1562275 3124550 5311735 7726160 9657700 10400600 9657700 7726160 5311735 3124550 1562275 657800 230230 65780 14950 2600 325 26 1

1 27 351 2925 17550 80730 296010 888030 2220075 4686825 8436285 13037895 17383860 20058300 20058300 17383860 13037895 8436285 4686825 2220075 888030 296010 80730 17550 2925 351 27 1

1 28 378 3276 20475 98280 376740 1184040 3108105 6906900 13123110 21474180 30421755 37442160 40116600 37442160 30421755 21474180 13123110 6906900 3108105 1184040 376740 98280 20475 3276 378 28 1

1 29 406 3654 23751 118755 475020 1560780 4292145 10015005 20030010 34597290 51895935 67863915 77558760 77558760 67863915 51895935 34597290 20030010 10015005 4292145 1560780 475020 118755 23751 3654 406 29 1

1 30 435 4060 27405 142506 593775 2035800 5852925 14307150 30045015 54627300 86493225 119759850 145422675 -131213633 145422675 119759850 86493225 54627300 30045015 14307150 5852925 2035800 593775 142506 27405 4060 435 30 1

1 31 465 4495 31465 169911 736281 2629575 7888725 20160075 44352165 84672315 141120525 -124129024 147188918 -119517046 -119517046 147188918 -124129024 141120525 84672315 44352165 20160075 7888725 2629575 736281 169911 31465 4495 465 31 1

1 32 496 4960 35960 201376 906192 3365856 10518300 28048800 64512240 129024480 -132121101 127118867 -134264915 125213255 133039083 125213255 -134264915 127118867 -132121101 129024480 64512240 28048800 10518300 3365856 906192 201376 35960 4960 496 32 1

1 33 528 5456 40920 237336 1107568 4272048 13884156 38567100 92561040 193536720 -3096621 -5002233 -7146047 -9051659 -10183116 -10183116 -9051659 -7146047 -5002233 -3096621 193536720 92561040 38567100 13884156 4272048 1107568 237336 40920 5456 528 33 1

1 34 561 5984 46376 278256 1344904 5379616 18156204 52451256 131128140 -104353812 157902468 -63162538 -94743807 -126325076 118424428 125390570 118424428 -126325076 -94743807 -63162538 157902468 -104353812 131128140 52451256 18156204 5379616 1344904 278256 46376 5984 561 34 1

1 35 595 6545 52360 324632 1623160 6724520 23535820 70607460 183579396 26774327 53548654 94739926 148877026 -77903316 -97379145 -108835515 -108835515 -97379145 -77903316 148877026 94739926 53548654 26774327 183579396 70607460 23535820 6724520 1623160 324632 52360 6545 595 35 1

1 36 630 7140 58905 376992 1947792 8347680 30260340 94143280 -175309873 -23917218 -49827537 -91989299 -151125276 64680748 84893481 99874683 105423276 99874683 84893481 64680748 -151125276 -91989299 -49827537 -23917218 -175309873 94143280 30260340 8347680 1947792 376992 58905 7140 630 36 1

1 37 666 7770 66045 435897 2324784 10295472 38608020 124403620 -81166593 191224480 56405765 108472625 -120830306 101058017 -129480682 92698410 102998233 102998233 92698410 -129480682 101058017 -120830306 108472625 56405765 191224480 -81166593 124403620 38608020 10295472 2324784 435897 66045 7770 666 37 1

1 38 703 8436 73815 501942 2760681 12620256 48903492 163011640 43237026 110057884 -110283702 109814695 -110685708 109234020 -111411552 108465479 -112066235 108086452 -112066235 108465479 -111411552 109234020 -110685708 109814695 -110283702 110057884 43237026 163011640 48903492 12620256 2760681 501942 73815 8436 703 38 1

1 39 741 9139 82251 575757 3262623 15380937 61523748 211915132 206248666 153294910 -225818 -469006 -871011 -1451685 -2177527 -2946065 -3600746 -3979771 -3979771 -3600746 -2946065 -2177527 -1451685 -871011 -469006 -225818 153294910 206248666 211915132 61523748 15380937 3262623 575757 82251 9139 741 39 1

Time taken for n = 40: 15 milliseconds.


Related Solutions

How to rewrite the bin() function below using recursion instead of a for loop in C?...
How to rewrite the bin() function below using recursion instead of a for loop in C? #include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdint.h> char* bin(int x, int i); int main(int argc, char **argv) { char* binstr = bin(10, 0); printf("%s\n", binstr); free(binstr); return 0; } char* bin(int x, int i){ int bits = log10((double) x)/log10(2)+1; char* ret = malloc((bits+1)*sizeof(char)); for(int i = 0; i < bits; i++){ ret[bits-i-1] = (x & 1) ? '1' : '0'; x >>= 1;...
C programming Rewrite the following function using no loops, and only tail call recursion double question5...
C programming Rewrite the following function using no loops, and only tail call recursion double question5 (int in) { int i; int result; for (result = rand(), i = 0; i < in; i += 3) { result /= i; result += rand(); } return result; }
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] =...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };   //Challenge #2: print the list from given range   printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6   cout << endl;   //Challenge #3: print the list, but backwards   printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8   cout << endl;                  //Challenge #4: reverse order of items in list   reverse(nums,...
In what case(s) would you use recursion over iteration and which approach is more memory efficient...
In what case(s) would you use recursion over iteration and which approach is more memory efficient and why when you compare which approach would you say is faster and why?
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm to work along with a worked exercise from Chapter 5. Use two different looping strategies for different purposes. Prior Task Completion: 1. Read Chapter 05. 2. View Chapter 05’s video notes. 3. As you view the video, work along with each code sample in PyCharm using the Ch 5 Iteration PPT Code Samples.zip. Important: Do not skip the process of following along with the...
An excellent example of a program that can be greatly simplified by the use of recursion...
An excellent example of a program that can be greatly simplified by the use of recursion is the Chapter 4 case study, escaping a maze. As already explained, in each maze cell the mouse stores on the maze stack up to four cells neighboring the cell in which it is currently located. The cells put on the stack are the ones that should be investigated after reaching a dead end. It does the same for each visited cell. Write a...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
python code You are to write a program which produces a triangle as seen below. •Include...
python code You are to write a program which produces a triangle as seen below. •Include a function named goingUpwhich accepts a value and prints lines of stars beginning at 1 star and ending at the number of stars which was sent to it. For example, if the value sent to it is 4, the function would print this:********** •Include a function named goingDown which accepts a value and prints lines of stars beginning at 1 LESS THAN the value...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT