Ask Your Question
0

weird result for a product of two integers

asked 2023-01-05 21:38:08 +0100

moon gravatar image

updated 2023-01-05 21:42:21 +0100

FrédéricC gravatar image

I'm facing an issue. either I'm blind and can't see something abious or there is a type issue of my variables. I set this little code to illustrate what I am trying to fix

exp="khjkmqSDVB KNÙsdhgvd elgjvaolfba barmknham:baDB?Z?dbk,are bafbmao\
lfba barmknham:baDB?Z?N NBZRLHKMZRKHNZMB;ZGFKHZRPKolfba barmknham:baDB?\
Z?HTNPZKGJolfba barmknham:baDB?Z?ZPEROYAÉALZTIYUHNGKEG\
jkmqSDVB KNÙsdhgvd elgjvaolfba barmknham:baDB?Z?dbk,are bafbmao\
lfba barmknham:baDB?Z?N NBZRLHKMZRKHNZMB;ZGFKHZRPKolfba barmknham:baDB?\
Z?HTNPZKGJolfba barmknham:baDB?Z?ZPEROYAÉALZTIYUHNGKEG\
jkmqSDVB KNÙsdhgvd elgjvaolfba barmknham:baDB?Z?dbk,are bafbmao\
lfba barmknham:baDB?Z?N NBZRLHKMZRKHNZMB;ZGFKHZRPKolfba barmknham:baDB?\
Z?HTNPZKGJolfba barmknham:baDB?Z?ZPEROYAÉALZTIYUHNGKEG"
nb = len(exp)//40
t= mod(len(exp),40)
print("length of exp = " + str(len(exp)))
print("nb = " + str(nb))
print("t = " + str(t))
print("nb x 40 = " + str(nb*40))
print("t + (40 x nb)  = " +  str(t + nb*40))
print(nb, t, nb*t)

Everything go right until the last expression

print("t + (40 x nb)  = " +  str(t + nb*40))

This is what I am getting (restarted the kernel jut-in-case)

Out[1]:
length of exp = 566
nb = 14
t = 6
nb x 40 = 560
t + (40 x nb)  = 6
14 6 4

Thx for the one who'll see what's wrong.

edit retag flag offensive close merge delete

Comments

1

to know more about the type of some variable v, in particular the ring in which it lives, use parent(v)

FrédéricC gravatar imageFrédéricC ( 2023-01-05 21:45:15 +0100 )edit
1

here t belongs to a finite ring, not to the integer ring

FrédéricC gravatar imageFrédéricC ( 2023-01-05 21:46:34 +0100 )edit

If you want an integer version, you could use t = len(exp) % 40.

John Palmieri gravatar imageJohn Palmieri ( 2023-01-05 21:54:39 +0100 )edit

thx this what I ended up doing. appreciated

moon gravatar imagemoon ( 2023-01-05 22:22:06 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2023-01-06 14:47:48 +0100

Emmanuel Charpentier gravatar image

updated 2023-01-06 14:52:46 +0100

After running your code :

sage: nb.parent()
Integer Ring
sage: t.parent()
Ring of integers modulo 40

Here's the rub ! Therefore :

sage: (t+nb*40).parent()
Ring of integers modulo 40

And, indeed :

sage: 566%40
6

But :

sage: (566%40).parent()
Integer Ring

and from mod? :

Docstring:     
   Return the equivalence class of n modulo m as an element of
   \ZZ/m\ZZ.

which explains your result...

What you wanted is probably :

sage: Integer(len(exp)).quo_rem(40)
(14, 6)

because :

sage: list(map(parent, Integer(len(exp)).quo_rem(40)))
[Integer Ring, Integer Ring]

HTH,

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-01-05 21:38:08 +0100

Seen: 227 times

Last updated: Jan 06 '23