Questions tagged [modular-arithmetic]

Modular arithmetic is quite a useful tool in number theory. In particular, it can be used to obtain information about the solutions (or lack thereof) of a specific equation.

In mathematics, modular arithmetic (sometimes called clock arithmetic) is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value—the modulus. The modern approach to modular arithmetic was developed by Carl Friedrich Gauss in his book Disquisitiones Arithmeticae, published in 1801.

Time-keeping on this clock uses arithmetic modulo 12. A familiar use of modular arithmetic is in the 12-hour clock, in which the day is divided into two 12-hour periods. If the time is 7:00 now, then 8 hours later it will be 3:00. Usual addition would suggest that the later time should be 7 + 8 = 15, but this is not the answer because clock time "wraps around" every 12 hours; in 12-hour time, there is no "15 o'clock". Likewise, if the clock starts at 12:00 (noon) and 21 hours elapse, then the time will be 9:00 the next day, rather than 33:00. Since the hour number starts over after it reaches 12, this is arithmetic modulo 12. 12 is congruent not only to 12 itself, but also to 0, so the time called "12:00" could also be called "0:00", since 0 ≡ 12 mod 12.

324 questions
1
vote
2 answers

Calculate a formula in a Finite Field

I am trying to transform a formula over to a finite-field equivalent of that formula. The formula can be seen below: Now I have this implemented and it works correctly, but I need this in a finite-field, which means that I introduce a p, let's say…
1
vote
1 answer

Calculate the Minimum Number of Coins For Given Amount of Change in Haskell

I am trying to write an algorithm for the Change-making problem. I am assuming the coin system is canonical, which means the greedy algorithm of always choosing the largest coin that is less than the remaining amount is an optimal…
Inquirer
  • 101
  • 1
  • 7
1
vote
1 answer

Can I ignore the last k while expanding (a + b) % k?

Today I was trying to solve a problem that involved modular arithmetic. I was not able to solve it. So I looked it up on Geeks for Geeks The above image shows what the author did. I know modular addition for two numbers (a + b) % m = (a % m + b…
1
vote
1 answer

Calculate distance between points in matrix connected in all directions

I'm trying to make an online game where I want to simulate a world that is connected in all directions, just like "PAC-MAN" does. When the player crosses the boundaries of the map he would be in the other side of the grid. So far so good, I managed…
blacksmith
  • 25
  • 1
  • 5
1
vote
1 answer

AND vs. MOD for ODD

One of the most basic operations in programming is figuring out whether the given x is even or odd. The common way to do that is: ODD(x) = x MOD 2 == 1 The other less popular variant being: ODD(x) = x AND 1 == 1 It is widely known that those ~bit…
1
vote
0 answers

SINGLE PRODUCT AT A TIME

I have sequences like : 1:1,2,3,4,...10 2:1,2,3,4,...10 Similarly ,a3,a4,a5,..a I have (≤15) queries , and in each query : two numbers and are given , for eg if =1 and =2, have to calculate such that : =1∗1+2∗2+3∗3+....+10∗10 as p is large we…
1
vote
1 answer

Efficient reasoning in modular arithmetic

I decided to prove the following theorem: theory Scratch imports Main begin lemma "(3::int)^k mod 4 = 1 ⟷ even k" proof (cases "even k") case True then obtain l where "2*l = k" by auto then show ?thesis using power_mult [of…
Maya
  • 1,490
  • 12
  • 24
1
vote
2 answers

How to order or compare two circular variables (i.e. milliseconds in whole minute), modular arithmetic

I have 2 timestamps represented as milliseconds in the last minute. Imagine there are no synchronization issues between nodes. The receiver has to distinguish which is the first that was generated message. Unfortunately, after 59 seconds the…
DAme
  • 697
  • 8
  • 21
1
vote
0 answers

How to change my code to solve modular equations with division?

I wrote the Extended Euclidean Algorithm, but i'm having trouble with using it to solve the following equation: which the Modular Equation Solver reduces to and Here is my code: def fastlinearcongruenceSO(powx, divmodx, N, withstats=False): x,…
oppressionslayer
  • 6,942
  • 2
  • 7
  • 24
1
vote
0 answers

How to compute a/b mod P, P is a prime number?

I have been searching an answer for this question on various sites. I can understand all the basic rules of modular arithmetic until this bad guy comes in. Recently I have been suggested this page for simple understanding that…
1
vote
1 answer

Can this script have better performance using modular exponentiation?

def f(a, b, c): return ((a ** b)-1) // c % b Can this script be faster in some way? (I have been looking for something with modular exponentiation): pow(a, b, c) == a ** b % c but this above script doesn't seem to be improvable like that. Does…
1
vote
1 answer

How to calculate 2 to the power of a large number modulo another large number?

M = 115792089237316195423570985008687907853269984665640564039457584007908834671663 296514807760119017459957299373576180339312098253841362800539826362414936958669 % M = ? Is it possible to calculate this in Python? Or are there other methods?
Mamu
  • 13
  • 3
1
vote
0 answers

Modular Arithmetic in R

I'm quite new to R and I've been wondering if there is a way to perform modular arithmetic, either in base R or any package, I have not found anything/thought of anything so far. If it is not clear what i mean by modular arithmetic, i mean something…
1
vote
0 answers

Modular Exponentiation with 8 digit numbers

A Java program to find the result of raising a large number to a power over a modulus does not appear to work when the base of the exponent is 8 digits or greater. Bases that have 8 or more digits do not work (checked against online modular…
Marco Deicas
  • 53
  • 1
  • 3