modulo

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

北慕城南 提交于 2019-12-06 19:50:33
问题 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 modulus between two TimeSpans may seem a little weird, so allow me to explain - Say we have TimeSpan A = 1 Minute 30 Seconds TimeSpan B = 20 Seconds Here would be a

How to return the next letter in the alphabet of given letter wrapped around in Haskell [closed]

对着背影说爱祢 提交于 2019-12-06 16:06:16
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I'm trying to implement a function that returns the next letter in alphabetical order. For example: > returnNext 'A' 'B' But also: > returnNext 'Z' 'A' The function should thus cycle between char codes in alphabetical order (mod 26). Two ways come to mind import Data.Char returnNext c = chr (((1 + ord c - ord 'A') `mod` 26) + ord 'A') Which is kind of ugly to say the least. And: returnNext 'Z' = 'A' returnNext c

When taking modulo of negative numbers makes sense?

与世无争的帅哥 提交于 2019-12-06 08:10:15
I'm curious because we found a bug in our code written 2 years ago. We initialized a 16 bit signed integer with 0x8000 (the platform is of course uses 2's complement representation for negative numbers). In a hardly reproducible special case, modulo by 10 was being performed on this variable to extract individual digits in decimal representation while the value is still 0x8000 (-32768). -32768 % 10 == 248 which makes no sense for our application. Our platform is OKI 411 micro-controller. I'm curious, though taking modulo of negative number in this case makes no sense, is there any real life

C/C++ Large number calculation

此生再无相见时 提交于 2019-12-06 06:58:26
问题 I'm trying to compute the following number in a C program : result = (3 * pow(2,500000000) - 2 ) % 1000000000 The power of 2 is way to large to be handled properly => I'm under the impression I could split the calculation in many steps using the modulo to reduce the result size. Does someone has a strategy for doing so ? Any other idea ? Thanx in advance Manu 回答1: The simplest method is exponentiation by repeated squaring reducing by the modulus in each step. unsigned long long mod_pow

Base Conversion Problem

末鹿安然 提交于 2019-12-06 04:34:49
I'm trying to convert an integer to a string right now, and I'm having a problem. I've gotten the code written and working for the most part, but it has a small flaw when carrying to the next place. It's hard to describe, so I'll give you an example. Using base 26 with a character set consisting of the lowercase alphabet: 0 = "a" 1 = "b" 2 = "c" ... 25 = "z" 26 = "ba" (This should equal "aa") It seems to skip the character at the zero place in the character set in certain situations. The thing that's confusing me is I see nothing wrong with my code. I've been working on this for too long now,

How can I add a class to every 4th - 1 element?

◇◆丶佛笑我妖孽 提交于 2019-12-05 19:32:51
Don't ask why but I need to add class zebra to the <li> elements with the content next to them. This is as far as I've got, but I'm not sure what calculation to use: $("li").each(function(index){ if(index % ??? == 0) { // <-- not sure what to put here } }); <ul> <li></li> <li></li> <li></li> <!-- add the zebra class here --> <li></li> <li></li> <li></li> <li></li> <!-- add the zebra class here --> <li></li> <li></li> <li></li> <li></li> <!-- add the zebra class here --> <li></li> </ul> Can anyone help? The :nth-child() selector can accept an equation, and it solves your problem perfectly: $(

Sorting arrays in Java

邮差的信 提交于 2019-12-05 13:57:55
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 all the rest numbers (those which divide by 4 with the remainder 3) will appear. (The order of the

How do I correctly use the mod operator in MIPS?

☆樱花仙子☆ 提交于 2019-12-05 12:57:25
In MIPS, I am confused on how to get the mod to work. Below is the code I have come up with thus far. I may have more errors besides the mod, but I feel those errors are a result of the mod misunderstanding. All I'm trying to do is to get the working code (python) here: i = 1 k = 0 while i < 9: if i % 2 != 0: k = k + i i += 1 print(k) to be correctly translated into MIPS. This is my first shot at assembly, so there may be more than mod errors that are tripping me up in the code below: # Takes the odd integers from 1 to 9, adds them, # and spits out the result. # main/driver starts here .globl

Is the behavior of % with negative operands defined in Perl 5?

随声附和 提交于 2019-12-05 10:47:28
Until recently (i.e. C99), the behavior of the modulo operator was implementation defined in C. Since Perl 5 is written in C, is it reliant on the behavior of the C compiler used to build it? No, Perl 5 defines the modulo operator in perlop and even has tests to ensure it works as documented. from perl/t/op/arith.t tryeq $T++, 13 % 4, 1; tryeq $T++, -13 % 4, 3; tryeq $T++, 13 % -4, -3; tryeq $T++, -13 % -4, -1; However, if you use the integer pragma, you are at the mercies of the C compiler. Perl implements its own modulo operator, but you can get to the one from your C compiler by using the

Java inverse modulo 2**64

谁都会走 提交于 2019-12-05 10:41:04
Given an odd long x , I'm looking for long y such that their product modulo 2**64 (i.e., using the normal overflowing arithmetic) equals to 1. To make clear what I mean: This could be computed in a few thousand year this way: for (long y=1; ; y+=2) { if (x*y == 1) return y; } I know that this can be solved quickly using the extended Euclidean algorithm, but it requires the ability to represent all the involved numbers (ranging up to 2**64 , so even unsigned arithmetic wouldn't help). Using BigInteger would surely help, but I wonder if there's a simpler way, possibly using the extended