Showing posts with label error. Show all posts
Showing posts with label error. Show all posts

Wednesday, 10 April 2024

Dartboard Totals


I've only made one post about darts and dartboards and that was Measuring Dartsmanship on Wednesday 24th of January 2024. In this post, I want to quantify the number the of ways in which a certain total can be achieved using one, two or three darts. To this end, I developed a program in SageMath to do the job and at first glance it worked fine. Figure 1 shows the code using 75 as sample input (permalink).

Figure 1

The output is shown below (there are 194 possible ways of achieving a total of 75):

[[1, 14, 60], [1, 17, 57], [1, 20, 54], [1, 26, 48], [1, 34, 40], [1, 36, 38], [1, 42, 32], [2, 13, 60], [2, 16, 57], [2, 22, 51], [2, 28, 45], [2, 33, 40], [2, 39, 34], [2, 54, 19], [3, 12, 60], [3, 15, 57], [3, 18, 54], [3, 21, 51], [3, 24, 48], [3, 27, 45], [3, 30, 42], [3, 32, 40], [3, 33, 39], [3, 34, 38], [3, 36, 36], [4, 11, 60], [4, 14, 57], [4, 17, 54], [4, 20, 51], [4, 26, 45], [4, 33, 38], [4, 39, 32], [5, 10, 60], [5, 13, 57], [5, 16, 54], [5, 22, 48], [5, 28, 42], [5, 30, 40], [5, 32, 38], [5, 36, 34], [5, 51, 19], [6, 9, 60], [6, 12, 57], [6, 15, 54], [6, 18, 51], [6, 21, 48], [6, 24, 45], [6, 27, 42], [6, 30, 39], [6, 33, 36], [7, 11, 57], [7, 14, 54], [7, 17, 51], [7, 20, 48], [7, 26, 42], [7, 28, 40], [7, 30, 38], [7, 34, 34], [7, 36, 32], [8, 7, 60], [8, 10, 57], [8, 13, 54], [8, 16, 51], [8, 22, 45], [8, 27, 40], [8, 33, 34], [8, 39, 28], [8, 48, 19], [9, 9, 57], [9, 12, 54], [9, 15, 51], [9, 18, 48], [9, 21, 45], [9, 24, 42], [9, 26, 40], [9, 27, 39], [9, 28, 38], [9, 30, 36], [9, 32, 34], [9, 33, 33], [10, 11, 54], [10, 14, 51], [10, 20, 45], [10, 26, 39], [10, 27, 38], [10, 33, 32], [10, 48, 17], [11, 13, 51], [11, 22, 42], [11, 26, 38], [11, 32, 32], [11, 36, 28], [11, 45, 19], [12, 12, 51], [12, 15, 48], [12, 18, 45], [12, 21, 42], [12, 24, 39], [12, 27, 36], [12, 30, 33], [13, 28, 34], [13, 45, 17], [14, 13, 48], [14, 16, 45], [14, 21, 40], [14, 22, 39], [14, 27, 34], [14, 33, 28], [14, 42, 19], [15, 15, 45], [15, 18, 42], [15, 20, 40], [15, 21, 39], [15, 22, 38], [15, 24, 36], [15, 26, 34], [15, 27, 33], [15, 28, 32], [15, 30, 30], [15, 60], [16, 11, 48], [16, 19, 40], [16, 20, 39], [16, 27, 32], [16, 33, 26], [16, 42, 17], [18, 17, 40], [18, 18, 39], [18, 19, 38], [18, 21, 36], [18, 24, 33], [18, 27, 30], [18, 57], [20, 13, 42], [20, 17, 38], [20, 22, 33], [20, 36, 19], [21, 16, 38], [21, 20, 34], [21, 21, 33], [21, 22, 32], [21, 24, 30], [21, 26, 28], [21, 27, 27], [21, 54], [22, 13, 40], [22, 34, 19], [22, 36, 17], [24, 11, 40], [24, 13, 38], [24, 17, 34], [24, 24, 27], [24, 32, 19], [24, 51], [25, 2, 48], [25, 5, 45], [25, 8, 42], [25, 10, 40], [25, 11, 39], [25, 12, 38], [25, 14, 36], [25, 16, 34], [25, 18, 32], [25, 20, 30], [25, 22, 28], [25, 24, 26], [25, 25, 25], [25, 33, 17], [25, 50], [26, 32, 17], [27, 20, 28], [27, 22, 26], [27, 48], [28, 28, 19], [30, 11, 34], [30, 13, 32], [30, 26, 19], [30, 28, 17], [30, 45], [33, 42], [36, 13, 26], [36, 39], [39, 17, 19], [50, 1, 24], [50, 3, 22], [50, 4, 21], [50, 5, 20], [50, 6, 19], [50, 8, 17], [50, 9, 16], [50, 10, 15], [50, 12, 13], [50, 14, 11], [50, 18, 7]]

194

However, using 48 as a total produces an error message as shown in Figure 2.


Figure 2

I don't understand why sum(c) works when the target is 75 but it doesn't work when the target is 48. However, I found that it would work if I made the total 49 and altered the code from "if target = sum(c)" to "if target - 1 = sum(c)". Weird, right. The program works for 23, 29, 31, 35, 37, 41, 43, 44, 46, 47, 49, 52 and does better as the totals get larger. I can't see any obvious pattern to the misfires.

I imported Numpy and used its sum() function but that didn't work either. The same with Pandas. I put Google's Gemini to work on the problem using this prompt:

This SageMath code works for some numbers e.g. 49 but not for other numbers such as 48. When I use 48 as input, I get the following error message: unsupported operand parent(s) for +: 'Integer Ring' and '<class 'list'>'. The error location is sum(c) but I can't determine what the problem is as I don't understand the error message. 

Gemini was confident that it had identified the problem and even proposed a solution but that proved to be nonsense even though appearing plausible at first glance. I'll have to leave it there and if I do discover the source of the problem I'll discuss it here. Link to Airtable record.

Saturday, 5 November 2022

The Mediant

The mediant is such a simple concept and yet I can't recall ever having heard of the term before.$$ \begin{align} \text{Given two fractions } \frac{a}{b} \text{ and } \frac{c}{d} \text{ such that } \frac{a}{b}<\frac{c}{d} \text{ then}\\ \text{the mediant is defined as } \frac{a+c}{b+d} \text{ where } \frac{a}{b} < \frac{a+c}{b+d} < \frac{c}{d} \end{align} $$There's a nice visual proof of this inequality to be found on this Mathematical Visual Proofs YouTube channel.

One interesting application of the mediant is a means of finding fractional approximations to irrational and transcendental numbers. I got the idea for this after watching this YouTube video titled Fraction-Finding with the Mediant Method and even though the author uses Python it was not clear what he was doing. However, I grasped what he was on about and wrote my own code. Here is a permalink to it in which I've used \(e\) as an example but any number can be used as a target e.g. \( \pi \) or \( \sqrt{2} \). 

The program discards initially the whole number part of the number. So in the case of \(e\), this means discarding 2. What's left is then a decimal number between 0 and 1. We begin by starting with two fractions: one below the number and the other above. If we set \(a=0\) and \(b=1\) and \(c=1\) and \(d=1\) then we know that:$$ \frac{a}{b}=\frac{0}{1}=0 \text{ and } \frac{c}{d}=\frac{1}{1}=1$$are starting points that will always work. The algorithm then generates a new fraction (always 1/2 initially) that may be above or below the (infinite) decimal part of the number that we are trying to approximate. The algorithm ensures that another fraction is always chosen so that it is on the opposite side of the number on the number line. In this way the fractions get closer and closer to the number and the algorithm terminates when the approximation is within a bound that has been set initially.

In the case of \(e\) the following fractions are generated once the integer part has been added back in (so our initial 1/2 becomes 2+1/2 = 5/2 etc):

5/2, 8/3, 11/4, 19/7, 30/11, 49/18, 68/25, 87/32, 106/39, 193/71, 299/110, 492/181, 685/252, 878/323, 1071/394, 1264/465, 1457/536, 2721/1001, 4178/1537, 6899/2538, 9620/3539, 12341/4540, 15062/5541, 17783/6542, 20504/7543, 23225/8544, 25946/9545, 49171/18089, 75117/27634, 124288/45723, 173459/63812, 222630/81901, 271801/99990, 320972/118079, 370143/136168, 419314/154257, 468485/172346, 517656/190435, 566827/208524, 1084483/398959

I set the acceptable error to 0.000000000001 and once the fraction was below this the program stopped. The output that should be displayed above is:

Final approximating fraction for e is 1084483/398959
Decimal approximation is for e is 2.71828182845856
Actual decimal approximation for e is 2.71828182845905