N-time loop of a function

Hello,

im trying to apply a function n-times to a variabel. In the screenshot I build it up with nodes, but just four times. The goal is to loop this function as often as I want to, and have a list with every single (n-times) output. I already tried with the loop-while node, but couldn´t reach it. After some research, appearently it´s only possible to solve this by Design Script, with a loop funcition, is this correct?

I´m already learning the Design Script, I would appreciate some help!

Thanks,
Stef

Welcome to the Dynamo community @stef

Hope the code below satisfies your requirement

``````n = 4;
x = 10;
y = 10;

[Imperative]
{
z = 0;
for (i in 1..n)
{
x = x + ((y*2-x)/2);
}
return x;
};
````Preformatted text```````
3 Likes

Thanks for the welcome and the solution, you helped me a lot! I transformed it a little and defined the block to use it within my script. Line 7 in your script (z=0) is not necessary, right?

1 Like

Not necessary.

While your adaptation of the code to a function is nice, if you want a list, you could also do something line this.

``````n = 4;
x = 10;
y = 10;
z = [];

[Imperative]
{
for (i in 0..(n-1))
{
z[i] = x + ((y*2-x)/2);
x = z[i];
}
return z;
};
``````

i think you can also use scan and reduce for this. i’ll update with a snippet when i get to a different computer. They’re interesting concepts that i haven’t been able to implement well beyond arithmetic examples, but they seem like useful/important ones to understand.