void Solve() { ans = -1; long g = NumTheoryUtils.Gcd(p, q); p /= g; q /= g; long tt = q; while (tt % 2 == 0) { tt /= 2; } if (tt != 1) { return; } ans = 1; while (p < q / 2) { q /= 2; ++ans; } }
public void GcdTests() { for (int a = -100; a <= 100; ++a) { for (int b = -100; b <= 100; ++b) { int g = NumTheoryUtils.Gcd(a, b); int brute = 0; for (int i = Math.Max(Math.Abs(a), Math.Abs(b)); i > 0; --i) { if (a % i == 0 && b % i == 0) { brute = i; break; } } Assert.AreEqual(brute, g); } } }
public void EulerPhiMustWork() { for (int i = 1; i <= 1000; ++i) { int cnt = 0; for (int j = 1; j <= i; ++j) { if (NumTheoryUtils.Gcd(i, j) == 1) { ++cnt; } } Assert.AreEqual(cnt, NumTheoryUtils.EulerPhi(i)); } var phiArr = NumTheoryUtils.EulerPhiArray(100000); for (int i = 1; i < phiArr.Length; i++) { Assert.AreEqual(NumTheoryUtils.EulerPhi(i), phiArr[i]); } }