Derivation of the formula for the nth term for a recurrent sequence using the example of the task from the Amnesia quest
Pelengsky farmer Bukh'rik breeds hryaklyuklyuhov. These animals breed so quickly that their livestock increases 3 times daily. But, starting from the second day, a flock of terrible dolbogryz animals began to attack the farm, each evening devouring twice as much hryakoplyukhov than they were on the previous day. How many farms will the farmer have on the 7th evening, if at first there were 10 of them?
You asked the Gaalan what he thinks about this problem. After some reflection, he replied: - In the beginning there were 10 hryakoplyukhs. On the first day, they multiplied, and by the beginning of the second day they were 30. On the second day, they multiplied again (there were 90), but in the evening dolbogryzy came and ate twice as many hryakoplyukhov than it was yesterday (on the first day), i.e. 20 pieces. Total, at the beginning of the third day we get 70 hryakoplyukhov. It seems to me that, continuing to solve in this way, it is possible to calculate the number of hryakoplyukhov on any given day.
This is a task from the game "Space Rangers 2", quest Amnesia. Let's try to derive a formula for the number of hryakoplyukhov on the n-th day, and count for example the number of hryakoplyukhov on the 32nd day. Let's look at the first few days (I added day “0” only for the convenience of the formula, you can do everything without it)
Day "0"
Day 1
Day 2
Day 3
Morning
0
ten
thirty
70
After breeding
0
10 * 3 = 30
30 * 3 = 90
70 * 3 = 210
After dolbogryzov (evening)
0
10 * 3 - 0 * 2 = 30
30 * 3 - 10 * 2 = 70
70 * 3 - 30 * 2 = 150
We will be interested in the number of hryakoplyukhovs on the morning of the nth day, and, despite the fact that the task asks about the evening, the morning number will be correct. Thus, the number of hryakoplyukhov (in the morning) is a sequence
We know about this sequence (by the condition of the problem) that and ')
Also, according to the condition of the problem, nothing happens at night, so the hryaklyuchs in the evening are transferred without changes to the next morning (i.e. the number of hryakyklyuhs in the morning is equal to their number in the evening of the previous day).
Those. on the morning of the second day they became . On the morning of the third day they are already .
Day n-2
Day n-1
Day n
Morning
After breeding
After dolbogryzov (evening)
Starting from the second day the following recurrence formula works:
Total, all together again
We have obtained a formal definition of the sequence and this is already enough so that we can manually calculate how many hryakoplyukhovs will be on which day. Moreover, if we magically have some formula for , and this formula is satisfied for these 3 conditions, then this formula will be what we need.
We will try to get this formula.
As a rule, to find something you need to make an equation, so we will do it. Our sequence - this is what we want to find (more precisely, we want to get a formula for its members).
We will make an equation out of sequences. To do this, invent the addition for the sequences:
Those. the sum of the sequences is also a sequence.
It is important to mention that our sequences are actually endless, and so it’s just impossible to do something with them. It is impossible, for example, to sum up all the members of a sequence without any reservations - no one can sit and sum up for an infinite time.
An infinite sequence is where for any member number we can tell how to calculate it in a finite time. In the case of a sequence we kind of say: take the day, take the day, and then slowly, step by step, count the following days until you reach the one you need.
For the sum of the sequences, we say: if you need the 100th member of the sum, then take the 100th member from the sum of the sequences and add them. You need the millionth - just take the millionth there, take the millionth here, add these numbers and get what you need.
This is similar to how polynomials sum up, for example:
.
You can even imagine an infinite polynomial
Where - just some characters. And if we add two such "infinite members", then their sum will be just the sum of the sequences (this is how we determined the sum of the sequences).
Here it is important to understand that in reality there are no infinite polynomials, it is just a different but more convenient record of an infinite sequence: we write and see .
Now we come up with multiplication for sequences. Just pairwise multiply the terms as well as with addition will not go - although it will be like multiplication, it will not give interesting properties. Yes, and I want the multiplication to be similar to the multiplication of polynomials.
For example, ordinary finite polynomials multiply like this:
Accepted and multiplied: brackets are revealed, and members with the same degree of xx are grouped
In other words, each member of the first polynomial is multiplied by each member of the second. Each with each.
So simply, it’s impossible to generalize to infinite sequences - again, because no one can multiply an infinite number of times by an infinite number of elements. It is important to say correctly here, namely, that let the n-th member of the product of sequences be
Why so
In fact, if you are wondering what is in the 100th place, i.e. what coefficient then you can quite say: can happen if multiplied . Also can happen if multiplied . You can figure out and bring the whole 101 pair of members, which, when multiplied, will give . And after that mention no more in the product can not work.
Now we can add infinite sequences and we can also multiply them. You may notice some good properties, for example, that ; ; . We have invented this addition and multiplication, it is necessary to check that such properties are fulfilled (this is “group” and “ring”). It is easy to check, and, in general, all these properties are quite natural, especially for those who at least somewhat add up and multiply the final polynomials.
Now let's try counting for example. . According to the definition of multiplication we get
It looks logical - multiplying a sequence by a number will multiply each term by that number. And it fits with
Important note: where respectively
Now calculate . Also according to the definition of multiplication we get
Those.
Which, in fact, also converges with
We also get that .
These are basic operations on our sequence, and now it would be nice to collect all this and apply it to the initial recurrent formula. :
From the first line we subtract the second and add the third:
Total we get (using good properties of addition and multiplication):
which, I will remind once again, in fact
This is the equation, and we will solve it. We can not take it so easy and divide by , if only because we did not define a division operation. Instead, we will multiply both sides of the equation by something that seems to be , and ultimately leave the lonely F to the left. Currently, from what we have, we cannot reliably say whether such a thing exists at all. (although this is how it exists, and we will do it now).
Why can you multiply both sides of an equation for something?
If we have any and and even though such that then
But this absolutely does not mean that if then . For example, from the fact that does not follow that . To be true
need to have such an element , what . Then you can get that
Let's slightly transform our equation:
This, again, not just like that, is because the sequence multiplied by and multiplied by gives as a result Now it's a little easier, now it would be good to find such $ inline $ "(1-x) ^ {- 1}" $ inline $ and $ inline $ "(1/2-x) ^ {- 1}" $ inline $ , and multiply our equation on them. There are such reverse sequences, and here is their formula.
How can I get it
To begin with, let's try on a simple case, with :
It's clear that , here without options. Now let's see what can happen with the 1st degree x: (I just multiply by component the first sequence by the second). It's clear that . Just further we get that . For the general case act in a similar way.
Thus, we multiply both sides of the equation:
Let's take a closer look at
By definition of the product, we get the formula for the nth term:
Where did the last step come from
This is the usual sum of the members of a finite geometric progression. You can quickly get this: Let be . Then . In the same time, Consequently Total equating we get .
Substitute the resulting work
When multiplied by all members multiplied by 10 and moved 1 to the right. Total we get the result
It is this formula that should be used to complete the quest, and it can be found in the tips in the quest. For example, on the 32nd day we get . Looking at the formula, we can say that dolbogryz hryakuplyuhs are not a hindrance - they, even when eaten, have time to multiply exponentially.
In the same way, you can derive a formula for the nth term of the Fibonacci sequence, although there numbers will be more terrible - with roots, and for any n, the formula will give a natural number.
A little thought about the complexity of the algorithm
If we implement an algorithm for calculating hryakoplyukhov according to a recurrent definition (it is not necessary to use recursion), then the complexity is O (n). But if by the formula, then you can argue whether it will be O (n) or O (1). Depends how we think 2 ** n is O (1) or O (n). On the one hand, this is a bit shift, on the other hand, when n is larger than the processor's width, it will take more time to calculate. For example, if you make Ethereum a contract that will count these hryakoplyuhs, and see what happens in terms of gas consumption depending on the algorithm:
Code on solidity
pragma solidity ^0.4.0; contract Rangers { /* 10 -> 1335 gas 20 -> 2385 gas 30 -> 3435 gas 40 -> 4485 gas 50 -> 5535 gas 60 -> 6585 gas 70 -> 7635 gas 80 -> 8685 gas 90 -> 9735 gas 100 -> 10785 gas */function byStepByStep(uint n) public pure returns (uint) { if (n == 0) { return0; } if (n == 1) { return10; } uint result = 10; uint prev1 = 10; uint prev2 = 0; for (uint i = 2; i <= n; i++) { result = 3*prev1 - 2*prev2; prev2 = prev1; prev1 = result; } return result; } /* 10 -> 310 gas 100 -> 310 gas 200 -> 310 gas */function byFormula(uint n) public pure returns (uint) { return10*(2 ** n - 1); } }
It is quite predictable that the steps will be O (n), and the formula will be the real O (1).