modulo

Efficient (cycles wise) algorithm to compute modulo 25?

Hi, I have a code in which i am computing x % 25. x always takes a positive value but its dynamic range is large. I found out that this particular code piece of computing a x % 25 is taking large cycles. I need to optimize it. Pre-computed lookup table is ruled out due to the possible large memory size of the table. As second appro...

Fast way to manually mod a number

I need to be able to calculate (a^b) % c for very large values of a and b (which individually are pushing limit and which cause overflow errors when you try to calculate a^b). For small enough numbers, using the identity (a^b)%c = (a%c)^b%c works, but if c is too large this doesn't really help. I wrote a loop to do the mod operation manu...

Weird Objective-C Mod Behavior

So I thought that negative numbers, when mod'ed should be put into positive space... I cant get this to happen in objective-c I expect this: -1 % 3 = 2 0 % 3 = 0 1 % 3 = 1 2 % 3 = 2 But get this -1 % 3 = -1 0 % 3 = 0 1 % 3 = 1 2 % 3 = 2 Why is this and is there a workaround? ...

Printing python modulus operator as it is over command line

I want to print modulus operator as it is over the command line: E.g this is how the output should look like: 1%2 2%4 or 30% 40% I am using the print statement like this: print 'computing %s % %s' % (num1, num2) Its throwing the default error: TypeError: not all arguments converted during string formatting For now I am ...

Mod of negative number is melting my brain!

Duplicate of Weird Objective-C Mod Behavior I'm trying to mod an integer to get an array position so that it will loop round. Doing i % arrayLength works fine for positive numbers but for negative numbers it all goes wrong. so i need an implementation of int GetArrayIndex(int i, int arrayLength) such that GetArrayIndex(-4, 3) == 2...

Even and odd number

To check for odd and even integer, is the lowest bit checking more efficient than using the modulo? >>> def isodd(num): return num & 1 and True or False >>> isodd(10) False >>> isodd(9) True ...

What does it mean? expr1 = expr2 Mod expr3 = 0

So I am porting a VBA application to PHP and ran into this wonderful little nugget of code: expr1 = expr2 Mod expr3 = 0 I thought it was behaving like a ternary operator but when I broke it down to simple if then statements the outcome was not as expected. So I ask the brilliant stack**overflow** community to help me out and put it in...

f# whole number float modulus 1.0 = 1.0?

Ok I have two functions the first of which looks like so: let dlth x = float (x.ToString().Length) which takes a float and returns the number of digits, that part works fine. The second function looks like this: let droot x = ((x ** (1./(dlth x))) % 1.) which takes a float and raises it to a power equal to 1.0/(number of digits), ...

How to make a modulo operation in objective-c / cocoa touch?

I have two CGFloat values, and want to calculate the modulo result. Or in other words: I want to know what's left if valueA is placed as much as possible into valueB. So I just tried: CGFloat moduloResult = valueB % valueA; the compiler complains about the % and tells me: "invalid operands to binary %". Any idea? ...

How can I achieve a modulus operation with System.TimeSpan values, without looping?

I'm in a very performance-sensitive portion of my code (C#/WPF), and I need to perform a modulus operation between two System.TimeSpan values in the quickest way possible. This code will be running thousands of times per second, and I would very much prefer to avoid using a manual loop calculation - at all costs. The idea of a modu...

In ruby, why is "100.7".to_f.modulo(1) = 0.700000000000003?

This is very strange to me: irb(main):012:0> "100.7".to_f.modulo(1) => 0.700000000000003 Why the 3 at the end? irb(main):019:0> "10.7".to_f.modulo(1) => 0.699999999999999 Same thing here...we are only getting the remainder of this value divided by one. It should be exact. ...

Why does 2 mod 4 = 2?

I'm embarrassed to ask such a simple question. My term does not start for two more weeks so I can't ask a professor, and the suspense would kill me. 2/4 = .5 so why does 2 mod 4 = 2? Sorry if I'm missing something obvious. Thanks! ...

How do I combine n and e to create the public key in RSA?

Hello :) I have a 128 byte (1024 bit) modulus (in a byte array format) and my exponent (also in a byte array format). I need to create a 128 bytes byte array representing the public key. According to Wikipedia, "The public key consists of the modulus n and the public (or encryption) exponent e." But that doesn't tell me how to mix both...

Fastest modular exponentiation in JavaScript

My problem is to compute (g^x) mod p quickly in JavaScript, where ^ is exponentiation, mod is the modulo operation. All inputs are nonnegative integers, x has about 256 bits, and p is a prime number of 2048 bits, and g may have up to 2048 bits. Most of the software I've found that can do this in JavaScript seems to use the JavaScript Bi...

How to calculate "modular multiplicative inverse" when the denominator is not co-prime with m?

I need to calculate (a/b) mod m where a and b are very large numbers. What I am trying to do is to calculate (a mod m) * (x mod m), where x is the modular inverse of b. I tried using Extended Euclidean algorithm, but what to do when b and m are not co-prime? It is specifically mentioned that b and m need to be co-prime. I tried using ...

simple question: How does a modulo operation work when the first number is smaller?

I'm messing with the modulo operation in python and I understand that it will spit back what the remainder is. But what if the first number is smaller than the second? for instance 2 % 5 the answer is 2. How does that work? 2/5 = .4! ...

long/large numbers and modulus in .NET

Hello, I'm currently writing a quick custom encoding method where I take a stamp a key with a number to verify that it is a valid key. Basically I was taking whatever number that comes out of the encoding and multiplying it by a key. I would then multiply those numbers to the deploy to the user/customer who purchases the key. I wanted...

Does (!(i % j)) mean not modulus of i and j = 0?

int main() { int i,j; for (i=1; i<=25; i++) { for (j=2; j<= i/2; j++) if (!(i%j)) break; if (j>i/2) cout << i << "\n"; } return 0; } This program (not written by me) outputs the prime numbers from 1 to 25, including 1 even though 1 isnt prime. I am having trouble with this line: if (!(i%j)) break; Does this say "not modu...

How to analyze evenness/oddness of numbers in Java

I have to write a program which reads in 3 numbers (using input boxes), and depending on their values it should write one of these messages: All 3 numbers are odd OR All 3 numbers are even OR 2 numbers are odd and 1 is even OR 1 number is odd and 2 are even This is what I have so far: import javax.swing.JOptionPane; class program3 ...

Is there a way to improve the speed or efficiency of this lookup? (C/C++)

I have a function I've written to convert from a 64-bit integer to a base 62 string. Originally, I achieved this like so: char* charset = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; int charsetLength = strlen(charset); std::string integerToKey(unsigned long long input) { unsigned long long num = input; st...