1

Let's consider a list of rational numbers, e.g.

5/102
7/211
88/21

Find an integer A such that it equals to the denominators Least Common Multiple (LCM) only if it does not exceed a given upper bound U, otherwise the closest integer less than U which can be used to represent the list of rational numbers:

X1/A ≈ 5/102
X2/A ≈ 7/211
X3/A ≈ 88/21

being A, X1, X2, X3 integers and X1/A, X2/A, X3/A the closest representation possible of 5/102, 7/211, 88/21.

Using standard Python libraries to solve this problem is fine. It's also possible to use the fractions.Fraction.limit_denominator() function, but of course, limiting each fraction's denominator does not guarantuee that the LCM stays under the chosen upper bound U.

mvn1587
  • 11
  • 3
  • 2
    Sounds like homework... what have you tried so far? Show us your code and where you are stuck – T C Molenaar Oct 12 '22 at 09:43
  • 2
    Have you attempted to find a solution yourself? [This thread](https://stackoverflow.com/questions/51716916/built-in-module-to-calculate-the-least-common-multiple) might help you get started – Timo Oct 12 '22 at 09:43
  • It's not that important the code itself, but the procedure. What I tried: 1) Limiting the precision of the floating point numbers until I get LCM < U ; 2) Limiting denominator of individual ratios by 1 untill I get LCM < U. The problem is that it does not guarantuee to be the best fit for the given ratios because there can be many possible denominators reductions (i.e. limiting only first one and not the others? limiting all? limiting only the n-th one?). – mvn1587 Oct 12 '22 at 09:56
  • @Timo it's not about finding LCM, please read the question carefully. – mvn1587 Oct 12 '22 at 09:57
  • I don't know what you mean by limiting the precision of the floating point numbers since the problem description seems to involve ints only. LCM is not a floating point notion. – John Coleman Oct 12 '22 at 10:17
  • 1
    Could you please rephrase the problem in a way that makes it obvious why the following algorithm is not correct? Here is my algorithm: `def denom_under_upperbound(list_of_fractions, U): M = lcm(x.denominator for x in list_of_fractions); return min(M, U)` – Stef Oct 12 '22 at 10:22
  • Your algorithm just selects the lowest between LCM and U. But in the case U is the lowest, if you put U as a common denominator for all fractions, you may not get integers on each numerator. So the question is to find some number <= U which can also produce the best possible approximation (X1/U, X2/U, ...) of the original fractions (5/102, 7/211) being X1, X2, ... integers. – mvn1587 Oct 12 '22 at 10:31
  • 1
    Define "best possible approximation". You have several different fractions so you will have several different approximations. A denominator that makes one better might make another one worse. – John Coleman Oct 12 '22 at 10:37
  • Simply rounding X1, X2 to the nearest integer would be a reasonable heuristic. If you want to do better than that, perhaps you could ask on [mathematics]. Something nontrivial involving e.g. continued fractions (heavily used in the theory of rational approximations) might be required. – John Coleman Oct 12 '22 at 11:04
  • I guess something like the "sum of error" should be minimized. But it may turn into a more complex linear programming problem and I'm wondering if there is a simple way of achieving the same goal. – mvn1587 Oct 12 '22 at 11:06
  • 1
    If q > r, chances are that using q as the denominator will result in better approximations than using r. So, always picking U sounds like a good approximation. Unless you have a more specific and precise way to define "best approximation", I can't give a more specific answer and I suggest to just pick U. – Stef Oct 12 '22 at 11:21
  • Although if you do want to dive into this subject more deeply, I suggest a more math-oriented approach. See this related question on math.stackexchange: [Approximate irrational numbers with the same denominator](https://math.stackexchange.com/questions/1042356/approximate-irrational-numbers-with-the-same-denominator) – Stef Oct 12 '22 at 11:25
  • Also perhaps this pdf document from math.uha.fr: [Best simultaneous Diophantine approximations and multidimensional continued fraction expansions, by Nicolas Chevallier, 2013](http://www.math.uha.fr/chevallier/publication/Chevallier-meilleures.pdf) – Stef Oct 12 '22 at 11:27
  • Perhaps more directly helpful: [math.stackexchange: An algorithm for the simultaneous Diophantine approximation?](https://math.stackexchange.com/questions/2233214/an-algorithm-for-the-simultaneous-diophantine-approximation) – Stef Oct 12 '22 at 11:46
  • @Stef By the way, for N=A/B it's NOT true that the greater is B, the more precise we can get in general. For example, let N=3.123 and limit B to 10. If we set B=10, the best numerator is 3, i.e. the resulting number is 3/10 = 0.3 which is not the closest to N, since we could have 25/8 which is 3.125. – mvn1587 Oct 12 '22 at 15:32
  • 1
    @mvn1587 Indeed. A much simpler example would have been N = 1/3, for which 1/3 is a better approximation than 3/10. Which is why I began my statement with "chances are". When the denominator is B, the error can be at most 1/(2B). So increasing B does tend to decrease the error. And since you're trying to decrease the error on three numbers simultaneously, chances are that reducing the error on one number by reducing B risks increasing the error on another number. So, picking B = U, the maximum allowed value, sounds like a good approximation. – Stef Oct 12 '22 at 15:54
  • 1
    @mvn1587 A better approximation can be found, but unless you want to bruteforce, it will require a lot more math in the algorithm. And begins with reading about Dirichlet's simultaneous approximation theorem, and then reading about the Lenstra–Lenstra–Lovász lattice basis reduction algorithm. I briefly searched to see if a python implementation was available somewhere, but couldn't find one. – Stef Oct 12 '22 at 15:55
  • 1
    Please [edit] your question and incorporate the (relevant) details mentioned in the comments. The question should be clear without having to read all comments. – wovano Oct 12 '22 at 20:04

0 Answers0