21 21 votes Consider the function $F(n)$ for which the pseudocode is given below : Function F(n) begin F1 ← 1 if(n=1) then F ← 3 else For i = 1 to n do begin C ← 0 For j = 1 to n – 1 do begin C ← C + 1 end F1 = F1 * C end F = F1 end [$n$ is a positive integer greater than zero] Derive a recurrence relation for $F(n)$. Algorithms gate1992 algorithms recurrence-relation descriptive + – Kathleen 6.6k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply Kiyoshi commented Jun 9, 2021 reply Follow flag Can anyone tell what is the difference between these two questions. both looks same apparantely… https://gateoverflow.in/43600/ 0 0 replyShare Thuglife commented Oct 15, 2024 reply Follow flag in Q 7a we have to write recurrence relation and in Q7b we have to fin the value of recurrence relation at some general variable n 1 1 replyShare Please log in or register to add a comment.
Best answer 51 51 votes The function $F(n)$ is NOT a recursive function. You can't have a recurrence relation for it in the first place! $F(n)$ calculates $(n-1)^n$. The equivalent C++ code is as follows: (You can try it out here: http://ideone.com/w0u4lk) long F(long n) { long F1 = 1; if(n==1) { return 3; } else { for(long i = 1; i <= n; i++) { long C = 0; // Note: the belore For loop only has one line for(long j = 1; j <= n-1; j++) { C = C+1; } // At the end of this for loop, C will be = (n-1) F1 = F1 * C; } } return F1; } It is clear that the inner for loop can be replaced by a single statement as follows: long F(long n) { long F1 = 1; if(n==1) { return 3; } else { for(long i = 1; i <= n; i++) F1 = F1 * (n-1); } return F1; } And this calculates $(n-1)^n$ Pragy Agarwal answered May 9, 2016 • edited Jun 13, 2018 by Milicevic3306 Pragy Agarwal comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments neel19 commented Jan 28, 2021 reply Follow flag I checked. It’s $(n-1)^n$. 0 0 replyShare Pranavpurkar commented Sep 22, 2022 reply Follow flag `JEET sir It is correct. for n=5 we get 4^5. 0 0 replyShare ashishtomarx commented May 7, 2024 reply Follow flag Solving recurrence relation without recursion: https://stackoverflow.com/questions/30201391/how-to-write-a-recurrence-relation-for-a-given-piece-of-code 1 1 replyShare Please log in or register to add a comment.
2 2 votes The program is giving factorial upto (n-1) numbers here are two "for" loops But each time i value increments C initialized to 0 again Say i=4 and j=3 it will give (3!)4 as answer Recurrence relation will be an = ((n-1) an-1)n +2 srestha answered May 9, 2016 srestha comment Share Follow See all 2 Comments 2 2 Comments reply vijaycs commented May 9, 2016 reply Follow flag Why You have added 2 ?? see F(1)= 1, F(2)= 1, F(3)= (2!)^3 0 0 replyShare svas7246 commented Feb 16, 2021 reply Follow flag @srestha can you explain it more clearly how 3! came 0 0 replyShare Please log in or register to add a comment.
2 2 votes Inner loop is giving factorial of (n-1). And outer loop is multiplying the same (n-1)! to n times. F(n) = $\begin{cases} 1 & \text{ if } n= 1\\ ( (n-1)F(n-1))^{n}& \text{ if } n >1 \end{cases}$ vijaycs answered May 9, 2016 vijaycs comment Share Follow See all 2 Comments 2 2 Comments reply Pragy Agarwal commented May 9, 2016 reply Follow flag Notice where the inner loop begins and ends. begin C ← C + 1 end The following line is outside the inner loop F1 = F1 * C 6 6 replyShare vijaycs commented May 10, 2016 reply Follow flag Thanks @Pragy , I did not notice that. 2 2 replyShare Please log in or register to add a comment.
0 0 votes I think there is a recurrence relation for this function. $f(i) = f(i-1)*(n-1)$ provided $i\geq2$ and $f(1)=1$ which gives $f(n)=(n-1)^{n}$ 2019_Aspirant answered Oct 13, 2018 2019_Aspirant comment Share Follow 0 reply Please log in or register to add a comment.