# Figure out this algorithm by analyzing the sequence of numbers generated

You are given a function f that, using an algorithm (that uses an old method), performs a calculation on four distinct values _ and generates a sequence of ten numbers.

Now, your task is to figure out how the algorithm works based on the outputs received for a set of inputs.

Inputs and Outputs:

f([1, 1, 1, 1])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([2, 2, 2, 2])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([3, 3, 3, 3])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([4, 4, 4, 4])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([5, 5, 5, 5])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([6, 6, 6, 6])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([8, 8, 8, 8])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([7, 7, 7, 7])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([9, 9, 9, 9])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([1, 2, 3, 6])=[5, 0, 1, 2, 3, 4, 5, 0, 1, 2]
f([2, 3, 6, 5])=[2, 4, 1, 3, 0, 2, 4, 1, 3, 0]
f([4, 1, 7, 9])=[2, 6, 1, 5, 0, 4, 8, 3, 7, 2]
f([5, 2, 6, 7])=[2, 0, 5, 3, 1, 6, 4, 2, 0, 5]
f([8, 1, 3, 2])=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
f([4, 6, 5, 8])=[5, 1, 5, 1, 5, 1, 5, 1, 5, 1]
f([1, 7, 9, 44])=[16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
f([1, 6, 47, 2])=[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
f([45, 87, 95, 68])=[66, 43, 20, 65, 42, 19, 64, 41, 18, 63]
f([335, 66, 48, 97])=[42, 86, 33, 77, 24, 68, 15, 59, 6, 50]
f([48, 796, 548, 1])=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
f([8794, 35, 3, 3])=[2, 0, 1, 2, 0, 1, 2, 0, 1, 2]
f([89, 554, 665, 7])=[5, 3, 1, 6, 4, 2, 0, 5, 3, 1]
f([54, 89, 32, 67])=[14, 1, 55, 42, 29, 16, 3, 57, 44, 31]
f([21, 65, 78, 49])=[22, 43, 15, 36, 8, 29, 1, 22, 43, 15]
f([3, 5, 11, 9])=[8, 2, 5, 8, 2, 5, 8, 2, 5, 8]
f([37, 42, 13, 128])=[31, 68, 105, 14, 51, 88, 125, 34, 71, 108]
f([48, 76, 2, 34])=[12, 26, 6, 20, 0, 14, 28, 8, 22, 2]
f([94, 57, 62, 111])=[92, 75, 58, 41, 24, 7, 101, 84, 67, 50]
f([111, 222, 333, 664])=[407, 518, 629, 76, 187, 298, 409, 520, 631, 78]
f([795, 1, 45, 3333])=[840, 1635, 2430, 3225, 687, 1482, 2277, 3072, 534, 1329]
f([897, 4, 6, 2])=[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
f([68, 749, 44, 88])=[24, 4, 72, 52, 32, 12, 80, 60, 40, 20]
f([78946, 6656, 4, 10])=[0, 6, 2, 8, 4, 0, 6, 2, 8, 4]
f([8, 1, 222, 77])=[76, 7, 15, 23, 31, 39, 47, 55, 63, 71]
f([1, 2, 3, 99999])=[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

• Could you potentially edit in a more descriptive title? – Aza Apr 9 '15 at 17:07
• @Emrakul♦ i have edited it. Thank u for pointing it for me. I hope its better now. And more info cant be provided in the title than this or else they will be spoilers. Thanx again =) – Switch Apr 9 '15 at 17:30
• That makes sense. Thanks for the edit! – Aza Apr 9 '15 at 17:33

Let's call $A,B,C,D$ the parameters of the function. The first term of the sequence is

$((A\times B)+C)$mod $D$

Each consequent term is obtained from the previous this way:

Add $A$ then modulo $D$

The $n$th term of the sequence can be expressed as:

$(A\times (B+n)+C)mod\ D$

If you need a proof of the equivalence of the two definitions, just ask in the comments.

Example:

f([89, 554, 665, 7])=[5, 3, 1, 6, 4, 2, 0, 5, 3, 1]


The first term is

$(89\times 554+665) mod\ 7 =5$

The second is

$(5+89)mod\ 7=3$

And the $n$th is

$(89\times (554+n)+665)mod\ 7$

• I think you can simplify by saying all terms are ((A x (B + n)) + C) mod D, where n is the position in the series (0-indexed). – Set Big O Apr 9 '15 at 15:21
• Thanks, it can easily proven that the two formula are equivalent, but your is easier. I will edit asap – leoll2 Apr 9 '15 at 15:25

This function delivers

an arithmetic sequence.

The first element of the parameter is

the number to add on each iteration.

The second element of the parameter is

the initial value of our iterator.

The third element of the parameter is

the initial offset of the sequence.

The fourth element of the parameter is

the modulus.

The function is described in code as:

def f(x):
r = []
for i in range(x[1],x[1]+10):
r += [(x[2] + (i*x[0]))%x[3]]
return r


• The first and fourth arguments seem correct, that's what I was thinking too. The 2nd and 3rd are totally wrong, check this: f([2, 3, 6, 5])=[2, 4, 1, 3, 0, 2, 4, 1, 3, 0] – leoll2 Apr 9 '15 at 14:45