The Riemann series theorem states that the terms of a conditionally convergent series can be permutated such that the resulting series converges to an arbitrary value.
The Challenge
Write a program or function that takes in three parameters via stdin (or equivalent) or as parameters:
- The expression for the sequence
x_n
(with variablen
). The exact format (e.g., whether a power is represented as^
,pow()
or**
) is irrelevant and may be chosen to fit your language. Alternatively you may pass a function directly as an argument. The definition of the function does not count towards the byte count. - A value
S
that we want to approach. Numerical values only are fine, you don't need to support mathematical expressions. - A threshold
t>0
. Numerical values only are fine here as well.
Your program/function must output (to stdout or equivalent) resp. return a space- or newline-separated list p_1, ..., p_N
of indices.
Your output must satisfy the condition that the sum from k = 1..N
over x_(p_k)
differs from S
no more than t
in terms of the absolute difference. In other words, it must satisfy
So far, since the output will be a finite number of indices, the order would not matter. Therefore, another restriction applies: with decreasing threshold t
, you may only add indices to the output (assuming a fixed S
).
In other words, for a fixed S
, if t_1 < t_2
and [x_1, ..., x_N]
is a solution for t_2
, then a solution for t_1
must be of the form [x_1, ..., x_N, ..., x_M]
with M >= N
.
Details and Restrictions
- The order of the arguments may be changed to suit your needs.
- Provide an example call with the arguments from the example below. Ideally, post a link to an online interpreter/compiler.
eval
and the like are explicitly allowed for the purpose of evaluating mathematical expressions.- Sequences are 1-based, so the first index is 1.
- You don't need to take special care of big integers or floating point problems. There will be no extreme test cases such as using machine precision as the threshold.
- Standard loopholes apply.
- You may assume that the sequence passed to your program is conditionally convergent, i.e. that it is possible to solve the task with the given parameters.
Example
Say your program is in a file riemann.ext
, then a call might be
./riemann.ext "(-1)^(n+1)/n" 0.3465 0.1
Scoring
This is code-golf. The shortest program in bytes (UTF-8 encoding) wins. In case of a tie, the earlier entry wins.
-10
adds10
. \$\endgroup\$x_n
as Javascript'sMath.pow
or Haskell'sfromInteger
for sequences that involve manipulating integers? (as in, assume the expression is eval-ready no matter how non-pseudocodey it is) \$\endgroup\$