modulo

Python code doesn't work like it does in C++

Python code doesn't work like it does in C++ Question: How to get the same values ​​in Python as in C++? CPP code #include <bitset> #include <iostream> using namespace std; int main() { short int divider = -32768; auto divider_bin_str = bitset<16>(divider).to_string(); cout << "divider: " << divider << " " << divider_bin_str << endl; …

Total answers: 1

how to get modulus difference of values of two json objects in python

how to get modulus difference of values of two json objects in python Question: I have two JSON arrays , I need to get the modulus difference of the JSON object keys. My array list can have 1000s of elements. How to calculate it efficiently? Is there a way to do it parallelly without using …

Total answers: 1

How can I fix a bug using modulo (%) in Python?

How can I fix a bug using modulo (%) in Python? Question: I am making a converting program in Python, and in this case it’s from feet to yards. I have having trouble with a specific line of code that is not showing an error message, yet refuses to actually work. When I run the …

Total answers: 3

Generating the sequence 0, 0, 1, 1, 0, 0, 1, 1, … in Python

Generating the sequence 0, 0, 1, 1, 0, 0, 1, 1, … in Python Question: Using % 2 gives me the alternating sequence [0, 1, 0, 1, …] seq = [] for i in range(10): e = i % 2 seq.append(e) Is there a way to generate the sequence [0, 0, 1, 1, 0, 0, …

Total answers: 1

Inconsistent modulus result in python

Inconsistent modulus result in python Question: I’ve got a simple list "passage" that contains strings. Goal is to join these split words into lines of 5 words each. Using modulus 5 (against index value + 1 to simplify) as test of when to add a newline character. For reasons that escape me, it works fine, …

Total answers: 1

How do I find Legendre's Symbol?

How do I find Legendre's Symbol? Question: Given a 1024 bit modulo and several long integers, I wanted to find out which of these values is a quadratic residue. The Legendre Symbol is supposed to return -1,0, or 1 and yet my code returns values several orders of magnitude larger than these. p = 101524035174539890485408575671085261788758965189060164484385690801466167356667036677932998889725476582421738788500738738503134356158197247473850273565349249573867251280253564698939768700489401960767007716413932851838937641880157263936985954881657889497583485535527613578457628399173971810541670838543309159139 …

Total answers: 1

Is divmod() faster than using the % and // operators?

Is divmod() faster than using the % and // operators? Question: I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and // operators (suppose of course one needs both the quotient and the remainder)? q, …

Total answers: 3

Python modulo on floats

Python modulo on floats Question: Can anyone explain how the modulo operator works in Python? I cannot understand why 3.5 % 0.1 = 0.1. Asked By: beruic || Source Answers: It has to do with the inexact nature of floating point arithmetic. 3.5 % 0.1 gets me 0.099999999999999811, so Python is thinking that 0.1 divides …

Total answers: 3

Integer division & modulo operation with negative operands in Python

Integer division & modulo operation with negative operands in Python Question: Questions arise when I type in these expressions to Python 3.3.0 -10 // 3 # -4 -10 % 3 # 2 10 // -3 # -4 10 % -3 # -2 -10 // -3 # 3 It appears as though it takes the approximate …

Total answers: 4

Numpy matrix power/exponent with modulo?

Numpy matrix power/exponent with modulo? Question: Is it possible to use numpy’s linalg.matrix_power with a modulo so the elements don’t grow larger than a certain value? Asked By: John Smith || Source Answers: What’s wrong with the obvious approach? E.g. import numpy as np x = np.arange(100).reshape(10,10) y = np.linalg.matrix_power(x, 2) % 50 Answered By: …

Total answers: 5