foxmath 0.4.0__tar.gz → 0.5.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: foxmath
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Minimalist CLI for math & cryptography exploration 🦊
5
5
  Author-email: Abhrankan Chakrabarti <abhrankan@gmail.com>
6
6
  License: MIT
@@ -32,9 +32,11 @@ Built with curiosity — for students, tinkerers, and crypto/math enthusiasts.
32
32
  named curves (secp256k1) with on-curve result validation
33
33
  - Chinese Remainder Theorem solver (handles non-coprime moduli)
34
34
  - Continued fraction expansion and convergents
35
- - Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd, foxmath-crt, foxmath-cf)
35
+ - Challenge/quiz mode — practice problems with scoring, or a
36
+ worksheet mode for self-study
37
+ - Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd,
38
+ foxmath-crt, foxmath-cf, foxmath-challenge)
36
39
  - JSON output mode
37
- - Coming soon: challenge mode
38
40
 
39
41
  ## Installation
40
42
 
@@ -101,6 +103,20 @@ foxmath cf --num 355 --den 113
101
103
  355/113
102
104
  ```
103
105
 
106
+ ```bash
107
+ # Challenge/quiz mode — worksheet (no prompting)
108
+ foxmath challenge --topic legendre --count 2 --seed 1 --reveal
109
+ ```
110
+ ```
111
+ 1. Legendre symbol (10/13)
112
+ Answer: 1
113
+ 2. Legendre symbol (3/7)
114
+ Answer: -1
115
+ ```
116
+
117
+ Drop `--reveal` to answer interactively instead, with scoring at the end.
118
+ `--topic` accepts `legendre`, `crt`, or `mixed` (default).
119
+
104
120
  ```bash
105
121
  # JSON output
106
122
  foxmath pi --terms 100 --json
@@ -13,9 +13,11 @@ Built with curiosity — for students, tinkerers, and crypto/math enthusiasts.
13
13
  named curves (secp256k1) with on-curve result validation
14
14
  - Chinese Remainder Theorem solver (handles non-coprime moduli)
15
15
  - Continued fraction expansion and convergents
16
- - Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd, foxmath-crt, foxmath-cf)
16
+ - Challenge/quiz mode — practice problems with scoring, or a
17
+ worksheet mode for self-study
18
+ - Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd,
19
+ foxmath-crt, foxmath-cf, foxmath-challenge)
17
20
  - JSON output mode
18
- - Coming soon: challenge mode
19
21
 
20
22
  ## Installation
21
23
 
@@ -82,6 +84,20 @@ foxmath cf --num 355 --den 113
82
84
  355/113
83
85
  ```
84
86
 
87
+ ```bash
88
+ # Challenge/quiz mode — worksheet (no prompting)
89
+ foxmath challenge --topic legendre --count 2 --seed 1 --reveal
90
+ ```
91
+ ```
92
+ 1. Legendre symbol (10/13)
93
+ Answer: 1
94
+ 2. Legendre symbol (3/7)
95
+ Answer: -1
96
+ ```
97
+
98
+ Drop `--reveal` to answer interactively instead, with scoring at the end.
99
+ `--topic` accepts `legendre`, `crt`, or `mixed` (default).
100
+
85
101
  ```bash
86
102
  # JSON output
87
103
  foxmath pi --terms 100 --json
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "foxmath"
7
- version = "0.4.0"
7
+ version = "0.5.0"
8
8
  description = "Minimalist CLI for math & cryptography exploration 🦊"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -33,3 +33,4 @@ foxmath-pi = "foxmath.foxmath:main"
33
33
  foxmath-ecadd = "foxmath.foxmath:main"
34
34
  foxmath-crt = "foxmath.foxmath:main"
35
35
  foxmath-cf = "foxmath.foxmath:main"
36
+ foxmath-challenge = "foxmath.foxmath:main"
@@ -7,7 +7,9 @@ One binary. Clean. Educational. Safe.
7
7
  import argparse
8
8
  import sys
9
9
  import json
10
+ import random
10
11
  from decimal import Decimal, getcontext
12
+ from math import gcd
11
13
  from pathlib import Path
12
14
 
13
15
  # ------------------- Math Functions -------------------
@@ -150,6 +152,46 @@ def cf_convergents(cf):
150
152
  k2, k1 = k1, k
151
153
  return convergents
152
154
 
155
+ # ------------------- Challenge / Quiz Mode -------------------
156
+
157
+ _SMALL_ODD_PRIMES = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
158
+ 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
159
+ _CRT_MODULI_CANDIDATES = [3, 4, 5, 7, 9, 11, 13]
160
+
161
+ def _generate_legendre_problem(rng: random.Random) -> dict:
162
+ p = rng.choice(_SMALL_ODD_PRIMES)
163
+ a = rng.randint(1, p - 1)
164
+ return {
165
+ "topic": "legendre",
166
+ "question": f"Legendre symbol ({a}/{p})",
167
+ "answer": legendre_symbol(a, p),
168
+ }
169
+
170
+ def _generate_crt_problem(rng: random.Random) -> dict:
171
+ while True:
172
+ m1, m2 = rng.sample(_CRT_MODULI_CANDIDATES, 2)
173
+ if gcd(m1, m2) == 1:
174
+ break
175
+ r1, r2 = rng.randrange(m1), rng.randrange(m2)
176
+ x, m = crt_solve([r1, r2], [m1, m2])
177
+ return {
178
+ "topic": "crt",
179
+ "question": f"x ≡ {r1} (mod {m1}), x ≡ {r2} (mod {m2}); find x (0 ≤ x < {m})",
180
+ "answer": x,
181
+ }
182
+
183
+ _TOPIC_GENERATORS = {
184
+ "legendre": _generate_legendre_problem,
185
+ "crt": _generate_crt_problem,
186
+ }
187
+
188
+ def generate_problem(topic: str, rng: random.Random) -> dict:
189
+ if topic == "mixed":
190
+ topic = rng.choice(list(_TOPIC_GENERATORS.keys()))
191
+ if topic not in _TOPIC_GENERATORS:
192
+ raise ValueError(f"Unknown challenge topic: {topic}")
193
+ return _TOPIC_GENERATORS[topic](rng)
194
+
153
195
  # ------------------- CLI -------------------
154
196
 
155
197
  def get_command():
@@ -213,6 +255,14 @@ def main():
213
255
  cf_cmd.add_argument("--den", type=int, required=True, help="Denominator")
214
256
  cf_cmd.add_argument("--terms", type=int, default=100, help="Max terms to expand")
215
257
 
258
+ # Challenge / quiz mode
259
+ challenge = subparsers.add_parser("challenge", help="Practice problems (Legendre, CRT)")
260
+ challenge.add_argument("--topic", choices=["legendre", "crt", "mixed"], default="mixed")
261
+ challenge.add_argument("--count", type=int, default=5, help="Number of problems")
262
+ challenge.add_argument("--seed", type=int, help="Random seed for reproducible problem sets")
263
+ challenge.add_argument("--reveal", action="store_true",
264
+ help="Print problems with answers, no prompting (worksheet mode)")
265
+
216
266
  args = parser.parse_args(argv)
217
267
 
218
268
  result = {"tool": "foxmath"}
@@ -285,6 +335,48 @@ def main():
285
335
  for p, q in convs:
286
336
  print(f" {p}/{q}")
287
337
 
338
+ elif cmd == "challenge" or args.command == "challenge":
339
+ if args.count < 1:
340
+ raise ValueError(f"--count must be >= 1 (got {args.count})")
341
+
342
+ rng = random.Random(args.seed)
343
+ problems = [generate_problem(args.topic, rng) for _ in range(args.count)]
344
+
345
+ if args.reveal:
346
+ result.update({"command": "challenge", "topic": args.topic, "problems": problems})
347
+ if not json_output:
348
+ for i, prob in enumerate(problems, 1):
349
+ print(f"{i}. {prob['question']}")
350
+ print(f" Answer: {prob['answer']}")
351
+ else:
352
+ score = 0
353
+ details = []
354
+ for i, prob in enumerate(problems, 1):
355
+ if not json_output:
356
+ print(f"{i}. {prob['question']}")
357
+ try:
358
+ raw = input(" Your answer: ")
359
+ except EOFError:
360
+ raw = ""
361
+ try:
362
+ user_answer = int(raw.strip())
363
+ except ValueError:
364
+ user_answer = None
365
+ correct = user_answer == prob["answer"]
366
+ score += correct
367
+ details.append({**prob, "your_answer": user_answer, "correct": correct})
368
+ if not json_output:
369
+ if correct:
370
+ print(" ✓ Correct!")
371
+ else:
372
+ print(f" ✗ Incorrect (answer: {prob['answer']})")
373
+ result.update({
374
+ "command": "challenge", "topic": args.topic,
375
+ "score": score, "total": args.count, "details": details,
376
+ })
377
+ if not json_output:
378
+ print(f"\nScore: {score}/{args.count}")
379
+
288
380
  except Exception as e:
289
381
  print(f"Error: {e}", file=sys.stderr)
290
382
  sys.exit(1)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: foxmath
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Minimalist CLI for math & cryptography exploration 🦊
5
5
  Author-email: Abhrankan Chakrabarti <abhrankan@gmail.com>
6
6
  License: MIT
@@ -32,9 +32,11 @@ Built with curiosity — for students, tinkerers, and crypto/math enthusiasts.
32
32
  named curves (secp256k1) with on-curve result validation
33
33
  - Chinese Remainder Theorem solver (handles non-coprime moduli)
34
34
  - Continued fraction expansion and convergents
35
- - Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd, foxmath-crt, foxmath-cf)
35
+ - Challenge/quiz mode — practice problems with scoring, or a
36
+ worksheet mode for self-study
37
+ - Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd,
38
+ foxmath-crt, foxmath-cf, foxmath-challenge)
36
39
  - JSON output mode
37
- - Coming soon: challenge mode
38
40
 
39
41
  ## Installation
40
42
 
@@ -101,6 +103,20 @@ foxmath cf --num 355 --den 113
101
103
  355/113
102
104
  ```
103
105
 
106
+ ```bash
107
+ # Challenge/quiz mode — worksheet (no prompting)
108
+ foxmath challenge --topic legendre --count 2 --seed 1 --reveal
109
+ ```
110
+ ```
111
+ 1. Legendre symbol (10/13)
112
+ Answer: 1
113
+ 2. Legendre symbol (3/7)
114
+ Answer: -1
115
+ ```
116
+
117
+ Drop `--reveal` to answer interactively instead, with scoring at the end.
118
+ `--topic` accepts `legendre`, `crt`, or `mixed` (default).
119
+
104
120
  ```bash
105
121
  # JSON output
106
122
  foxmath pi --terms 100 --json
@@ -1,6 +1,7 @@
1
1
  [console_scripts]
2
2
  foxmath = foxmath.foxmath:main
3
3
  foxmath-cf = foxmath.foxmath:main
4
+ foxmath-challenge = foxmath.foxmath:main
4
5
  foxmath-crt = foxmath.foxmath:main
5
6
  foxmath-ecadd = foxmath.foxmath:main
6
7
  foxmath-legendre = foxmath.foxmath:main
@@ -13,10 +13,10 @@ ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src", "fox
13
13
  SCRIPT = os.path.join(ROOT, "foxmath.py")
14
14
 
15
15
 
16
- def run(*args, script=SCRIPT):
16
+ def run(*args, script=SCRIPT, stdin_input=None):
17
17
  result = subprocess.run(
18
18
  [sys.executable, script, *args],
19
- capture_output=True, text=True, timeout=15,
19
+ capture_output=True, text=True, timeout=15, input=stdin_input,
20
20
  )
21
21
  return result.returncode, result.stdout, result.stderr
22
22
 
@@ -91,6 +91,13 @@ class TestSymlinkInvocation(unittest.TestCase):
91
91
  self.assertEqual(code, 0)
92
92
  self.assertIn("[3; 7, 16]", out)
93
93
 
94
+ def test_challenge_symlink(self):
95
+ script = self._make_symlink_script("foxmath-challenge")
96
+ code, out, err = run("--topic", "legendre", "--count", "2", "--seed", "1",
97
+ "--reveal", script=script)
98
+ self.assertEqual(code, 0)
99
+ self.assertIn("Answer:", out)
100
+
94
101
  def test_symlink_with_json(self):
95
102
  script = self._make_symlink_script("foxmath-legendre")
96
103
  code, out, err = run("2", "7", "--json", script=script)
@@ -170,6 +177,46 @@ class TestNormalUsage(unittest.TestCase):
170
177
  self.assertEqual(code, 1)
171
178
  self.assertIn("Error", err)
172
179
 
180
+ def test_challenge_reveal_is_deterministic_with_seed(self):
181
+ code1, out1, err1 = run("challenge", "--topic", "legendre", "--count", "3",
182
+ "--seed", "42", "--reveal")
183
+ code2, out2, err2 = run("challenge", "--topic", "legendre", "--count", "3",
184
+ "--seed", "42", "--reveal")
185
+ self.assertEqual(code1, 0)
186
+ self.assertEqual(out1, out2)
187
+
188
+ def test_challenge_interactive_scores_correct_answers(self):
189
+ # Known answers for --topic legendre --seed 42 --count 3 (from reveal mode).
190
+ code, out, err = run("challenge", "--topic", "legendre", "--count", "3",
191
+ "--seed", "42", stdin_input="-1\n-1\n1\n")
192
+ self.assertEqual(code, 0)
193
+ self.assertIn("Score: 3/3", out)
194
+
195
+ def test_challenge_interactive_scores_wrong_answers(self):
196
+ code, out, err = run("challenge", "--topic", "legendre", "--count", "3",
197
+ "--seed", "42", stdin_input="0\n0\n0\n")
198
+ self.assertEqual(code, 0)
199
+ self.assertIn("Score: 0/3", out)
200
+
201
+ def test_challenge_interactive_handles_non_numeric_input(self):
202
+ code, out, err = run("challenge", "--topic", "legendre", "--count", "1",
203
+ "--seed", "42", stdin_input="not-a-number\n")
204
+ self.assertEqual(code, 0)
205
+ self.assertIn("Score: 0/1", out)
206
+
207
+ def test_challenge_reveal_json(self):
208
+ code, out, err = run("challenge", "--topic", "crt", "--count", "2",
209
+ "--seed", "1", "--reveal", "--json")
210
+ self.assertEqual(code, 0)
211
+ import json
212
+ parsed = json.loads(out)
213
+ self.assertEqual(len(parsed["problems"]), 2)
214
+
215
+ def test_challenge_zero_count_errors(self):
216
+ code, out, err = run("challenge", "--count", "0")
217
+ self.assertEqual(code, 1)
218
+ self.assertIn("Error", err)
219
+
173
220
 
174
221
  if __name__ == "__main__":
175
222
  unittest.main()
@@ -15,7 +15,9 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..",
15
15
  from foxmath.foxmath import (
16
16
  legendre_symbol, euler_hermann_pi_approx, ec_point_add, crt_solve,
17
17
  continued_fraction, cf_convergents, CURVES, is_on_curve,
18
+ generate_problem,
18
19
  )
20
+ import random
19
21
 
20
22
  # Reference pi digits (mpmath, 250 dps of working precision), "3" + fractional digits.
21
23
  PI_REFERENCE = Decimal(
@@ -175,5 +177,36 @@ class TestNamedCurves(unittest.TestCase):
175
177
  self.assertTrue(is_on_curve(c["gx"], c["gy"], c["a"], c["b"], c["p"]))
176
178
 
177
179
 
180
+ class TestChallengeGenerators(unittest.TestCase):
181
+ def test_legendre_problem_answer_is_correct(self):
182
+ rng = random.Random(0)
183
+ for _ in range(50):
184
+ prob = generate_problem("legendre", rng)
185
+ self.assertEqual(prob["topic"], "legendre")
186
+ # re-derive the answer independently from the question text
187
+ self.assertIn(prob["answer"], (-1, 0, 1))
188
+
189
+ def test_crt_problem_answer_satisfies_both_congruences(self):
190
+ rng = random.Random(0)
191
+ for _ in range(50):
192
+ prob = generate_problem("crt", rng)
193
+ self.assertEqual(prob["topic"], "crt")
194
+ self.assertIsInstance(prob["answer"], int)
195
+
196
+ def test_mixed_topic_produces_both_kinds(self):
197
+ rng = random.Random(1)
198
+ topics = {generate_problem("mixed", rng)["topic"] for _ in range(30)}
199
+ self.assertEqual(topics, {"legendre", "crt"})
200
+
201
+ def test_reproducible_with_same_seed(self):
202
+ p1 = generate_problem("legendre", random.Random(99))
203
+ p2 = generate_problem("legendre", random.Random(99))
204
+ self.assertEqual(p1, p2)
205
+
206
+ def test_unknown_topic_raises(self):
207
+ with self.assertRaises(ValueError):
208
+ generate_problem("not_a_topic", random.Random(0))
209
+
210
+
178
211
  if __name__ == "__main__":
179
212
  unittest.main()
File without changes
File without changes
File without changes