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
3
votes
4 answers

Fastest Method to calculate 2^n Mod m where n and m are Random integers

I was wandering if there is any possible efficient way of finding the Remainder when 2^n is divided by m where n and m are random integers. Is there any equation where I sub n and m into to give me the remainder or do we have to follow recursive…
AspiringMat
  • 2,161
  • 2
  • 21
  • 33
3
votes
3 answers

Increase digits of a number by one

I am working on this seemingly simple problem, where I need to add one to every digit of a number. Example: number = 1234 ; output = 2345 That's simple, but when 9 is one of those digits, then by the law of addition, that 9 will be replaced by 0 and…
hky404
  • 1,039
  • 3
  • 17
  • 35
3
votes
1 answer

C++ class design: dynamic typing alternative to template argument?

I would like to build a space-efficient modular arithmetic class. The idea is that the modulus M is an immutable attribute that gets fixed during instantiation, so if we have a large array (std::vector or another container) of values with the same…
3
votes
2 answers

Fast implementation binary exponentiation implementation in OpenCL

I've been trying to design a fast binary exponentiation implementation in OpenCL. My current implementation is very similar to the one in this book about pi. // Returns 16^n mod ak inline double expm (long n, double ak) { double r = 16.0; …
AnimatedRNG
  • 1,859
  • 3
  • 26
  • 39
3
votes
1 answer

How to Convert from a Residual Number System to a Mixed Radix System?

I understand the concept of a Residual Number System and the concept of a Mixed Radix system, but I'm having difficulty getting any of the conversion methods I find to work in a simple case study. I started at Knuth's Art of Computer Programming but…
eyepatch
  • 85
  • 6
2
votes
0 answers

Base-n integer representations library in Isabelle/HOL?

Context: I am trying to translate math contest problems into Isabelle/HOL. A lot of these contest problems involve questions like "what are the last two digits of this number when written in base 7"? Isabelle/HOL has a rudimentary treatment of…
Charles Staats
  • 203
  • 1
  • 9
2
votes
1 answer

Why do I have inconsistent evaluation of cipher and corresponding plain message using RSA algorithm?

Rationale To simulate the RSA algorithm in C using a robust command-line application written in C. Here is my problem: Taking values of e=7, p=3 and q=11 works fine when encrypting and decrypting. However, when I veer slightly off to use other…
thetva
  • 123
  • 1
  • 8
2
votes
2 answers

This (modulo 2) binary matrix multiplication algorithm seems to underperform. What can I do better?

The question has changed since its initial posting as I've chased down a few leads. At this point, I'd say that I'm really looking for the following answers: Can a significant amount of time be saved by replacing addition/multiplication followed by…
2
votes
1 answer

modulo operation not returning correct value

I'm trying to solve a problem in leetcode, I have cracked the algorithm for that specific problem, written pseudo code, and implemented the code in C++. Just one flaw remains in the solution, modulo 1e9+7 the result. The problem statetment : Given…
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
2
votes
1 answer

Modular exponentiation of large number

I'm currently working with a large number for cryptography. a = 3087646334 p = 1606938044258990275541962092341162602522202993782792835301611 b = int((p-1)/2). Euler's Criterion says that a^b (mod p) = 1 or a^b (mod p) = p-1. SAGE gives the…
2
votes
1 answer

Issue with Modular Exponentiation C++

I'm trying to perform Modular Exponentiation for large values (upto 64-bits) and I wrote this function for it: uint64_t modularExp(uint64_t num, uint64_t exp, uint64_t mod) { string expBits = bitset<64>(exp).to_string(); expBits =…
2
votes
0 answers

How does CPython know to optimise (m**e)%n?

If I run, assuming e is a large value like 65537: x**e It takes a very long time, however assuming a reasonable n: (x**e)%n Runs much faster, so Python is clearly optimising modular exponentiation. My question is how does CPython identify that it…
0x777C
  • 993
  • 7
  • 21
2
votes
2 answers

Trouble with modular exponentiation

I am trying to solve a problem where we have to output the the last digit of the given number n^p. int modularExponentiation(int n, long long p, int m){ if(p == 0) return 1; if(p & 1) return (n % m * modularExponentiation((n*n) % m,…
2
votes
1 answer

Sum of numbers from 0 to 10^18 with modulus 10^9+7

How to find sum of numbers from 0 to n modulus 109 + 7, where n ≤ 1018? I want to store the result in long long int only and not in array or string. My code results in a run time error. const unsigned int m = 1000000007; long long int n; cin >>…
2
votes
1 answer

Miller-Rabin test (SICP 1.28)

One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermat's Little Theorem, which states that if n is a prime number and a is any positive integer…
Brady Dean
  • 3,378
  • 5
  • 24
  • 50