"""Matematik testinin cevap anahtarı: her soru KABA KUVVETLE hesaplanır (cevap uydurulmaz).
    python3 projects/argon-kapsamli/matematik/dogrula.py  -> cevaplar.json
"""
import itertools, json, math, pathlib
from fractions import Fraction

def asal(n):
    if n < 2: return False
    for p in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37):
        if n % p == 0: return n == p
    d, s = n - 1, 0
    while d % 2 == 0: d //= 2; s += 1
    for a in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37):
        x = pow(a, d, n)
        if x in (1, n - 1): continue
        for _ in range(s - 1):
            x = x * x % n
            if x == n - 1: break
        else: return False
    return True

C = {}
# 1
C[1] = sum(1 for n in range(1, 100001) if not asal(n * n + n + 41))
# 2
C[2] = sum(sum(map(int, str(k))) for k in range(1, 2027))
# 3 merdiven: 1/2/3 adım, art arda iki 3 yok, 20 basamak
from functools import lru_cache
@lru_cache(None)
def m(k, son3):
    if k == 0: return 1
    t = 0
    for a in (1, 2, 3):
        if a <= k and not (a == 3 and son3): t += m(k - a, a == 3)
    return t
C[3] = m(20, False)
# 4 torba: 5K 7M 3Y, geri koymadan 6 top, her renkten en az bir
top = ["K"] * 5 + ["M"] * 7 + ["Y"] * 3
iyi = sum(1 for c in itertools.combinations(range(15), 6) if {top[i] for i in c} == {"K", "M", "Y"})
p = Fraction(iyi, math.comb(15, 6)); C[4] = p.numerator + p.denominator; C["4_kesir"] = f"{p.numerator}/{p.denominator}"
# 5 en küçük n>1: 2^n ≡ n (mod 1009)
C[5] = next(n for n in range(2, 10**7) if pow(2, n, 1009) == n % 1009)
# 6 ızgara (0,0)->(9,9) sağ/yukarı, (3,3) (5,6) (7,2) noktalarından geçmeden
yasak = {(3, 3), (5, 6), (7, 2)}
yol = [[0] * 10 for _ in range(10)]
for x in range(10):
    for y in range(10):
        if (x, y) in yasak: continue
        yol[x][y] = 1 if x == y == 0 else (yol[x - 1][y] if x else 0) + (yol[x][y - 1] if y else 0)
C[6] = yol[9][9]
# 7 x^3 - 7x + 3 = 0 kökleri a,b,c: a^7+b^7+c^7 (Newton)
e1, e2, e3 = 0, -7, -3          # x^3 - e1 x^2 + e2 x - e3: e1=0, e2=-7, e3=-3
P = [3, e1, e1 * e1 - 2 * e2]
for k in range(3, 8): P.append(e1 * P[k - 1] - e2 * P[k - 2] + e3 * P[k - 3])
C[7] = P[7]
# 8 x^2 + y^2 < 2026 tam sayı noktaları
C[8] = sum(1 for x in range(-46, 47) for y in range(-46, 47) if x * x + y * y < 2026)
# 9 1..9 permütasyonları, hiçbir i için |p(i) - i| = 1 olmayan
C[9] = sum(1 for q in itertools.permutations(range(1, 10)) if all(abs(q[i] - (i + 1)) != 1 for i in range(9)))
# 10 rakamları çarpımı 720 olan 4 basamaklı sayılar
C[10] = sum(1 for k in range(1000, 10000) if math.prod(map(int, str(k))) == 720)
pathlib.Path(__file__).with_name("cevaplar.json").write_text(json.dumps(C, ensure_ascii=False, indent=1))
print(C)
