2. For the Intermediate
In this section i will introduce 2 common types of sequence, AP and GP.
Arithmetic progression (AP)
An AP is simply a sequence of numbers in which the difference between each term is a constant.
Eg.
2,4,6,8,10,....
6,2,-2,-6,-10,....
Let's jump straight into an example:
Suppose that we want to drop all the items, a common approach would be to create a TIntegerArray and inputting the pattern to drop it [1,5,9,13,17,21,2,6,10,14,18,22,3,7,11,15,19,23,4,8 ,12,16,20,24] (column by column). This approach might be foolproof, but what if in another scenario, there are 100 rows and columns to drop? It would then be a hassle to try input the pattern again (and even prone to errors).
We should then perhaps observe the pattern:
1,5,9,13,17,21
That is in fact an AP! It's first term is 1 and the common difference between each term is 4. Based on the formula of the general term of an AP:
Tn=a + (n-1)d
where a is the first term, d is the common difference and n is the nth term (1,2,3,4,5,6...)
So we will get
1+(n-1)*4
for our first column of inventory.
We can then use a loop to drop the items:
Simba Code:
program Example;
{$i SRL/srl.simba}
procedure MouseKeyDrop;
var
i,x,y,InvSlot: Integer;
begin
for i:=1 to 6 do //if you want to do 6 rows
begin
InvSlot:=(1+(i-1)*4);
Writeln('Moving to InvSlot ' + ToStr(InvSlot));
{MouseItem(InvSlot,mouse_Right);
GetMousePos(x,y);
MoveMouse(x,y+40);
ClickMouse2(mouse_Left); }
end;
end;
begin
MouseKeyDrop;
end.
Run this and you will see how it magically generates the whole sequence
How do we deal with the other 3 columns then? Surely we don't want to create another AP?
Observe this:
1,5,9,13,17,21,...
2,6,10,14,18,22,...
The pattern is obvious now, for the next column, the slot number increases by 1, so we can simply add another loop that adds 1-3 to every InvSlot based on the column number:
Simba Code:
program Example;
{$i SRL/srl.simba}
procedure MouseKeyDrop;
var
k,i,x,y,InvSlot: Integer;
begin
for k:=0 to 3 do
for i:=1 to 6 do
begin
InvSlot:=((1+(i-1)*4)+k);
Writeln('Moving to InvSlot ' + ToStr(InvSlot));
{MouseItem(InvSlot,mouse_Right);
GetMousePos(x,y);
MoveMouse(x,y+40);
ClickMouse2(mouse_Left); }
end;
end;
begin
MouseKeyDrop;
end.
You now officially have the right to brag to YoHoJo that your mousekey procedure is cleaner than his!
Sum of an AP
The sum of an AP is given by
Sn=(n/2)(2a+(n-1)d)
OR
Sn=(n/2)(a+l)
where l is the last term of the sequence. (The other terms are same as AP)
Usually you won't know the last term so the first equation is more commonly used.
I can't think of an example for its application for RS atm, tell me if you have an idea.
Geometric progression (GP)
A GP is another common type of sequence, where the difference between each term is a ratio.
Eg.
1,2,4,8,16,32... (Multiply the previous term by 2 to get the next term)
2, 2/5, 2/25, 2/125,....(ratio=1/5)
General Term
Tn=a(pow(r,n-1))
where r is the common ratio.
Sum of a GP
Sn=(a(1-pow(r,n))) / (1-r)