modulo

Recognizing when to use the mod operator

I have a quick question about the mod operator. I know what it does; it calculates the remainder of a division. My question is, how can I identify a situation where I would need to use the mod operator? I know I can use the mod operator to see whether a number is even or odd and prime or composite, but that's about it. I don't often thin...

Big problem with Dijkstra algorithm in a linked list graph implementation

Hi, I have my graph implemented with linked lists, for both vertices and edges and that is becoming an issue for the Dijkstra algorithm. As I said on a previous question, I'm converting this code that uses an adjacency matrix to work with my graph implementation. The problem is that when I find the minimum value I get an array index. T...

Implementing the modulo operator as a function in C

How can we implement the modulo operator as a function in C without using the operator? ...

If I'm using a 1d array to represent a square board, how can I take my index and check the sqaures above, below and to its sides?

If I have a 4x4 gameboard which I'm representing in my program as a 1d integer array of size 16. How can I get the indexs of the squares above, below, to the left and to the right any given index? So, for example: A = { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 } Which represents this board 0 1 2 3 4 5 6 7 8 9 10 11 ...

How Does Modulus Divison Work

I don't really understand how modulus division works. I was calculating 27 % 16 and wound up with 11 and I don't understand why. I can't seem to find an explanation in layman's terms online. Can someone elaborate on a very high level as to what's going on here? EDIT: Thanks for all your answers. You guys are incredibly quick. It all ma...

Mathematical modulus in c#

Is there a library function in c# for the mathematical modulus of a number - by this I specifically mean that a negative integer modulo a positive integer should yield a positive result. edited to provide an example: -5 modulo 3 should return 1 ...

How can I do mod without a mod operator?

This silly scripting language doesn't have a % or Mod(). I do have a Fix() that chops off the decimal part of a number. I only need positive results, so don't get too robust. ...

Better ways to implement a modulo operation (algorithm question)

I've been trying to implement a modular exponentiator recently. I'm writing the code in VHDL, but I'm looking for advice of a more algorithmic nature. The main component of the modular exponentiator is a modular multiplier which I also have to implement myself. I haven't had any problems with the multiplication algorithm- it's just ad...

jquery find if variable is divisible by 2

How do I figure out if a variable is divisible by 2? Furthermore I need do a function if it is and do a different function if it is not. ...

power and modulo on the fly for big numbers

I raise some basis b to the power p and take the modulo m of that. Let's assume b=55170 or 55172 and m=3043839241 (which happens to be the square of 55171). The linux-calculator bc gives the results (we need this for control): echo "p=5606;b=55171;m=b*b;((b-1)^p)%m;((b+1)^p)%m" | bc 2734550616 309288627 Now calculating 55170^5606 gi...

mod,prime -> inverse possible

Hi all. I was wondering if one can do the following: We have: X is a product of N-primes, thus I assume unique. C is a constant. We can assure that C is a number that is part of the N-primes or not. Whichever will work best. X mod C = Z We have Z and C and we know that X was a product of N-primes, where N is restricted lets say firs...

Sorting arrays in Java

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 di...

Modulo jquery question

Hi all, Dont ask why but I need to add class zebra to the lis with the content next to them. I do a $("li").each(function(index){ if(index%??? == 0) { } }); <ul> <li></li> <li></li> <li></li> //add here class zebra <li></li> <li></li> <li></li> <li></li> //add here class zebra <li></li> <li></li> <li></li> <li></li> //add here clas...

gcc gives error while using fmod()

Sample code for fmod #include <stdio.h> #include <math.h> int main(void) { double x = 0.14527, y = 3.14159; printf("fmod(x, y) = %.6lf\n", fmod(x, y)); return 0; } $ gcc main.c -o main I get /tmp/ccztJO01.o: In function `main': main.c:(.text+0x4d): undefined reference to `fmod' collect2: ld returned 1 ...

change a variable based on even/odd status of another variable?

for($i=0;$i<$num;$i++) { if($i==even) $hilite="hilite"; dothing($i,$hilite); } This is basically what I want to accomplish. What is the most efficient way to determine if $i is even? I know I could check if half == mod 2 ... but that seems a little excessive on the calculations? Is there a simpler way? ...

How to get the separate digits of an int number?

I have numbers like 1100, 1002, 1022 etc. I would like to have the individual digits, for example for the first number 1100 I want to have 1, 1, 0, 0. How can I get it in Java? ...

Modulo of negative numbers

Possible Duplicate: Mod of negative number is melting my brain! I was wondering if there was a nicer algorithm for what I'm trying to do: wrapIndex(-6, 3) = 0 wrapIndex(-5, 3) = 1 wrapIndex(-4, 3) = 2 wrapIndex(-3, 3) = 0 wrapIndex(-2, 3) = 1 wrapIndex(-1, 3) = 2 wrapIndex(0, 3) = 0 wrapIndex(1, 3) = 1 wrapIndex(2, 3) = 2 w...

Check if a number is divisible by 3

Not sure if it's a duplicate. But 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? ...

question about % operator in C++

i have following code #include <iostream> #include<exception> #include <cstdlib> int main(){ for (int i=0;i<100;i++){ std::cout<<i<<" "; if (i %5==0){ abort(); } } return 0; } but it only writes 0 and says that abort was called why?i think it should ouput 0 1 2 3 4 and than...

How does a 32-bit operating system perform the 2^56 modulo 7 ?

How does the system perform the 2^56 modulo 7, if it's 32 bits operating system in cryptography for example? And how it stored in memory? ...