modulo

Sorting arrays in Java

安稳与你 提交于 2019-12-22 08:04:43
问题 Write a static method in Java: public static void sortByFour (int[] arr) That receives as a parameter an array full of non-negative numbers (zero or positive) and sorts the array in the following way: In the beginning of the array all the numbers that are divisible by four will appear. After them all the numbers in the array that divide by 4 with a remainder of 1 will appear. After them all the numbers in the array that divide by 4 with a remainder of 2 will appear. In the end of the array

Modulo of a negative number

余生长醉 提交于 2019-12-21 18:42:30
问题 Consider the following expression: (a - b) mod N Which of the following is equivalent to the above expression? 1) ((a mod N) + (-b mod N)) mod N 2) ((a mod N) - (b mod N)) mod N Also, how is (-b mod N) calculated, i.e., how is the mod of a negative number calculated? Thanks. 回答1: I don't want to bother you with some complex mathematical concepts, so i'll try to keep it simple. When we say that a = b (mod c), we simply say that a-b is a multiple of c. This means that when we want to know what

How do I do modulus in C++?

情到浓时终转凉″ 提交于 2019-12-21 17:54:23
问题 How do I perform a mod operation between two integers in C++? 回答1: Like this: x=y%z 回答2: In c++, use % operator More Help 回答3: As the other answers have stated, you can use the C++ % operator. But be aware that there's a wrinkle no one has mentioned yet: in the expression a % b , what if a is negative? Should the result of this operation be positive or negative? The C++ standard leaves this up to the implementation. So if you want to handle negative inputs portably, you should probably do

Python OpenCV cv.WaitKey spits back weird output on Ubuntu modulo 256 maps correctly

微笑、不失礼 提交于 2019-12-21 09:14:33
问题 I am running Ubuntu 11.10 (Lenovo T400) with OpenCV 2.2 (I believe as imports are done as import cv2.cv as cv). This problem also happens if i just 'import cv' instead. I recently started having this problem, and it's kind of a weird one. I don't know anything significant I did, I have restarted since it started happening. I installed a couple programs, but I don't think those would affect this. When I run with an artificial image showing (just a black image), I try to poll cv.WaitKey(10). It

Fast way to manually mod a number

泪湿孤枕 提交于 2019-12-21 04:33:29
问题 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 manually, one a at a time: private static long no_Overflow_Mod(ulong num_base, ulong num_exponent, ulong mod) { long answer = 1; for (int x = 0; x < num_exponent;

Does INT_MIN % -1 produce undefined behavior?

牧云@^-^@ 提交于 2019-12-21 03:13:20
问题 gcc generates floating code that raises SIGFPE for the following code: #include <limits.h> int x = -1; int main() { return INT_MIN % x; } However I can find no statement in the standard that this code invokes undefined or implementation-defined behavior. As far as I can tell, it's required to return 0. Is this a bug in gcc or am I missing some special exception the standard makes? 回答1: You are probably right that this can be considered as a bug in the actual standard. The current draft

What is the fastest way to get the 4 least significant bits in a byte (C++)?

丶灬走出姿态 提交于 2019-12-21 02:17:11
问题 I'm talking about this: If we have the letter 'A' which is 77 in decimal and 4D in Hex. I am looking for the fastest way to get D. I thought about two ways: Given x is a byte. x << 4; x >> 4 x %= 16 Any other ways? Which one is faster? 回答1: I always use x &= 0x0f 回答2: Brevity is nice - explanations are better :) x &= 0x0f is, of course, the right answer. It exactly expresses the intent of what you're trying to achieve, and on any sane architecture will always compile down to the minimum

Why does using modulo on non-integer values lose floating-point precision? [duplicate]

本小妞迷上赌 提交于 2019-12-20 04:34:23
问题 This question already has answers here : Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273? (14 answers) Closed 6 years ago . I am wondering why I am losing precision when using this code : double x = 12.0456; // or float : same result System.out.println(x); // outputs 12.0456 obviously x %= 1; // should now be equal to 0.0456 right? System.out.println(x); // outputs 0.04560000000000031 or 0.045599937 when using float 12.0456 modulo 1 should equal 0.0456

Modulus PHP Problem

泄露秘密 提交于 2019-12-20 02:56:40
问题 I have a problem, I am trying to calculate what the lowest prime is of a number but I do not understand the result that PHP is giving me. If I have this number $number = 600851475143; Then I modulus it: $primes = array( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97); foreach($primes as $key=>$value) { if($number % $value == 0 ) {echo $value; break; } } Why is it that $value = 3? If $value = 3, that means that 600851475143 / 3 should be an

Remainder on Float in Python [duplicate]

泪湿孤枕 提交于 2019-12-20 02:34:49
问题 This question already has answers here : Python modulo on floats (3 answers) Closed 5 years ago . I just want to show you the results of the operations in python. I cannot explain. >>> 1.0%1.0 0.0 (OK) >>> 1.0%0.1 0.09999.... >>> 1.0%0.001 0.00999.... >>> 1.0 %0.0001 0.000999... ... and so on. I need something that allows me to understand whether the remainder of 'x%y' is 0.0, namely 'y' divides 'x' exactly N times, where N is an integer. Due to the previous behavior I don't know how to set a