Questions tagged [modulus]

The remainder of the quotient of two numbers (usually integers).

847 questions
2
votes
2 answers

Solving modular linear congruences for large numbers

I'm looking for a better algorithm than one I found on stackoverflow to handle 4096 byte numbers, i'm hitting a maximum recursion depth. Code from stackoverlow post, i copy/pasted it but lost the original link: def linear_congruence(a, b, m): if…
oppressionslayer
  • 6,942
  • 2
  • 7
  • 24
2
votes
2 answers

How does this if statement determine whether num%2 is true or false

Okay, so here is a piece of code. The function of the code is to take a value and determine whether or not it is odd or even. def isEvenOrOdd(num): return 'odd' if num % 2 else 'even' Now my question lies in why this works. The way I am…
imanidiot
  • 37
  • 4
2
votes
1 answer

Modulus Function to Avoid Integer Overflow in C++

If I have 2 int or long long variables, call them a and b, and I want to compute the sum (a + b) mod p, where p is a large prime integer, how can I utilize the modulo operator in C++ to achieve the desired result? I have tried (a + b) % p, but this…
coder_jam
  • 94
  • 12
2
votes
1 answer

Why is this modular arithmetic incorrect in my rotate string function?

I am writing a rotate string function in C. The intended behavior is that the strings chars will be rotated using a modulus of the string length, and a key. The function seems to work in the forward direction, but if I specify a negative key, I want…
the_endian
  • 2,259
  • 1
  • 24
  • 49
2
votes
1 answer

How does the modulus operator handle strings in Javascript

I know how modulus works in general, but it is not clear to me how the operator handles strings. Recently, I had to write a script which checks if a name (string) contains an even number of letters. This actually worked, using modulus 2 and checking…
0x4e696b
  • 23
  • 5
2
votes
4 answers

JavaScript - Wrap every 3 items from an array to a div

I have an array consists 50 items. I want to wrap every 3 items to a
. I'm cracking my head trying to achieve this but I can't get it to work var cards = []; for(var i=0; i < 50; i++){ cards.push('
jenna_3108
  • 437
  • 1
  • 7
  • 20
2
votes
3 answers

Complete loss of accuracy in modulus when calculating with very large numbers

I have the following problem: > 1e20 %% 3 [1] 0 Warning message: probable complete loss of accuracy in modulus The result can't be correct and I'm sure it is because 1e20 is really big. But I want to solve calculations like this in R. Is there a…
TobiSonne
  • 1,044
  • 7
  • 22
2
votes
1 answer

Sawtooth wave Function for integers Javascript

Saby people, I'm trying to write the function which will map this --> ...-5|-4|-3|-2|-1| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9... to this --> ... 3| 0| 1| 2| 3| 0| 1| 2| 3| 0| 1| 2| 3| 0| 1... Ideally if I enter myFunc(-1,4) it will return 3 the second…
Manvel A.
  • 33
  • 5
2
votes
2 answers

How do you form the opposite statement of an 'any' condition?

I have the following code which works [i for i in range(1, 16) if any(i % j == 0 for j in [3,5])] This has an output of [3, 5, 6, 9, 10, 12, 15], the numbers that can be divided by either 3 or 5. However, when I try [i for i in range(1, 16) if any(i…
Thanos Dodd
  • 572
  • 1
  • 4
  • 14
2
votes
2 answers

Difference between modulus and remainder

I am doing some calculations with % operator in java and python. While doing calculations, i found out that % operator works differently in both languages when dealing with negative numbers. For example -21 % 4 = -1 // Java -21 % 4 = 3 #…
user7500403
2
votes
2 answers

How to read then parse with split and write into a text file?

I'm struggling to get readline() and split() to work together as I was expecting. Im trying to use .split(')') to cut down some data from a text file and write some of that data to a next text file. I have tried writing everything from the line. I…
HYUTS
  • 97
  • 2
  • 12
2
votes
2 answers

How to get Modulo of a String in Pyspark

I'd like to calculate for an alphanumeric DataFrame column the relative modulo. In pure Python I could do something like int(str, base) to convert it to a numeric value. Then simply applying the modulo %. For example: >>>…
Vzzarr
  • 4,600
  • 2
  • 43
  • 80
2
votes
1 answer

Understanding Mod Operator in Math vs Programming

From what I understand, in mathematics, the mod operator is the result of the remainder of Euclidean division. Where 0 ≤ r < |b|, meaning that the result will always be positive. In programming however, there are operators in many languages which…
csguy
  • 1,354
  • 2
  • 17
  • 37
2
votes
1 answer

Adding integers to an ArrayList depending on the modulus of their indexes

import java.util.*; public class AddingIntegers{ public static void main(String []args){ ArrayList list = new ArrayList<>(Arrays.asList(10,20,30)); add(list); } public static void add(ArrayList list)…
2
votes
2 answers

How to find A%B for A and B are very large numbers (stored in string)

If I have two numbers A and B and I want to compute A%B for A and B are very large (as large as 10^100), both are stored in strings, how can I achieve that?