Writing a math routine

I need to take a big number N then calculate (N-2) and store the number as (N-2) instance 1,2,3…etc…

For example 153 - 2 = 151 keep 151 as instance 1
Then 151 - 2 = 149 keep 149 as instance 2
Then 149 - 2 = 147 keep 147 as instance 3

Then work with these numbers later…
Just gettin’ started… Thank you

Store the numbers (“instances”) in a list.

Thank you. I’ll look into it and try it out.
I want to make sure I can label the numbers each with a label of 1+ whatever the last number was.

Like I need for sure to put the first number in the list stored next to a number 1.
The second stored next to a number 2.

Ultimately, I need the numbers stored so I can work with them.
For instance, continuing what I began…

153 - 2 = 151 so store 151 first in the list

Then “add the first number in the list with its list number”.
Then “add the second number in the list with its list number”…

IF 151 was the first number in the list it gets 1 as its list number
=so it spits out 152.
IF 149 was the second number in the list it gets 2 as its list number
=so it spits out 151, etc…
Way to do this?

In Python, list indexes (positions) count from 0, not 1, but it’s easy to add 1 to compensate.

Any decent Python tutorial will tell you about lists.

Ok, thanks.

mathematically, I think, if you know N & 2 then you know every instance without writing anything down

Although (as pointed out) Python uses ‘zero indexing’, you can force an index to start at 1, by using enumerate(), which is one of the many ‘builtins’. e.g: enumerate(N_instance, start=1)

By using that in a for index... loop, you can add the value of index (which will be incremented by 1 with each iteration of the for loop) to every value in the N_instance list.

If you don’t understand what I mean or need help, then post back and say as much. I can post the working code for you, but it would be better for you to at least try, as you’ll learn more by working this out for yourself.

Hi Rob, I need help, just getting started… If you could post the working code, I’ll try it and try to understand it. Thanks for your help.

I’d really like to see you to try this for yourself.

Start with:

N = 153
N_instance = []

and think about what you need to do next. Hint: you’ll need a loop that appends the value of N to the N_instance list, while ever N is greater then zero.

Once you’ve got that part sussed, you can move on to the next part.

Try this site, if you’re stuck for a tutorial:

One example often used with a similar factorial implementation is to use @cache and to use arguments to allow for recursive usage.

Edit: coming back to this at a later time, though maybe too complex for the original poster. I often try to use itertools to help improve performance. You could also change how you go about the solution, use a generator and then use accumulate to get the same result as proposed by others.