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
1 answer

why Recusrsive modular exponentiation not equals iterative?

I have implemented a non-recursive modular exponentiation typedef long long uii; uii modularExponentiation(uii base,uii exponent,uii p) { int result= 1; base = base % p; while( exponent > 0) { if (exponent % 2 == 1) …
Unbound
  • 243
  • 2
  • 12
1
vote
3 answers

Calculate floor(pow(2,n)/10) mod 10 - sum of digits of pow(2,n)

This is also a math related question, but I'd like to implement it in C++...so, I have a number in the form 2^n, and I have to calculate the sum of its digits ( in base 10;P ). My idea is to calculate it with the following formula: sum = (2^n mod…
David Szalai
  • 2,363
  • 28
  • 47
1
vote
1 answer

implementation of nCr and inverse factorial (MODm) for very large numbers

Hi i have problem in implementing nCr MODm in code sprint5 problem. Link to the problem is ...... https://www.hackerrank.com/contests/codesprint5/challenges/matrix-tracing. what I learned yet is I can apply rules of mudular arithmatic to factorial…
Tushar
  • 61
  • 8
1
vote
1 answer

Smart modular multiplication using BigInteger Java

I need to compute the product of two BigIntegers raised to BigIntegers modular a prime. I'm computing - y^r * r^s (mod p). The code I'm using works, but I can't help feeling that it is performing unnecessary calculations, which are pretty…
Saf
  • 517
  • 1
  • 9
  • 24
1
vote
3 answers

Clock style counter for Enigma Encoding Machine in Haskell

I am trying to program the Enigma Coding Machine. I have managed to get the rotors and reflector working fine but am trying to work out the rotor advances. For anyone not familiar with this. An Enigma Machine consists of 3 rotors which are…
1
vote
3 answers

What is the semantic for the modulo operator x % y, when y is 0?

Suppose I try to perform the following: y = 0; z = x % y; is the semantic for this well-defined, platform-dependent, or undefined? I'm asking mainly about C/C++, but am interested in the answer for various programming/scripting languages (Java,…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1
vote
1 answer

Modular exponentiation implementation in Python 3

Basically this is a homework question. I'm supposed to implement these two pseudo-code algorithms in Python3. I'm doing something wrong and I can't figure out what (it seems like this should be simple so I'm not sure what/where I botched this. It…
Niko
  • 4,158
  • 9
  • 46
  • 85
1
vote
1 answer

number of solution to nonlinear congruence equation

I am trying to find the number of solution to x^a (mod b) =c with 0<=x<=u where b<=50 but a and u can be large. My approach is to iterate through each value of x from 0 till min(b,u), and if it satisfies the equation add ceil((u-x)/b) (to…
1
vote
2 answers

Modular arithmetic in Python

Problem 48 description from Project Euler: The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. I've just solved this problem using a one-liner in Python: print…
syntagma
  • 23,346
  • 16
  • 78
  • 134
1
vote
1 answer

Montgomery multiplication VHDL Implementation

I am trying to create a modular arithmetic operation in this case: x*y mod n As far as I have read the fastest way to do it is using the Montgomery multiplication, but I cant understand how that is actually done in other to implement it in…
mmm
  • 689
  • 2
  • 12
  • 25
1
vote
1 answer

Modular multiplication of large numbers

I need to find an efficient algorithm for doing modular multiplication of three numbers. In other words is there a way to find this in C? (100100101010001001 * 1010000100100010 * 10010001001010100101001) % 1000000007
mjnovice
  • 172
  • 11
0
votes
1 answer

RSA - CTF Encrypt and Decrypt

I am currently trying to solve a practice CTF challenge on RSA. The source code of the challenge is the following: from Crypto.Util.number import getStrongPrime, bytes_to_long from secret import flag p = getStrongPrime(1024) n = p*p ct =…
Shark44
  • 593
  • 1
  • 4
  • 11
0
votes
1 answer

Find the combination of coin chips by minimising the value of E=4N + 0.3n

I want to find the perfect combination of coin chips for an amount (say A) for given denominations = [0.1, 0.5, 1, 5, 20, 100, 500]. There are multiple combinations possible but we have to consider only that combination list which will have minimum…
0
votes
1 answer

Modular Exponentiation using mips

Write a program which prompts the user for three positive numbers x, n and p, and outputs x^n mod p. Your program should use the recursive version of modular exponentiation. .text main: sub $sp,$sp,4 # save return address on…
eswcs
  • 5
  • 3
0
votes
0 answers

Plot of gcd(a,b) with python

I want to make a plot of gcd function over some region of integers. So basically I want a dot over (x=a,y=b) at z=gcd(x,y) when 0
yngabl
  • 121
  • 1
  • 5