modulo

How to calculate the mod of large exponents?

喜夏-厌秋 提交于 2019-12-02 08:53:04
问题 For example I want to calculate (reasonably efficiently) 2^1000003 mod 12321 And finally I want to do (2^1000003 - 3) mod 12321. Is there any feasible way to do this? 回答1: Basic modulo properties tell us that 1) a + b (mod n) is (a (mod n)) + (b (mod n)) (mod n) , so you can split the operation in two steps 2) a * b (mod n) is (a (mod n)) * (b (mod n)) (mod n) , so you can use modulo exponentiation (pseudocode): x = 1 for (10000003 times) { x = (x * 2) % 12321; # x will never grow beyond

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

旧城冷巷雨未停 提交于 2019-12-02 08:14:02
This question already has an answer here: Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273? 14 answers 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 right? But it shows a slightly different value, why do I keep losing precision? I mean the code should substract exactly 1

modulo operation on negative numbers [duplicate]

独自空忆成欢 提交于 2019-12-02 07:49:07
This question already has an answer here: Modulo operation with negative numbers 12 answers A modulo operation a%b returns the remainder for a/b but for negative numbers it does not do so. #include <stdio.h> int main(void) { int n=-4; printf("%d\n",n%3); return 0; } It should return 2 as 3*(-2)=-6 is just smaller than -4 and a multiple of 3 but the output is -1. Why is it treating (-a) mod b same as -(a mod b) As a general rule, the modulo and division should satisfy the equation b * (a/b) + a%b == a For positive numbers, it is obvious that this means that a%b must be a positive number. But if

How to calculate the mod of large exponents?

我与影子孤独终老i 提交于 2019-12-02 03:34:05
For example I want to calculate (reasonably efficiently) 2^1000003 mod 12321 And finally I want to do (2^1000003 - 3) mod 12321. Is there any feasible way to do this? Basic modulo properties tell us that 1) a + b (mod n) is (a (mod n)) + (b (mod n)) (mod n) , so you can split the operation in two steps 2) a * b (mod n) is (a (mod n)) * (b (mod n)) (mod n) , so you can use modulo exponentiation (pseudocode): x = 1 for (10000003 times) { x = (x * 2) % 12321; # x will never grow beyond 12320 } Of course, you shouldn't do 10000003 iterations, just remember that 2 1000003 = 2 * 2 1000002 , and 2

Modulus PHP Problem

爷,独闯天下 提交于 2019-12-01 23:30:24
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 integer, but its not. So I do not understand why that if() evaluates to true? Using PHP 5.2.8, it fails as

Remainder on Float in Python [duplicate]

╄→гoц情女王★ 提交于 2019-12-01 20:55:49
This question already has an answer here: Python modulo on floats 3 answers 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 possible tolerance to determine if the remainder is next to 0. Any help? Maxime Lorant As this (long) response says, use

Why do we need IEEE 754 remainder?

非 Y 不嫁゛ 提交于 2019-12-01 18:05:00
I just read this topic (especially the last comments). Then I was wondering, why we actually need this was of giving the remainder. But it seems, that not many people "on google" were interested in that before... Simon Byrne If you're looking for reasons why you would want it, one is for what is known as "range reduction" Let's say you want sind function for computing the sine of an argument in degrees. A naive way to do this would be sind(x) = sin(x*pi/180) However pi here is not the true irrational number pi , but instead the floating point number closest to pi . This leads to things like

modulus operator to run 1st and then every 3rd item

一曲冷凌霜 提交于 2019-12-01 18:01:52
So i need it to run on the first loop and then every 3rd loop if ($k % 3 || $k==1 ) { echo '<div class="modcontainer">'; } Seems simple to me, but i don't have the understanding of modulus Modulus returns the remainder, not a boolean value. This code will resolve to true for 1, 3, 6, 9, ... if (($k % 3 == 0) || $k==1 ) { echo '<div class="modcontainer">'; } This code will resolve to true for 1, 4, 7, 10, ... if ($k % 3 == 1) { echo '<div class="modcontainer">'; } 来源: https://stackoverflow.com/questions/11261192/modulus-operator-to-run-1st-and-then-every-3rd-item

modulus operator to run 1st and then every 3rd item

空扰寡人 提交于 2019-12-01 17:18:49
问题 So i need it to run on the first loop and then every 3rd loop if ($k % 3 || $k==1 ) { echo '<div class="modcontainer">'; } Seems simple to me, but i don't have the understanding of modulus 回答1: Modulus returns the remainder, not a boolean value. This code will resolve to true for 1, 3, 6, 9, ... if (($k % 3 == 0) || $k==1 ) { echo '<div class="modcontainer">'; } This code will resolve to true for 1, 4, 7, 10, ... if ($k % 3 == 1) { echo '<div class="modcontainer">'; } 来源: https:/

What determines the sign of m % n for integers?

戏子无情 提交于 2019-12-01 15:33:08
问题 The modulo in Python is confusing. In Python, % operator is calculating the remainder: >>> 9 % 5 4 However: >>> -9 % 5 1 Why is the result 1 ? and not -4 ? 回答1: Because in python, the sign matches the denominator. >>> 9 % -5 -1 >>> -9 % 5 1 For an explanation of why it was implemented this way, read the blog post by Guido. 回答2: -10 % 5 is 0, ie, -10 is evenly divided by 5. You ask why -9 % 5 is not -4, and the answer is that both 1 and -4 can be correct answers, it depends on what -9 divided