Reputation: 556
I am using Python to implement the following equation:
S[n+1] = (k * S[n]) MOD m The values of the variables are: S[0] = 1111 k = 16807 m = 2147483647 I have calculate 10000 values (so S[0] to S[9999]).
I am using lists to calculate and append the values. This is what I am doing:
# Respective values of k, m and Seed value S[0] in sList
k = 16807
m = 2147483647
sList = [1111]
# Populating the list/array for 10000 iterations
while len(sList) < 10000:
Sn = (k * sList[-1]) % m
sList.append(Sn)
print (sList)
While the code works perfectly for smaller values of k and m, I am getting incorrect results S[2] onwards. I manually calculated S[2]=1417880224, however my code is generating S[2] = 297389177.
Is this happening because the numbers I am trying to append in the list is going beyond a certain range? Do I need to type cast to a different data type (like double)? Where am I going wrong?
Upvotes: 0
Views: 52
Reputation: 19825
Let's do it by hand using your definitions with a calculator:
>>> 1111
1111
>>> (16807 * 1111) % 2147483647
18672577
>>> (16807 * 18672577) % 2147483647
297389177
These are the same results your code produces.
So, it seems like either your hand calculations or the recursive formula you are providing is wrong.
Upvotes: 3