#include #include #include #include #include #include #include using u64 = uint64_t; using u128 = unsigned __int128; static std::string to_str(u128 v) { if (v == 0) return "0"; std::string s; while (v > 0) { s.push_back('0' + (int)(v % 10)); v /= 10; } std::reverse(s.begin(), s.end()); return s; } static u128 gcd128(u128 a, u128 b) { while (b) { u128 t = a % b; a = b; b = t; } return a; } static u128 mul_mod(u128 a, u128 b, u128 mod) { return (a * b) % mod; } static u128 pow_mod(u128 base, u128 exp, u128 mod) { u128 res = 1; base %= mod; while (exp > 0) { if (exp & 1) res = (res * base) % mod; base = (base * base) % mod; exp >>= 1; } return res; } static bool is_prime(u128 n) { if (n < 2) return false; static const u64 small_primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; for (u64 p : small_primes) { if (n == p) return true; if (n % p == 0) return false; } u128 d = n - 1; int s = 0; while ((d & 1) == 0) { d >>= 1; ++s; } for (u64 a : small_primes) { if (a >= n) break; u128 x = pow_mod(a, d, n); if (x == 1 || x == n - 1) continue; bool composite = true; for (int r = 1; r < s; ++r) { x = (x * x) % n; if (x == n - 1) { composite = false; break; } } if (composite) return false; } return true; } struct Factor { u64 p; int e; }; static std::vector sieve_primes(u64 limit) { std::vector is_p(limit + 1, true); std::vector primes; for (u64 i = 2; i <= limit; ++i) { if (is_p[i]) { primes.push_back(i); for (u64 j = i * i; j <= limit; j += i) is_p[j] = false; } } return primes; } static std::vector get_divisors(const std::vector& fac) { std::vector divs = {1}; for (auto [p, e] : fac) { size_t sz = divs.size(); u128 pe = 1; for (int k = 1; k <= e; ++k) { pe *= p; for (size_t i = 0; i < sz; ++i) { divs.push_back(divs[i] * pe); } } } std::sort(divs.begin(), divs.end()); return divs; } // Bitset up to 2048 (32 x u64) struct Bitset2048 { std::array w{}; void reset() { w.fill(0); w[0] = 1; } void add(u64 shift) { if (shift >= 2048) return; int ws = shift >> 6; int bs = shift & 63; if (bs == 0) { for (int i = 31; i >= ws; --i) w[i] |= w[i - ws]; } else { for (int i = 31; i > ws; --i) { w[i] |= (w[i - ws] << bs) | (w[i - ws - 1] >> (64 - bs)); } w[ws] |= (w[0] << bs); } } bool test(u64 bit) const { if (bit >= 2048) return false; return (w[bit >> 6] >> (bit & 63)) & 1ULL; } }; static bool contains_p(const std::vector& fac, u64 p) { for (auto& f : fac) if (f.p == p) return true; return false; } // Fast pseudoperfect check using small-divisor bitset (up to 2048) + greedy + bounded DFS static bool fast_is_pseudoperfect(const std::vector& fac, u128 n, u128 sig) { u128 delta = sig - 2 * n; if (delta == 0) return true; auto divs = get_divisors(fac); divs.pop_back(); // proper divisors u128 target = std::min(n, delta); Bitset2048 bs; bs.reset(); std::vector large; for (u128 d : divs) { if (d > target) break; if (d < 2048) bs.add((u64)d); else large.push_back(d); } if (target < 2048) return bs.test((u64)target); u128 rem = target; for (int i = (int)large.size() - 1; i >= 0; --i) { if (large[i] <= rem) { rem -= large[i]; if (rem < 2048 && bs.test((u64)rem)) return true; } } if (rem < 2048 && bs.test((u64)rem)) return true; // Exact DFS fallback std::vector all; for (u128 d : divs) if (d <= target) all.push_back(d); std::reverse(all.begin(), all.end()); std::vector suf(all.size() + 1, 0); for (int i = (int)all.size() - 1; i >= 0; --i) suf[i] = suf[i + 1] + all[i]; auto dfs = [&](auto& self, size_t idx, u128 r) -> bool { if (r == 0) return true; if (r < 2048 && bs.test((u64)r)) return true; if (idx >= all.size() || suf[idx] < r) return false; if (suf[idx] == r) return true; if (all[idx] <= r && self(self, idx + 1, r - all[idx])) return true; return self(self, idx + 1, r); }; return dfs(dfs, 0, target); } // 1. Search odd deficient m (w in 4..6) with small D/m, check n = m*p and n = m*p*q static void search_tiny_deficiency(const std::vector& odd_primes) { auto t0 = std::chrono::steady_clock::now(); u64 m_checked = 0; u64 mp_hits = 0; u64 mpq_p_hits = 0; u64 mpq_q_hits = 0; u64 min_D = UINT64_MAX; u128 min_D_m = 0; std::vector fac; auto dfs = [&](auto& self, size_t idx, int w, u128 m, u128 sig) -> void { if (w >= 4) { u128 D = 2 * m - sig; // Check ratio >= 1.98 (i.e. 100*sig >= 198*m) if (D > 0 && sig * 100 >= m * 198) { ++m_checked; if (D < min_D) { min_D = (u64)D; min_D_m = m; } // Build bitset of divs(m) up to 2048 auto divs = get_divisors(fac); Bitset2048 bs; bs.reset(); for (u128 d : divs) { if (d < 2048) bs.add((u64)d); else break; } // Check n = m * p for even gaps g in [2, 2046] u64 g_max = (u64)std::min(2046, sig / D); for (u64 g = 2; g <= g_max; g += 2) { if (!bs.test(g) && (sig - g) % D == 0) { u128 p = (sig - g) / D; if (p > g && (p > UINT64_MAX || !contains_p(fac, (u64)p)) && is_prime(p)) { ++mp_hits; std::cout << "*** MP HIT: m=" << to_str(m) << " p=" << to_str(p) << " delta=" << g << "\n"; } } } // Check n = m * p * q with small even c in [2, 200] u128 gmd = gcd128(m, D); if (sig * 1000 >= m * 1995) { for (u64 c = 2; c <= 200; c += 2) { if (c % gmd != 0) continue; if ((sig + c) % D != 0) continue; u128 p = (sig + c) / D; if (p <= 2 || (p <= UINT64_MAX && contains_p(fac, (u64)p)) || !is_prime(p)) continue; ++mpq_p_hits; u128 sig_mp = sig * (p + 1); // Check g = 2 (always a gap!) and small gaps of divs(mp) if ((sig_mp - 2) % c == 0) { u128 q = (sig_mp - 2) / c; if (q > 2 && q != p && (q > UINT64_MAX || !contains_p(fac, (u64)q)) && is_prime(q)) { ++mpq_q_hits; std::cout << "*** MPQ WEIRD (delta=2): m=" << to_str(m) << " p=" << to_str(p) << " q=" << to_str(q) << "\n"; } } } } } } if (w >= 6) return; for (size_t i = idx; i < odd_primes.size(); ++i) { u64 p = odd_primes[i]; if (p > 150 && w < 4) break; if (p > 250) break; int max_e = (p <= 7) ? 4 : ((p <= 19) ? 2 : 1); u128 pe = 1, sig_pe = 1; for (int e = 1; e <= max_e; ++e) { pe *= p; sig_pe += pe; u128 nm = m * pe; if (nm > (u128)1000000000000000ULL) break; // 10^15 u128 nsig = sig * sig_pe; if (nsig >= 2 * nm) break; fac.push_back({p, e}); self(self, i + 1, w + 1, nm, nsig); fac.pop_back(); } } }; dfs(dfs, 0, 0, 1, 1); double dt = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); std::cout << "[Tiny-D Search] checked=" << m_checked << " m (w=4..6, sig/m>=1.98, m<=1e15) in " << dt << "s | min_D=" << min_D << " (at m=" << to_str(min_D_m) << ")" << " | mp_hits=" << mp_hits << ", mpq_p_hits=" << mpq_p_hits << ", mpq_q_hits=" << mpq_q_hits << "\n"; } // 2. Search squarefree omega=7 primitive odd abundant numbers static void search_sqfree_w7(const std::vector& odd_primes, u64 max_p, u128 max_n) { auto t0 = std::chrono::steady_clock::now(); u64 found = 0, weird = 0; std::vector fac; // Precompute upper bound products std::vector ps; for (u64 p : odd_primes) if (p <= max_p) ps.push_back(p); auto dfs = [&](auto& self, size_t idx, int w, u128 n, u128 sig) -> void { if (w == 7) { if (sig > 2 * n) { // Check primitive abundant: removing smallest prime p0 or largest prime p6? // Actually removing ANY prime -> deficient iff removing p_max gives deficient? No, removing p_min gives smallest ratio! // Wait: removing p_max gives LARGEST remaining ratio (since (p+1)/p is decreasing in p)! // So primitive abundant iff removing p_6 (the largest prime) makes it deficient! u64 p_last = fac.back().p; u128 n_prev = n / p_last; u128 sig_prev = sig / (p_last + 1); if (sig_prev < 2 * n_prev) { ++found; if (!fast_is_pseudoperfect(fac, n, sig)) { ++weird; std::cout << "*** SQFREE W=7 WEIRD: n=" << to_str(n) << "\n"; } } } return; } int rem = 7 - w; if (idx + rem > ps.size()) return; for (size_t i = idx; i + rem <= ps.size(); ++i) { // Exact rational upper bound: sig * prod_{k=0}^{rem-1} (ps[i+k]+1) <= 2 * n * prod ps[i+k] u128 num = sig, den = n; for (int k = 0; k < rem; ++k) { num *= (ps[i + k] + 1); den *= ps[i + k]; } if (num <= 2 * den) break; u64 p = ps[i]; u128 nn = n * p; if (nn > max_n) break; u128 nsig = sig * (p + 1); if (w < 6 && nsig >= 2 * nn) continue; // not primitive at w=7 fac.push_back({p, 1}); self(self, i + 1, w + 1, nn, nsig); fac.pop_back(); } }; dfs(dfs, 0, 0, 1, 1); double dt = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); std::cout << "[Sqfree w=7] max_p=" << max_p << ", max_n=" << to_str(max_n) << ": primitive_abundant=" << found << ", weird=" << weird << " in " << dt << "s\n"; } // 3. Search large-gap odd deficient m (e.g. 5,7 not dividing m, or p_min >= 5) + n = m * p static void search_large_gap_mp(const std::vector& primes_pool, const std::string& label, int min_w, int max_w, int min_ratio_pct) { auto t0 = std::chrono::steady_clock::now(); u64 m_checked = 0, hits = 0, total_gaps_tested = 0; std::vector fac; auto dfs = [&](auto& self, size_t idx, int w, u128 m, u128 sig) -> void { if (w >= min_w) { u128 D = 2 * m - sig; if (D > 0 && sig * 100 >= m * (u128)min_ratio_pct) { ++m_checked; auto divs = get_divisors(fac); Bitset2048 bs; bs.reset(); for (u128 d : divs) { if (d < 2048) bs.add((u64)d); else break; } u64 g_max = (u64)std::min(2046, sig / D); for (u64 g = 2; g <= g_max; g += 2) { if (!bs.test(g)) { ++total_gaps_tested; if ((sig - g) % D == 0) { u128 p = (sig - g) / D; if (p > g && (p > UINT64_MAX || !contains_p(fac, (u64)p)) && is_prime(p)) { ++hits; std::cout << "*** LARGE-GAP MP HIT (" << label << "): m=" << to_str(m) << " p=" << to_str(p) << " delta=" << g << "\n"; } } } } } } if (w >= max_w) return; for (size_t i = idx; i < primes_pool.size(); ++i) { u64 p = primes_pool[i]; int max_e = (p <= 7) ? 3 : ((p <= 19) ? 2 : 1); u128 pe = 1, sig_pe = 1; for (int e = 1; e <= max_e; ++e) { pe *= p; sig_pe += pe; u128 nm = m * pe; if (nm > (u128)1000000000000000000ULL) break; // 10^18 u128 nsig = sig * sig_pe; if (nsig >= 2 * nm) break; fac.push_back({p, e}); self(self, i + 1, w + 1, nm, nsig); fac.pop_back(); } } }; dfs(dfs, 0, 0, 1, 1); double dt = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); std::cout << "[Large-Gap " << label << "] checked=" << m_checked << " m, gaps_tested=" << total_gaps_tested << ", hits=" << hits << " in " << dt << "s\n"; } int main() { auto all_primes = sieve_primes(1000); std::vector odd_primes; for (u64 p : all_primes) if (p >= 3) odd_primes.push_back(p); search_tiny_deficiency(odd_primes); search_sqfree_w7(odd_primes, 500, (u128)1000000000000000000ULL); // Sparse-small-prime pool: 3, then primes >= 13 up to 150 std::vector no5711 = {3}; for (u64 p : odd_primes) if (p >= 13 && p <= 150) no5711.push_back(p); search_large_gap_mp(no5711, "3_no5_7_11", 6, 8, 190); // p_min >= 5 pool up to 120 std::vector pge5; for (u64 p : odd_primes) if (p >= 5 && p <= 120) pge5.push_back(p); search_large_gap_mp(pge5, "p_min>=5", 7, 9, 195); return 0; }