Questions about properties of, working with and algorithms on integers.
Questions tagged [integers]
172 questions
24
votes
2 answers
Efficient algorithm for 'unsumming' a set of sums
Given a multiset of natural numbers X, consider the set of all possible sums:
$$\textrm{sums}(X)= \left\{ \sum_{i \in A} i \,|\, A \subseteq X \right\}$$
For example, $\textrm{sums}(\left\{1,5\right\}) = \left\{0, 1, 5, 6\right\}$ while …
Uri Granta
- 343
- 1
- 6
22
votes
3 answers
Algorithm to minimize surface area, given volume
Consider the following algorithmic task:
Input: a positive integer $n$, along with its prime factorization
Find: positive integers $x,y,z$ that minimize $xy+yz+xz$, subject to the restriction that $xyz=n$
What is the complexity of this problem? Is…
D.W.
- 167,959
- 22
- 232
- 500
21
votes
3 answers
What is the most efficient way to compute factorials modulo a prime?
Do you know any algorithm that calculates the factorial after modulus efficiently?
For example, I want to program:
for(i=0; i<5; i++)
sum += factorial(p-i) % p;
But, p is a big number (prime) for applying factorial directly $(p \leq 10^ 8)$.
In…
Jonathan Prieto-Cubides
- 2,229
- 3
- 18
- 26
17
votes
3 answers
Determine missing number in data stream
We receive a stream of $n-1$ pairwise different numbers from the set $\left\{1,\dots,n\right\}$.
How can I determine the missing number with an algorithm that reads the stream once and uses a memory of only $O(\log_2 n)$ bits?
Queue
- 489
- 4
- 11
16
votes
2 answers
Representing Negative and Complex Numbers Using Lambda Calculus
Most tutorials on Lambda Calculus provide example where Positive Integers and Booleans can be represented by Functions. What about -1 and i?
zcaudate
- 293
- 2
- 6
15
votes
4 answers
Comparing rational numbers
Given $a,b,c,d \in \mathbb N$ and $b,d \notin \{0\}$,
$$
\begin{eqnarray*}
\frac a b < \frac c d &\iff& ad < cb
\end{eqnarray*}
$$
My questions are:
Given $a,b,c,d$
Assuming we can decide $x < y \in \mathbb Z$ in $\mathcal{O}(|x| +|y|)$, is there…
Realz Slaw
- 6,251
- 33
- 71
13
votes
1 answer
Overflow safe summation
Suppose I am given $n$ fixed width integers (i.e. they fit in a register of width $w$), $a_1, a_2, \dots a_n$ such that their sum $a_1 + a_2 + \dots + a_n = S$ also fits in a register of width $w$.
It seems to me that we can always permute the…
Aryabhata
- 6,291
- 2
- 36
- 47
12
votes
3 answers
What data structure would efficiently store integer ranges?
I need to keep a collection on integers in the range 0 to 65535 so that I can quickly do the following:
Insert a new integer
Insert a range of contiguous integers
Remove an integer
Remove all integers below an integer
Test if an integer is…
WilliamKF
- 427
- 1
- 7
- 14
11
votes
5 answers
Language of the values of an affine function
Write $\bar n$ for the decimal expansion of $n$ (with no leading 0). Let $a$ and $b$ be integers, with $a > 0$. Consider the language of the decimal expansions of the multiples of $a$ plus a constant:
$$M = \{ \overline{a\,x+b} \mid x\in\mathbb{N}…
Gilles 'SO- stop being evil'
- 44,159
- 8
- 120
- 184
11
votes
1 answer
What algorithms exist for solving natural number linear systems?
I'm looking at the following problem:
Given $n$-dimensional vectors of natural numbers $v_1, \ldots, v_m$ and some input vector $u$, is $u$ a linear combination of the $v_i$'s with natural number coefficients?
i.e. are there some $t_1, \ldots, t_m…
Joey Eremondi
- 30,277
- 5
- 67
- 122
11
votes
4 answers
Most efficient algorithm to print 1-100 using a given random number generator
We are given a random number generator RandNum50 which generates a random integer uniformly in the range 1–50.
We may use only this random number generator to generate and print all integers from 1 to 100 in a random order. Every number must come…
Raj Wadhwa
- 233
- 2
- 7
11
votes
3 answers
Number of multisets such that each number from 1 to $n$ can be uniquely expressed as a sum of some of the elements of the multiset
My problem. Given $n$, I want to count the number of valid multisets $S$. A multiset $S$ is valid if
The sum of the elements of $S$ is $n$, and
Every number from $1$ to $n$ can be expressed uniquely as a sum of some of the elements of…
justice league
- 377
- 1
- 10
10
votes
2 answers
Complexity of computing $n^{n^2}$
What is the complexity of computing $n^{n^2},\;n \in \mathbb{N}$?
Croq
9
votes
1 answer
Algorithm for multiplying multivariate polynomials
Let $R$ be a commutative ring. Let $f(x_1, \dots, x_n), g(x_1, \dots, x_n)$ be two multidimensional polynomials in $R$ with maximal total degree $\delta$.
How fast can we compute the product of $f$ and $g$, i.e. the resulting coefficients of each…
chtenb
- 271
- 2
- 10
9
votes
3 answers
Test if there exists an integer k to add to one sequence to make it a subsequence of another sequence
Suppose that sequence $A$ contains $n$ integers $a_1,a_2,a_3,\ldots,a_n$ and sequence $B$ contains $m$ integers $b_1,b_2,b_3,\ldots,b_m$. We know that $m \geq n$. We assume without loss of generality that both sequences $A$ and $B$ are sorted in…
iouvxz
- 61
- 6