数学常用算法

数学常用算法

一、矩阵乘法

1.1 算法原理

矩阵乘法遵循A[m×n] * B[n×p] = C[m×p]规则,每个元素计算为:

C[i][j] = Σ(A[i][k] * B[k][j]), k=0→n-1

1.2 Java实现

public class MatrixOperations {
    // 矩阵乘法
    public static int[][] multiply(int[][] A, int[][] B) {
        int m = A.length, n = A[0].length, p = B[0].length;
        if (B.length != n) throw new IllegalArgumentException("矩阵维度不匹配");
        
        int[][] C = new int[m][p];
        for (int i = 0; i < m; i++)
            for (int j = 0; j < p; j++)
                for (int k = 0; k < n; k++)
                    C[i][j] += A[i][k] * B[k][j];
        return C;
    }
}

1.3 复杂度分析

  • 时间复杂度:O(m×n×p)
  • 空间复杂度:O(m×p)

二、数值计算基础

2.1 向上取整除法

public static int ceilDivide(int a, int b) {
    if (b == 0) throw new ArithmeticException("除数不能为零");
    return (a + b - 1) / b; // 适用于正数
}

2.2 同余判断

public static boolean isCongruent(int a, int b, int m) {
    return (a - b) % m == 0; // 改进版:Math.floorMod(a-b, m) == 0
}

三、数论核心算法

3.1 最大公约数(GCD)

// 欧几里得算法迭代版
public static int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return Math.abs(a);
}

3.2 最小公倍数(LCM)

public static int lcm(int a, int b) {
    if (a == 0 || b == 0) return 0;
    return Math.abs(a * b) / gcd(a, b);
}

四、快速幂算法

4.1 迭代实现

public static long fastPower(long base, long exponent) {
    long result = 1;
    while (exponent > 0) {
        if ((exponent & 1) == 1)
            result *= base;
        base *= base;
        exponent >>= 1;
    }
    return result;
}

// 带模数版本
public static long modPow(long base, long exponent, long mod) {
    base %= mod;
    long result = 1;
    while (exponent > 0) {
        if ((exponent & 1) != 0)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exponent >>= 1;
    }
    return result;
}

五、素数相关算法

5.1 素数判定

public static boolean isPrime(int n) {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    
    for (int i = 5; i*i <= n; i += 6)
        if (n % i == 0 || n % (i+2) == 0)
            return false;
    return true;
}

5.2 埃拉托斯特尼筛法

public static boolean[] sieve(int max) {
    boolean[] flags = new boolean[max+1];
    Arrays.fill(flags, true);
    flags[0] = flags[1] = false;
    
    for (int i = 2; i*i <= max; i++)
        if (flags[i])
            for (int j = i*i; j <= max; j += i)
                flags[j] = false;
    return flags;
}

六、斐波那契数列

6.1 动态规划实现

public static long fibonacciDP(int n) {
    if (n <= 1) return n;
    long[] dp = new long[n+1];
    dp[1] = 1;
    for (int i = 2; i <= n; i++)
        dp[i] = dp[i-1] + dp[i-2];
    return dp[n];
}

6.2 矩阵快速幂优化

public static long fibMatrix(int n) {
    if (n <= 1) return n;
    long[][] matrix = {{1,1}, {1,0}};
    long[][] result = matrixPower(matrix, n-1);
    return result[0][0];
}

private static long[][] matrixPower(long[][] m, int power) {
    long[][] result = {{1,0}, {0,1}}; // 单位矩阵
    while (power > 0) {
        if ((power & 1) == 1)
            result = multiplyMatrix(result, m);
        m = multiplyMatrix(m, m);
        power >>= 1;
    }
    return result;
}

private static long[][] multiplyMatrix(long[][] a, long[][] b) {
    long[][] res = new long[2][2];
    res[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0];
    res[0][1] = a[0][0]*b[0][1] + a[0][1]*b[1][1];
    res[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0];
    res[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1];
    return res;
}

七、组合数学算法

7.1 排列数计算

public static long permutation(int n, int k) {
    if (k > n) return 0;
    long result = 1;
    for (int i = 0; i < k; i++)
        result *= (n - i);
    return result;
}

7.2 组合数计算

public static long combination(int n, int k) {
    if (k > n/2) k = n - k; // 优化计算量
    long result = 1;
    for (int i = 1; i <= k; i++)
        result = result * (n - k + i) / i;
    return result;
}

八、扩展欧几里得算法

8.1 算法实现

public static class ExtendedEuclidResult {
    int gcd;
    int x;
    int y;
}

public static ExtendedEuclidResult extendedEuclid(int a, int b) {
    ExtendedEuclidResult result = new ExtendedEuclidResult();
    if (b == 0) {
        result.gcd = a;
        result.x = 1;
        result.y = 0;
    } else {
        ExtendedEuclidResult temp = extendedEuclid(b, a % b);
        result.gcd = temp.gcd;
        result.x = temp.y;
        result.y = temp.x - (a / b) * temp.y;
    }
    return result;
}

九、应用场景分析

9.1 密码学应用

  • RSA加密:依赖大素数生成和模幂运算
  • 椭圆曲线加密:使用扩展欧几里得算法求模逆元

9.2 算法竞赛应用

  • 快速幂:解决大数取模问题
  • 筛法:快速处理质数相关问题
  • 矩阵快速幂:优化递推关系计算

9.3 工程实践

  • 组合计算:推荐系统相似度计算
  • 斐波那契数列:金融模型预测
  • 矩阵运算:图形变换处理

十、性能优化技巧

10.1 记忆化技术

private static Map<Integer, Long> fibCache = new HashMap<>();

public static long fibonacciMemo(int n) {
    if (n <= 1) return n;
    if (fibCache.containsKey(n))
        return fibCache.get(n);
    long res = fibonacciMemo(n-1) + fibonacciMemo(n-2);
    fibCache.put(n, res);
    return res;
}

10.2 位运算优化

// 快速判断奇偶性
public static boolean isEven(int n) {
    return (n & 1) == 0;
}

// 快速乘2
public static int fastMultiplyTwo(int n) {
    return n << 1;
}

十一、完整测试用例

11.1 矩阵乘法测试

int[][] A = {{1,2}, {3,4}};
int[][] B = {{5,6}, {7,8}};
int[][] C = MatrixOperations.multiply(A, B);
// 结果应为[[19,22],[43,50]]

11.2 GCD/LCM测试

assert gcd(48, 18) == 6;
assert lcm(12, 18) == 36;

11.3 快速幂测试

assert fastPower(2, 10) == 1024;
assert modPow(3, 5, 7) == 5; // 3^5=243 ≡ 243%7=5
posted @ 2025-03-06 14:59  咋还没来  阅读(56)  评论(0)    收藏  举报