Post Reply 
Maths/Stats challenge - 1 of 2 - polls
05-25-2022, 06:54 AM
Post: #12
RE: Maths/Stats challenge - 1 of 2 - polls
This Python program uses brute force to loop through all numbers from 1 to 100:
Code:
import numpy as np

def approximate(p):
    m = 10
    
    for k in range(1, 100):
        r = np.round(k * p)
        q = r / k
        d = np.linalg.norm(p - q)
        if d < m:
            n, m, u, v = k, d, r, q

    print(f"{u} / {n} ~ {np.round(v, decimals=2)}")

We can use it with the given examples:
Code:
for p in [
    [.23, .41, .36],
    [.12, .18, .45, .25],
    [.16, .02, .51, .31],
    [.45, .17, .25, .13, .0],
]:
    approximate(np.array(p))

The result is:

[14. 25. 22.] / 61 ~ [0.23 0.41 0.36]
[10. 15. 38. 21.] / 84 ~ [0.12 0.18 0.45 0.25]
[15. 2. 48. 29.] / 94 ~ [0.16 0.02 0.51 0.31]
[34. 13. 19. 10. 0.] / 76 ~ [0.45 0.17 0.25 0.13 0. ]
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Maths/Stats challenge - 1 of 2 - polls - Thomas Klemm - 05-25-2022 06:54 AM



User(s) browsing this thread: 1 Guest(s)