modulo

Modular Exponentiation for high numbers in C++

孤人 提交于 2019-11-26 14:22:52
问题 So I've been working recently on an implementation of the Miller-Rabin primality test. I am limiting it to a scope of all 32-bit numbers, because this is a just-for-fun project that I am doing to familiarize myself with c++, and I don't want to have to work with anything 64-bits for awhile. An added bonus is that the algorithm is deterministic for all 32-bit numbers, so I can significantly increase efficiency because I know exactly what witnesses to test for. So for low numbers, the algorithm

Can't Mod Zero?

有些话、适合烂在心里 提交于 2019-11-26 13:47:00
Why is X % 0 an invalid expression? I always thought X % 0 should equal X. Since you can't divide by zero, shouldn't the answer naturally be the remainder, X (everything left over)? The C++ Standard(2003) says in §5.6/4, [...] If the second operand of / or % is zero the behavior is undefined ; [...] That is, following expressions invoke undefined-behavior(UB): X / 0; //UB X % 0; //UB Note also that -5 % 2 is NOT equal to -(5 % 2) (as Petar seems to suggest in his comment to his answer). It's implementation-defined. The spec says (§5.6/4), [...] If both operands are nonnegative then the

Check if a number is divisible by 3

☆樱花仙子☆ 提交于 2019-11-26 12:11:05
问题 I need to find whether a number is divisible by 3 without using % , / or * . The hint given was to use atoi() function. Any idea how to do it? 回答1: Subtract 3 until you either a) hit 0 - number was divisible by 3 b) get a number less than 0 - number wasn't divisible -- edited version to fix noted problems while n > 0: n -= 3 while n < 0: n += 3 return n == 0 回答2: The current answers all focus on decimal digits, when applying the "add all digits and see if that divides by 3". That trick

Mod of negative number is melting my brain

落花浮王杯 提交于 2019-11-26 12:02:34
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. 4 % 3 == 1 3 % 3 == 0 2 % 3 == 2 1 % 3 == 1 0 % 3 == 0 -1 % 3 == -1 -2 % 3 == -2 -3 % 3 == 0 -4 % 3 == -1 so i need an implementation of int GetArrayIndex(int i, int arrayLength) such that GetArrayIndex( 4, 3) == 1 GetArrayIndex( 3, 3) == 0 GetArrayIndex( 2, 3) == 2 GetArrayIndex( 1, 3) == 1 GetArrayIndex( 0, 3) == 0 GetArrayIndex(-1, 3) == 2 GetArrayIndex(-2, 3) == 1 GetArrayIndex(-3, 3) == 0 GetArrayIndex(-4, 3) ==

C: How to wrap a float to the interval [-pi, pi)

拥有回忆 提交于 2019-11-26 11:58:03
问题 I\'m looking for some nice C code that will accomplish effectively: while (deltaPhase >= M_PI) deltaPhase -= M_TWOPI; while (deltaPhase < -M_PI) deltaPhase += M_TWOPI; What are my options? 回答1: Edit Apr 19, 2013: Modulo function updated to handle boundary cases as noted by aka.nice and arr_sea: static const double _PI= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348; static const double _TWO_PI= 6

Can&#39;t use modulus on doubles?

戏子无情 提交于 2019-11-26 11:44:16
I have a program in C++ (compiled using g++). I'm trying to apply two doubles as operands to the modulus function, but I get the following error: error: invalid operands of types 'double' and 'double' to binary 'operator%' Here's the code: int main() { double x = 6.3; double y = 2; double z = x % y; } Mysticial The % operator is for integers. You're looking for the fmod() function . #include <cmath> int main() { double x = 6.3; double y = 2.0; double z = std::fmod(x,y); } fmod(x, y) is the function you use. Use fmod() from <cmath> . If you do not want to include the C header file: template

How to calculate modulus of large numbers?

强颜欢笑 提交于 2019-11-26 11:05:44
How to calculate modulus of 5^55 modulus 221 without much use of calculator? I guess there are some simple principles in number theory in cryptography to calculate such things. jason Okay, so you want to calculate a^b mod m . First we'll take a naive approach and then see how we can refine it. First, reduce a mod m . That means, find a number a1 so that 0 <= a1 < m and a = a1 mod m . Then repeatedly in a loop multiply by a1 and reduce again mod m . Thus, in pseudocode: a1 = a reduced mod m p = 1 for(int i = 1; i <= b; i++) { p *= a1 p = p reduced mod m } By doing this, we avoid numbers larger

Recognizing when to use the modulus operator

廉价感情. 提交于 2019-11-26 10:09:28
问题 I know the modulus (%) operator calculates the remainder of a division. How can I identify a situation where I would need to use the modulus operator? I know I can use the modulus operator to see whether a number is even or odd and prime or composite, but that\'s about it. I don\'t often think in terms of remainders. I\'m sure the modulus operator is useful, and I would like to learn to take advantage of it. I just have problems identifying where the modulus operator is applicable. In various

Check if a number is divisible by 3 [closed]

泪湿孤枕 提交于 2019-11-26 09:33:52
问题 Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a number divisible by 3, otherwise zero. Examples: input \"0\": (0) output 1 inputs \"1,0,0\": (4) output 0 inputs \"1,1,0,0\": (6) output 1 This is based on an interview question. I ask for a drawing of logic gates but since this is stackoverflow I\'ll accept any coding language. Bonus points for a

Assembly Language - How to Do Modulo?

牧云@^-^@ 提交于 2019-11-26 08:08:49
问题 Is there something like a modulo operator or instruction in x86 assembly? 回答1: If your modulus / divisor is a known constant, and you care about performance, see this and this. A multiplicative inverse is even possible for loop-invariant values that aren't known until runtime, e.g. see https://libdivide.com/ (But without JIT code-gen, that's less efficient than hard-coding just the steps necessary for one constant.) Never use div for known powers of 2: it's much slower than and for remainder,