Optimizing Memory Allocation in a Simple, but Long Function

Chris Angelico rosuav at gmail.com
Sun Apr 24 13:15:43 EDT 2016


On Mon, Apr 25, 2016 at 3:02 AM, Derek Klinge <schilke.60 at gmail.com> wrote:
> My problem is this: my attempt at Euler's Method involves creating a list of
> numbers that is n long. Is there a way I can iterate over the linear
> approximation method without creating a list of steps (maybe recursion, I am
> a bit new at this). Ideally I'd like to perform the linearApproximation
> method a arbitrary number of times (hopefully >10**10) and keep feeding the
> answers back into itself to get the new answer. I know this will be
> computationally time intensive, but how do I minimize memory usage (limit
> the size of my list)? I also may be misunderstanding the problem, in which
> case I am open to looking at it from a different perspective.

def EulersMethod(self, numberOfSteps): # Repeate linear approximation
over an even range
    e = 1 # e**0 = 1
    for step in range(numberOfSteps):
        e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)=
e**x, f'(x)=f(x)
    return e

This is your code, right?

I'm not seeing anywhere in here that creates a list of numbers. It
does exactly what you're hoping for: it feeds the answer back to
itself for the next step.

ChrisA



More information about the Python-list mailing list