How will you approach for program that computes this function f(X) = f(X+1) + X? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

How will you approach for program that computes this function f(X) = f(X+1) + X?

For input X=3 the program computes -2 as output and for X=4 the program computes -5. f(0) = 0 1 <= N <= 800

15th Jul 2019, 5:41 PM
Shubham Pratap Singh
Shubham Pratap Singh - avatar
4 Answers
+ 11
you didn't specified the base case, if its 3 then you can use recursion as Sonic answered, but if its for x=0(lets say) then also you can find f(0) easily & use it as base case : f(x)=f(x+1)+x let x=2 : f(2)=f(3)+2 => f(2)=-2+2=0 let x=1 : f(1)=f(2)+1 => f(1)= 0+ 1=1 let x=0 : f(0)=f(1)+0 =1 So using this we can find value if base case(here we found value of base case(x=0) as 1)
17th Jul 2019, 5:51 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 9
I think the function is better expressed as: f(x+1)=f(x)-x OR f(x)=f(x-1)-x+1 Let your f(0) value be stored as A. Then, def f(x): if x==0:return A return f(x-1)-x+1 Or you can approach this from a mathematical approach. You don't need long to figure that f(x)=A-x*(x-1)/2 or something like that which I don't know.
15th Jul 2019, 11:34 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
22nd Jul 2019, 12:40 PM
Cépagrave
Cépagrave - avatar
+ 6
I would think: f(x+1) = f(x) - x or f(n) = f(n-1) - (n -1) Use recursion. Not sure if your base case is f(3) = -2 or f(4) = -2.
15th Jul 2019, 6:23 PM
Sonic
Sonic - avatar