eyeling 1.11.21 → 1.11.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ # ==========================================================
2
+ # Matiyasevich (1985): π from Fibonacci numbers
3
+ #
4
+ # Illustrates the identity:
5
+ # π = 4 * Σ_{n≥1} atan( 1 / F_{2n+1} )
6
+ # where F_k is the k-th Fibonacci number.
7
+ #
8
+ # This N3 program computes a finite partial sum using the list
9
+ # of n values in :run :ns, and compares it with π = 4*atan(1).
10
+ #
11
+ # Notes:
12
+ # - To keep reasoners terminating, Fibonacci computation is gated:
13
+ # only indices reachable from the required {2n+1} set are derived.
14
+ # ==========================================================
15
+
16
+ @prefix : <http://example.org/matiyasevich#>.
17
+ @prefix math: <http://www.w3.org/2000/10/swap/math#>.
18
+ @prefix list: <http://www.w3.org/2000/10/swap/list#>.
19
+ @prefix log: <http://www.w3.org/2000/10/swap/log#>.
20
+
21
+ :run :ns (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15).
22
+
23
+ # --- Determine which Fibonacci indices are needed: k = 2n+1 ----------
24
+ {
25
+ :run :ns ?Ns0.
26
+ ?Ns0 list:iterate (?i0 ?n0).
27
+
28
+ (2 ?n0) math:product ?twoN0.
29
+ (?twoN0 1) math:sum ?k0.
30
+ }
31
+ =>
32
+ {
33
+ :need :fibIndex ?k0.
34
+ }.
35
+
36
+ # --- Close the needed set downward: if we need n, we also need n-1 and n-2 ----
37
+ {
38
+ :need :fibIndex ?nN.
39
+ ?nN math:greaterThan 1.
40
+
41
+ (?nN 1) math:difference ?n1N.
42
+ (?nN 2) math:difference ?n2N.
43
+ }
44
+ =>
45
+ {
46
+ :need :fibIndex ?n1N.
47
+ :need :fibIndex ?n2N.
48
+ }.
49
+
50
+ # --- Fibonacci facts, computed only for indices requested via :need :fibIndex ----
51
+ {
52
+ :need :fibIndex 0.
53
+ }
54
+ =>
55
+ {
56
+ 0 :fib 0.
57
+ }.
58
+
59
+ {
60
+ :need :fibIndex 1.
61
+ }
62
+ =>
63
+ {
64
+ 1 :fib 1.
65
+ }.
66
+
67
+ {
68
+ :need :fibIndex ?nF.
69
+ ?nF math:greaterThan 1.
70
+
71
+ (?nF 1) math:difference ?n1F.
72
+ (?nF 2) math:difference ?n2F.
73
+
74
+ ?n1F :fib ?f1F.
75
+ ?n2F :fib ?f2F.
76
+ (?f1F ?f2F) math:sum ?fnF.
77
+ }
78
+ =>
79
+ {
80
+ ?nF :fib ?fnF.
81
+ }.
82
+
83
+ # --- Final: collect terms, sum angles, approximate pi, and compute error ----
84
+ {
85
+ :run :ns ?Ns.
86
+
87
+ # Rows: (n k F_k theta_n) with theta_n = atan(1/F_k)
88
+ ( (?n ?k ?F ?theta)
89
+ {
90
+ :run :ns ?Ns.
91
+ ?Ns list:iterate (?i ?n).
92
+
93
+ (2 ?n) math:product ?twoN.
94
+ (?twoN 1) math:sum ?k.
95
+
96
+ ?k :fib ?F.
97
+ (1 ?F) math:quotient ?inv.
98
+ ?inv math:atan ?theta.
99
+ }
100
+ ?rows
101
+ ) log:collectAllIn _:rowsScope.
102
+
103
+ # Angles only, to sum
104
+ ( ?theta2
105
+ {
106
+ :run :ns ?Ns.
107
+ ?Ns list:iterate (?i2 ?n2).
108
+
109
+ (2 ?n2) math:product ?twoN2.
110
+ (?twoN2 1) math:sum ?k2.
111
+
112
+ ?k2 :fib ?F2.
113
+ (1 ?F2) math:quotient ?inv2.
114
+ ?inv2 math:atan ?theta2.
115
+ }
116
+ ?thetas
117
+ ) log:collectAllIn _:thetaScope.
118
+
119
+ # A small “grounding” guard: list:length requires the list input be present.
120
+ ?thetas list:length ?len.
121
+
122
+ ?thetas math:sum ?sumAngles.
123
+ (4 ?sumAngles) math:product ?piFromFibs.
124
+
125
+ # Reference value: atan(1) = pi/4 => pi = 4*atan(1)
126
+ 1 math:atan ?atan1.
127
+ (4 ?atan1) math:product ?piRef.
128
+
129
+ (?piFromFibs ?piRef) math:difference ?delta.
130
+ ?delta math:absoluteValue ?absError.
131
+ }
132
+ =>
133
+ {
134
+ :result
135
+ :rows ?rows;
136
+ :piFromFibs ?piFromFibs;
137
+ :piRef ?piRef;
138
+ :absError ?absError.
139
+ }.
140
+