Questions tagged [number-theory]

Number theory is that branch of mathematics that investigates the properties of numbers, typically whole numbers.

Number theory is that branch of mathematics that investigates the properties of numbers, typically whole numbers. Applications include cryptography, error correcting codes, checksums, and programming, since many tasks in programming are made much simpler using modular arithmetic. Key concepts/topics: modular arithmetic, unique factorisation theorem, prime numbers, congruences.

Advanced number theory is better treated on:

419 questions
137
votes
13 answers

Given a string of a million numbers, return all repeating 3 digit numbers

I had an interview with a hedge fund company in New York a few months ago and unfortunately, I did not get the internship offer as a data/software engineer. (They also asked the solution to be in Python.) I pretty much screwed up on the first…
its.david
  • 1,884
  • 5
  • 11
  • 29
35
votes
6 answers

finding a^b^c^... mod m

I would like to calculate: abcd... mod m Do you know any efficient way since this number is too big but a , b , c , ... and m fit in a simple 32-bit int. Any Ideas? Caveat: This question is different from finding ab mod m. Also please note that…
user182513
32
votes
2 answers

What's a nice method to factor gaussian integers?

I already have prime factorization (for integers), but now I want to implement it for gaussian integers but how should I do it? thanks!
22
votes
5 answers

Easiest way to perform modular matrix inversion with Python?

I'd like to take the modular inverse of a matrix like [[1,2],[3,4]] mod 7 in Python. I've looked at numpy (which does matrix inversion but not modular matrix inversion) and I saw a few number theory packages online, but nothing that seems to do this…
John
  • 331
  • 1
  • 2
  • 7
22
votes
5 answers

Given a permutation's lexicographic number, is it possible to get any item in it in O(1)

I want to know whether the task explained below is even theoretically possible, and if so how I could do it. You are given a space of N elements (i.e. all numbers between 0 and N-1.) Let's look at the space of all permutations on that space, and…
Ram Rachum
  • 71,442
  • 73
  • 210
  • 338
19
votes
4 answers

Calculating 1^X + 2^X + ... + N^X mod 1000000007

Is there any algorithm to calculate (1^x + 2^x + 3^x + ... + n^x) mod 1000000007? Note: a^b is the b-th power of a. The constraints are 1 <= n <= 10^16, 1 <= x <= 1000. So the value of N is very large. I can only solve for O(m log m) if m =…
square1001
  • 1,196
  • 7
  • 20
18
votes
4 answers

Calculating sum of geometric series (mod m)

I have a series S = i^(m) + i^(2m) + ............... + i^(km) (mod m) 0 <= i < m, k may be very large (up to 100,000,000), m <= 300000 I want to find the sum. I cannot apply the Geometric Progression (GP) formula because then result will…
avd
  • 12,513
  • 29
  • 74
  • 96
18
votes
2 answers

Picking A, C and M for Linear congruential generator

I am looking to implement a simple pseudorandom number generator (PRNG) that has a specified period and guaranteed no collisions for the duration of that period. After doing some research I came across the very famous LCG which is perfect. The…
Dave
  • 6,313
  • 11
  • 49
  • 95
15
votes
2 answers

Algorithm for finding smallest number with given number of factors

What's the most efficient algorithm anyone can think of that, given a natural number n, returns the least natural number x with n positive divisors (including 1 and x)? For example, given 4 the algorithm should result in 6 (divisors: 1,2,3,6); i.e.…
user401445
  • 974
  • 1
  • 12
  • 36
14
votes
1 answer

Parallel Algorithms for Generating Prime Numbers (possibly using Hadoop's map reduce)

Generating Prime numbers is a toy problem that I often attempt from time to time, especially when experimenting with a new programming language, platform or style. I was thinking of attempting to write a Prime Number Generation algorithm or a Prime…
user1172468
  • 4,910
  • 6
  • 29
  • 56
13
votes
3 answers

Represent natural number as sum of distinct squares

The problem is to find the largest set S of positive integers such that the sum of the squares of the elements of S is equal to a given number n. For example: 4 = 2² 20 = 4² + 2² 38 = 5² + 3² + 2² 300 = 11² + 8² + 7² + 6² + 4² + 3² + 2² +…
Eryk Pawilan
  • 143
  • 7
12
votes
1 answer

Binary GCD Algorithm vs. Euclid's Algorithm on modern computers

http://en.wikipedia.org/wiki/Binary_GCD_algorithm This Wikipedia entry has a very dissatisfying implication: the Binary GCD algorithm was at one time as much as 60% more efficient than the standard Euclid Algorithm, but as late as 1998 Knuth…
jkschneider
  • 23,906
  • 11
  • 68
  • 99
12
votes
6 answers

How to enumerate x^2 + y^2 = z^2 - 1 (with additional constraints)

Lets N be a number (10<=N<=10^5). I have to break it into 3 numbers (x,y,z) such that it validates the following conditions. 1. x<=y<=z 2. x^2+y^2=z^2-1; 3. x+y+z<=N I have to find how many combinations I can get from the given numbers in a…
A_Sk
  • 4,200
  • 3
  • 24
  • 47
12
votes
1 answer

Bitshifts to obtain remainder

I want to know how to obtain the remainder by dividing an integer with another integer (both positive) using bitshift or bitwise operators only. The / operator or % operator should not be used. For example, for obtaining the remainder when divisor…
Extreme Coders
  • 3,195
  • 2
  • 33
  • 49
12
votes
3 answers

Find pairs in an array such that a%b = k , where k is a given integer

Here is an interesting programming puzzle I came across . Given an array of positive integers, and a number K. We need to find pairs(a,b) from the array such that a % b = K. I have a naive O(n^2) solution to this where we can check for all pairs…
h4ck3d
  • 5,584
  • 15
  • 44
  • 73
1
2 3
27 28