Questions tagged [modulus]

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

847 questions
4
votes
2 answers

How does int(or long long) overflow in c++ affect modulus?

Suppose I have two long longs, a and b, that I need to multiply, then get the value mod k for some large k, such that a, b, and k are all in the range of long long but not of int. For simplicity, a, b < k. Thus the code would be: long long a, b,…
John Targaryen
  • 1,109
  • 1
  • 13
  • 29
4
votes
1 answer

In javascript, how do I deal with large numbers efficiently in a loop?

My instructions were "for i from 1 to n, do i % m and return the sum. You'll need to get a little clever with performance, since n can be a very large number" The program below works fine with small numbers. How can I make it efficient with large…
Wilfredo
  • 137
  • 1
  • 9
4
votes
1 answer

Sailsjs - Hook orm taking too long to load - Modulus

I have made a nodejs application using sails.js. It's working perfectly in my localhost. The problem appears in production when I try to publish it in the server(modulus). You can take a look the error below. Error: The hook `pubsub` is taking too…
gon250
  • 3,405
  • 6
  • 44
  • 75
4
votes
4 answers

c++ Greatest Common Divisor

I am very new to C++ and am attempting create a function to implement the Euclidean algorithm that returns the greatest common divisor for two integer inputs. The current approach I am taking keeps crashing - can you help me understand why? int…
David Loughnane
  • 143
  • 1
  • 3
  • 11
4
votes
2 answers

Python -- how to force enumerate to start at 1 -- or workaround?

I have a simple for loop: for index, (key, value) in enumerate(self.addArgs["khzObj"].nodes.items()): and I want to start a new wx horizontal boxsizer after every 3rd item to create a panel with 3 nodes each and going on for as many as are in…
linus72982
  • 1,418
  • 2
  • 16
  • 31
4
votes
3 answers

why (0.0006*100000)%10 is 10

When I did (0.0006*100000)%10 and (0.0003*100000)%10 in python it returned 9.999999999999993 respectively, but actually it has to be 0. Similarly in c++ fmod(0.0003*100000,10) gives the value as 10. Can someone help me out where i'm getting wrong.
lijo050
  • 233
  • 4
  • 14
4
votes
1 answer

Reformulate an equation to me faster in java

I'm working on encryption, and for the private key exponent d, you need to multiply d by e and take the mod of another number, and have the remainder be 1. the function i have already is this: private void genD() { d = e / 2; //…
PulsePanda
  • 1,806
  • 10
  • 33
  • 56
4
votes
3 answers

Add Elements evenly to List

I have a "Element" class with a "Index" property which is used for sorting a list of elements. When i'm adding elements to the list I want to spread out the new elements evenly according to existing elements in the list. This means if I have 6…
Dumpen
  • 1,622
  • 6
  • 22
  • 36
4
votes
3 answers

Divison and remainder in Prolog

Trying to figure out how to write a recursive predicate divide_by(X, D, I, R) that takes as input a positive integer X and a divisor D, and returns the answer as the whole number part I and the remainder part R, however, I can't seem to get my head…
user2326995
  • 163
  • 1
  • 11
4
votes
2 answers

C weird modulo operation

So I've got this thing where I got the following operation: NSInteger articleIndex = self.featuredArticlesCounter % self.featuredArticles.count; In this case self.featuredArticlesCounter is -1 and self.featuredArticles.count is 10 So it's basically…
Andrew
  • 3,166
  • 21
  • 32
4
votes
5 answers

How can I simplify my "equation" to switch between 3 and 5?

It is easy to "switch" between 0 and 1 in the following way: int i = 0; i = (++i) % 2; // i = 1 i = (++i) % 2; // i = 0 Similarly, I found that it is possible to "switch" between 3 and 5: int i = 3; i = (((i * 2) - 1) % 3) + 3; // i = 5 i =…
dejay
  • 758
  • 2
  • 6
  • 18
4
votes
1 answer

Remainder/Modulus of string is a number

Why is this even possible? console.log('13' % 2); 1 I assume JavaScript just converts the string on its own. I would appreciate any info on this behaviour.
Tomasz Szymanek
  • 253
  • 2
  • 5
  • 15
4
votes
2 answers

modulus returning incorrect amount in c program when a negative number is entered

Can someone please help me figure out why this program gives the wrong answer for a modulus operation when a negative number is entered in this C program? I am pretty sure what is causing the problems is the scanf function. The correct answer is…
user_loser
  • 881
  • 1
  • 13
  • 27
4
votes
2 answers

Is fmod equivalient to the % operator in HLSL

The following statement is valid HLSL: float3(300.0f,200.0f,100.0f) % 256.0f Apparently te % operator works on floating point numbers? How is that different from fmod? Is the following statement equivalent? fmod(float3(300.0f,200.0f,100.0f),…
Johan
  • 660
  • 1
  • 6
  • 13
4
votes
2 answers

How to efficiently calculate a modulus in C by hand?

I wrote a basic fixed-point variant of the sine function which utilizes a lookup table (targeting an AVR micro controller w/o FPU). My implementation also accepts negative values and values which exceed 2π, like its floating point pendant in math.h…