Questions tagged [modulus]

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

847 questions
8
votes
3 answers

Insert tr after every third loop

I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I…
user1169875
  • 141
  • 1
  • 3
  • 9
8
votes
1 answer

Why is 4 % -8 equal to 4?

In Java, 4 % -8 is giving 4 as an output and not -4 as expected by me.
Sourav
  • 139
  • 1
  • 1
  • 9
8
votes
3 answers

Using mod operator in iOS app

I have an NSTimeInterval that is stored as a double. I would like to get the amount of minutes that are inide of the second value using the % operator. minutes = secondValue % 60; where minutes is declared as double minutes The result is that XCode…
Jerry
  • 537
  • 1
  • 9
  • 16
8
votes
2 answers

Modulus Query in Django using F()

I want to filter for Django objects such that their "id modulo K == N" . Here's a way to do it in python, but I want it in the filter(): for foo in Foo.objects.all(): if foo.id % K == N: print foo So, I can use extra() with a query like…
Jeff Hoye
  • 580
  • 5
  • 11
8
votes
7 answers

Need an efficient subtraction algorithm modulo a number

For given numbers x,y and n, I would like to calculate x-y mod n in C. Look at this example: int substract_modulu(int x, int y, int n) { return (x-y) % n; } As long as x>y, we are fine. In the other case, however, the modulu operation is…
Johannes
  • 2,901
  • 5
  • 30
  • 50
7
votes
3 answers

How does modulus work and why is it different in Python than most languages?

Below is some code in C++. If you try something like -2%5 in python the result is positive 3 while many other languages like c++ C# (code) and flash give -2 Why do they give -2 and is one version more correct than the other? #include int…
user34537
7
votes
3 answers

Is it possible to get both the modulus and quotient of division in a single operation in C++?

I hear that when the processor does / or %, it will perform the same operation but one way it returns the quotient, the other way the remainder. Is it possible to get both in a single operation? Maybe if I throw in a snippet of assembly code (which…
mczarnek
  • 1,305
  • 2
  • 11
  • 24
7
votes
1 answer

Continuous Subarray sum

I came across this problem on Leetcode I saw the solution but I am unable to understand why it works. What property of modulus does it apply? How can we say that we have found a subarray with sum equal to k just by looking at the previous occurence…
7
votes
7 answers

Finding Primes with Modulo in Python

I have been sweating over this piece of code -- which returns all the primes in a list: primes = range(2, 20) for i in range(2, 8): primes = filter(lambda x: x == i or x % i, primes) print primes It works... but I don't understand the role…
fra
  • 205
  • 1
  • 3
  • 7
7
votes
1 answer

How to use spiderable with a meteor app hosted on modulus.io

I'm trying to make spiderable works on my meteor app hosted on modulus with SSL. I have Meteor 1.0, iron:router 1.0, spiderable and node package of phantomjs All is working on localhost. But once I deploy on Modulus, first I had the…
Jean-Noël
  • 141
  • 6
7
votes
4 answers

Add div after every 3d element twig loop, modulus

Hi so i have this twig loop, and what i need to to is after every 3rd element to add some html to close div row and open new one. I tried with various snippets from this website but no luck with any one of them
{% for date,…
Alex
  • 374
  • 1
  • 4
  • 13
7
votes
4 answers

What is the best function or method for computing modulus (Remainder of $a divided by $b) in PHP?

PHP provides different functions for computing modulus: Modulus operator %: echo ($a % $b); fmod() bcmod() gmp_mod() Which function should be used that considered safe and efficient? considering the (devision by zero issue that described here:…
user1646111
7
votes
1 answer

Mod Syntax in Clojure

How do I write modulus syntax in programming language clojure? For example the symbols money %= money_value.
user1585646
  • 421
  • 1
  • 4
  • 16
7
votes
5 answers

how to calculate reverse modulus

now I have one formula: int a = 53, x = 53, length = 62, result; result = (a + x) % length; but how to calculate reverse modulus to get the smallest "x" if I known result already (53 + x) % 62 = 44 //how to get x i mean what's the formula or logic…
Ivan Li
  • 1,850
  • 4
  • 19
  • 23
6
votes
2 answers

Python Modulus Giving String Formatting Errors

I'm trying to perform the modulus of a value in python, but I'm getting errors as it's interpretting the modulus as a string formatting constant, from my knowledge. My initial guess would be to type cast this, but then it hangs. val =…
r36363
  • 589
  • 4
  • 15