-1

I am trying to make a for loop which calculates the following

#x_i = (1+1/i)x_{i-1}

with x_0=1 I know how to do the first part which is

for i in range(1, 3):
    print(1+1/i)

I am not sure how to make the x_{i-1} work, so it should take the previous iteration and use the calculated value in making the new x.

So the final result I am looking for would be a list, looks as follow:

#[1, (1 + 1/1), (1 + 1/1)(1 + 1/2),(1 + 1/1)(1 + 1/2)(1 + 1/3),...]
Will
  • 47
  • 5
  • x_0=1, x_1=2 and so on? – Rohi Apr 25 '18 at 12:21
  • Do you want to end up with a bunch of variables like `x_0`, `x_1`, `x_2` etc or are you only interested in the final value of the calculation? – Aran-Fey Apr 25 '18 at 12:24
  • @Rohi somehow, for example [1, (1 + 1/1), (1 + 1/1)(1 + 1/2), ... ] – Will Apr 25 '18 at 12:24
  • @Aran-Fey only the numerical answers – Will Apr 25 '18 at 12:25
  • `x_{i-1}` is the previous value of `x`. Use `x_0=1` as starting point you calculate later values by `x_new = (1+1/i)*x` and update in each step – Eypros Apr 25 '18 at 12:25
  • Ok, then the duplicate is correct. Store all the intermediate results in a list or dict. – Aran-Fey Apr 25 '18 at 12:26
  • @Aran-Fey Not sure the duplicate is correct, since he isn't stating he wants to save them, and he could solve this with recursion. – Rohi Apr 25 '18 at 12:27
  • @Will As you can see, there is some confusion about your question. Please include the desired result in your question. Is it an integer? Is it a list of integers? Is it a bunch of variables holding integers? – Aran-Fey Apr 25 '18 at 12:29
  • @Aran-Fey I have just done so – Will Apr 25 '18 at 12:33
  • Okay, I've reopened the question. – Aran-Fey Apr 25 '18 at 12:33
  • @Rohi I don't think recursion is required here, just a simple non-recursive loop should be fine (unless I'm misunderstanding the question ;) ). Generally, recursion should be avoided in Python, unless it's appropriate for the problem domain (like processing recursive data structures) because Python cannot perform tail call optimization, so you get all that extra overhead of slow Python function calls. – PM 2Ring Apr 25 '18 at 12:34
  • @PM2Ring Didn't know recursion should be avoided, is this an opinion or a widely accepted concept? – Rohi Apr 25 '18 at 12:36
  • 1
    @Rohi It's widely accepted. Somewhat ironically, I have posted a lot of recursive Python code on SO, but that's because I _love_ recursive generators. :) – PM 2Ring Apr 25 '18 at 12:41

1 Answers1

2

I hope I understood you correctly.

initial_value = 1
answer_list = []
answer_list.append(initial_value)
for i in range(1, 3):
    answer_list.append((1+1/i)*answer_list[i-1])

print(answer_list)

This should work for you, basically what you want to do is insert the answer into a list in a consecutive manner and multiple in the (index-1).

Rohi
  • 814
  • 1
  • 9
  • 26
  • The produced list is [0,0,0] – Will Apr 25 '18 at 12:44
  • @Will Accidently set the initial value to 0, change it to whatever value you like. – Rohi Apr 25 '18 at 12:45
  • when I do print(answer_list.append((1+1/i)*answer_list[i-1])) I get none none none – Will Apr 25 '18 at 12:48
  • @Will what the code does is fill answer_list with values according to your request. After the loop is done, add : print(answer_list) – Rohi Apr 25 '18 at 12:51
  • @Will Why are you doing that? Just print the final `answer_list` *after* the `for` loop has finished. The `.append` method returns `None`, which is why "None" is getting printed. – PM 2Ring Apr 25 '18 at 12:53