Formula for giving always the same sequence of numbers

128 Views Asked by At

I have a variable in excel that starts at 1 and it keeps incrementing indefinitely (i named it lr). I want that when lr gets to 21 it stop getting it's actual value and restart valuing 1. When it gets to 31, it must return 11. Or when it gets to 32, it must return 12. But when it gets to 41 it must return 1 again. As lr gets large, I want it to still return numbers between the $ 1, ... , 20 $ interval. One example would be 21 returns, 1 ; 22 returns, 2 ... 61, returns 1, 62 returns 2, 63 returns 3. I can't seem to find the recursion of this progression. I've tried to fit the geometric progression formula but with no luck. This always happened when you have to show 'x' elements in a page and needs a variable to keep growing, but this variable must return numbers in a specific interval. Like if you had a variable to return the number of ith question (15 questions are showed per page by default) of math.stackexchange (in the questions page) as the pages keep growing, how to do that recursively? Through a formula?

1

There are 1 best solutions below

7
On BEST ANSWER

You should use $$1+\operatorname{MOD}(\operatorname{lr}-1,20)$$ to convert $\operatorname{lr}$ as you describe.

You can keep incrementing $\operatorname{lr}$, but this expression as a function of $\operatorname{lr}$ will assume values in the range $1-20$ cyclically.

Addendum: A somewhat more general function that maps successive integers onto a cycle of $n$ successive integers is $$y = b + \operatorname{MOD}(x-a,n)$$

Here, the $n$ integers $a,a+1,\ldots,a+n-1$ are mapped successively to the $n$ integers $b,b+1,\ldots,b+n-1$. The next integer $a+n$ starts the cycle over again, being mapped to $b$ just as $a$ was. And so on.

In your case, we took $a=b=1$ and $n=20$.