You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻找满足特定对称四次方等式的正整数n,k,x的方法及代码优化问询

寻找满足特定对称四次方等式的正整数n,k,x的方法及代码优化问询

问题背景

我正在研究一个结构对称的数学恒等式,它类似四次方的Taxicab问题,将原本四变量的搜索空间压缩到了三个正整数变量 (k, n, x)。我的核心需求是找到这样的 (k, n, x),使得等式中的四个项均为完美四次方,且等式严格成立。

目标恒等式

\left( -\frac{1}{2} \cdot \frac{((\frac{1}{3})^{1/3} \cdot n)^2}{\left( 3n^3 - \frac{3(n^4 - x)}{n} + \frac{\sqrt{\frac{1}{3}n^8 + 9x^2}}{n} \right)^{1/3}} + \frac{1}{2}n + \frac{1}{2} \cdot (\frac{1}{3})^{1/3} \cdot \left( 3n^3 - \frac{3(n^4 - x)}{n} + \frac{\sqrt{\frac{1}{3}n^8 + 9x^2}}{n} \right)^{1/3} \right)^4 + \left( -\frac{1}{2} \cdot \frac{(\frac{1}{3})^{2/3} \cdot k^2}{\left( 3k^3 - \frac{3(k^4 - x)}{k} + \frac{\sqrt{\frac{1}{3}k^8 + 9x^2}}{k} \right)^{1/3}} - \frac{1}{2}k + \frac{1}{2} \cdot (\frac{1}{3})^{1/3} \cdot \left( 3k^3 - \frac{3(k^4 - x)}{k} + \frac{\sqrt{\frac{1}{3}k^8 + 9x^2}}{k} \right)^{1/3} \right)^4 = \left( -\frac{1}{2} \cdot \frac{((\frac{1}{3})^{1/3} \cdot n)^2}{\left( 3n^3 - \frac{3(n^4 - x)}{n} + \frac{\sqrt{\frac{1}{3}n^8 + 9x^2}}{n} \right)^{1/3}} - \frac{1}{2}n + \frac{1}{2} \cdot (\frac{1}{3})^{1/3} \cdot \left( 3n^3 - \frac{3(n^4 - x)}{n} + \frac{\sqrt{\frac{1}{3}n^8 + 9x^2}}{n} \right)^{1/3} \right)^4 + \left( -\frac{1}{2} \cdot \frac{(\frac{1}{3})^{2/3} \cdot k^2}{\left( 3k^3 - \frac{3(k^4 - x)}{k} + \frac{\sqrt{\frac{1}{3}k^8 + 9x^2}}{k} \right)^{1/3}} + \frac{1}{2}k + \frac{1}{2} \cdot (\frac{1}{3})^{1/3} \cdot \left( 3k^3 - \frac{3(k^4 - x)}{k} + \frac{\sqrt{\frac{1}{3}k^8 + 9x^2}}{k} \right)^{1/3} \right)^4

已知有效解

当 (n = 24, k = 74, x = 300783360) 时,等式可简化为:
$$158^4 + 59^4 = 134^4 + 133^4$$
此时四个项的四次方根均为正整数,完全符合所有要求。

现有代码与问题

我编写了初步的Python代码用于搜索解,但存在明显缺陷:

  1. 仅基于浮点数容差判断等式是否成立,未验证四个项是否为完美四次方
  2. 默认搜索范围过小,已知解的 (x) 值远大于默认上限
  3. 浮点数精度误差可能导致对完美四次方的误判

原始代码

import math
import cmath

# Define cube roots as constants
cbrt_1_3 = (1 / 3) ** (1 / 3)
cbrt_1_3_sq = (1 / 3) ** (2 / 3)

def cbrt(x):
    """Calculate cube root, handling negative numbers"""
    if x >= 0:
        return x ** (1 / 3)
    else:
        return -(-x) ** (1 / 3)

def term_inner(var, x):
    """Calculate the inner expression for the cube root"""
    return 3 * var ** 3 - 3 * (var ** 4 - x) / var + math.sqrt((1 / 3) * var ** 8 + 9 * x ** 2) / var

def lhs_expr(n, k, x):
    """Left-hand side of the equation"""
    # Calculate the inner expressions
    inner_n = term_inner(n, x)
    inner_k = term_inner(k, x)
    
    # Calculate cube roots
    Rn = cbrt(inner_n)
    Rk = cbrt(inner_k)
    
    # First term
    A = (-0.5 * (cbrt_1_3 * n) ** 2 / Rn + 0.5 * n + 0.5 * cbrt_1_3 * Rn) ** 4
    # Second term - Fixed: using cbrt_1_3_sq as per original equation
    B = (-0.5 * cbrt_1_3_sq * k ** 2 / Rk - 0.5 * k + 0.5 * cbrt_1_3 * Rk) ** 4
    return A + B

def rhs_expr(n, k, x):
    """Right-hand side of the equation"""
    # Calculate the inner expressions
    inner_n = term_inner(n, x)
    inner_k = term_inner(k, x)
    
    # Calculate cube roots
    Rn = cbrt(inner_n)
    Rk = cbrt(inner_k)
    
    # First term - sign change on n term
    C = (-0.5 * (cbrt_1_3 * n) ** 2 / Rn - 0.5 * n + 0.5 * cbrt_1_3 * Rn) ** 4
    # Second term - Fixed: using cbrt_1_3_sq as per original equation
    D = (-0.5 * cbrt_1_3_sq * k ** 2 / Rk + 0.5 * k + 0.5 * cbrt_1_3 * Rk) ** 4
    return C + D

def find_integer_solution(n_max=20, k_max=20, x_max=100, tolerance=1e-8):
    """Find integer solutions to the equation where n ≠ k ≠ x"""
    print(f"Searching for solutions with n ≤ {n_max}, k ≤ {k_max}, x ≤ {x_max}")
    print("Constraint: n ≠ k ≠ x (all three values must be different)")
    solutions = []
    for n in range(1, n_max + 1):
        for k in range(1, k_max + 1):
            for x in range(1, x_max + 1):
                # Skip if any two values are equal
                if n == k or n == x or k == x:
                    continue
                try:
                    # Calculate LHS and RHS
                    lhs = lhs_expr(n, k, x)
                    rhs = rhs_expr(n, k, x)
                    
                    # Check if values are real and close
                    if isinstance(lhs, complex) or isinstance(rhs, complex):
                        continue
                    error = abs(lhs - rhs)
                    if error < tolerance:
                        solution = {
                            'n': n,
                            'k': k,
                            'x': x,
                            'LHS': lhs,
                            'RHS': rhs,
                            'error': error
                        }
                        solutions.append(solution)
                        print(f"✅ Solution found: n={n}, k={k}, x={x}, error={error:.2e}")
                except (ZeroDivisionError, ValueError, TypeError) as e:
                    continue
                except Exception as e:
                    # Skip any other computational errors
                    continue
    return solutions

def verify_solution(n, k, x):
    """Verify a specific solution"""
    print(f"\nVerifying solution: n={n}, k={k}, x={x}")
    try:
        lhs = lhs_expr(n, k, x)
        rhs = rhs_expr(n, k, x)
        print(f"LHS value: {lhs}")
        print(f"RHS value: {rhs}")
        print(f"Difference: {abs(lhs - rhs)}")
        return abs(lhs - rhs) < 1e-8
    except Exception as e:
        print(f"Error verifying solution: {e}")
        return False

优化需求

我希望对代码进行如下优化:

  1. 增加对四个项(A、B、C、D)是否为完美四次方的精确判断(避免浮点数精度干扰)
  2. 设计更高效的搜索策略,支持更大范围的 (n, k, x) 搜索
  3. 确保找到的解严格满足所有条件:等式精确成立,四个项均为正整数的四次方

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 03:13:03