0

I have a list v in sageMath worksheet like this

v = [3, 5, 22, 35, 230, 308, 683, 6546]
m = 14002
a = 185

and then I do modulus calculations on every number in the list

for i in range(0, len(v)):
    v[i] = mod(v[i] * a, m)

my problem is when I later want to do other calculations with the list the mod() function is somehow still remembered by the list. I have tried to copy the list to another list, it doesn't work.

For example, this loop below returns 6714 but should return 20716 and 20716 % 14002 = 6714

for i in range(len(v)):
    c = c + v[i]

I solved the problem by doing one more modulu calculation on v like

for i in range(len(v)):
    v[i] = mod(v[i], next_prime(m*m))

My question is if there are any better way to solve the problem

Mallom
  • 47
  • 1
  • 6
  • Possible duplicate of [How to clone or copy a list?](http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – Christian Dean Mar 01 '17 at 13:05
  • How does your solution solve the problem? It's a different calculation than above I assume, except if next_prime takes the root of m*m. What do you mean by "the mod() function is somehow still remembered by the list". Try to express clearly what you want. Also try to provide an example with the necessary imports (where does mod and next_prime come from?), don't make people trying to help you guess everything. – CodeMonkey Mar 01 '17 at 13:05
  • 2
    @CodeMonkey My guess is that he was trying to copy list `v` by doing `x = v` where `x` is the variable to copy to. And, as many seasoned pythonist know, this doesn't work. – Christian Dean Mar 01 '17 at 13:10
  • @leaf I'm not sure what he's doing. I'm trying to give him constructive criticism so he can pose better questions in the future. – CodeMonkey Mar 01 '17 at 13:20
  • 1
    If you don't want the numbers in `v` to change why are you changing them? Instead, you should create a new list that contains the results of your modulus calculations. A good way to do that is to use a list comprehension. – PM 2Ring Mar 01 '17 at 14:22

1 Answers1

1

I understand your question now. What happens when you use mod() is that it automatically makes an element of the integers modulo n, not just any old integer. So you can never get out of that. If you only truly want the remainder, you could use the builtin Python %:

v = [3, 5, 22, 35, 230, 308, 683, 6546]
m = 14002
a = 185
for i in range(0, len(v)):
    v[i] = v[i] * a % m

for i in range(len(v)):
    print v[i]*m

which yields a list starting with 7771110. The types will still (correctly) be sage.rings.integer.Integer.

kcrisman
  • 4,374
  • 20
  • 41