开方公式

本文介绍了一种处理大数平方根和立方根的算法实现,使用Java的BigInteger类进行高精度计算,适用于需要精确数学运算的场景。

平方根 y = n(20x+n)

立方根 y = n(30x(10x+n)+n^2)

x^8=x^2*^2*^2

x^6=x^2*^3

import java.math.BigInteger;

/**
 * @Description 大数开方 方法 平方 立方
 * @Author Joen
 * @Date 2020-08-11 11:44:39
 */
public class ExtractUtils {
    //TODO 偶数开方 n % 2 | n % 3 则n为2 3 的倍数 (素数 和 不能被2 3 整除的自然数不支持开)
    private static BigInteger MU2 = BigInteger.valueOf(20);
    private static BigInteger HU2 = BigInteger.TEN.pow(2);
    private static BigInteger MU3 = BigInteger.valueOf(300);
    private static BigInteger MU3_1 = BigInteger.valueOf(30);
    private static BigInteger HU3 = BigInteger.TEN.pow(3);

    /**
     * 立方 开方 公式 y = n(30x(10x+n)+n^2)
     * @param va1
     * @Author: Joen
     * @Date: 2020-08-11 11:45
     * @returns:
     */
    public static BigInteger extract3(BigInteger va1){
        String s = va1.toString();
        if (s.length() % 3 == 1){
            s = "00"+s;
        }
        if (s.length() % 3 == 2){
            s = "0"+s;
        }
        char[] chars = s.toCharArray();
        BigInteger result = BigInteger.ZERO,remainder = BigInteger.ZERO;
        BigInteger[] yu = new BigInteger[1];
        for (int i = 0;i < chars.length; i++){
            int[] next = new int[3];
            next[0] = chars[i++]-48;
            next[1] = chars[i++]-48;
            next[2] = chars[i]-48;
            remainder = remainder.multiply(HU3).add(BigInteger.valueOf((next[0]*100+next[1]*10+next[2])));
            BigInteger shang = f2(remainder, result,yu);
            result = result.multiply(BigInteger.TEN).add(shang);
            remainder = yu[0];
        }
        return result;
    }

    /**
     * 平方 开方 公式 y = n(20x+n)
     * @param va1
     * @Author: Joen
     * @Date: 2020-08-11 11:48
     * @returns:
     */
    public static BigInteger extract(BigInteger va1) {
        String s = va1.toString();
        if (s.length() % 2 == 1){
            s = "0"+s;
        }
        char[] chars = s.toCharArray();

        BigInteger result = BigInteger.ZERO,remainder = BigInteger.ZERO;
        BigInteger [] yu = new BigInteger[1];
        for (int i = 0; i < chars.length; i++) {
            int[] next = new int[2];
            next[0] = chars[i++]-48;
            next[1] = chars[i]-48;

            remainder = remainder.multiply(HU2).add(BigInteger.valueOf((next[0]*10+next[1])));
            BigInteger shang = f1(remainder, result,yu);
            result = result.multiply(BigInteger.TEN).add(shang);
            remainder = yu[0];
        }
        return result;
    }

    public static BigInteger f1(BigInteger remainder, BigInteger result,BigInteger[] yu) {
        for (int i = 9; i > 0; i--) {
            BigInteger value = BigInteger.valueOf(i);
            BigInteger r1 = result.multiply(MU2).add(value).multiply(value).subtract(remainder);
            if (r1.compareTo(BigInteger.ZERO) <= 0) {
                yu[0] = r1.abs();
                return BigInteger.valueOf(i);
            }
        }
        yu[0] = remainder;
        return BigInteger.ZERO;
    }

    public static BigInteger f2(BigInteger remainder, BigInteger result,BigInteger[] yu){
        for (int i = 9; i > 0; i--) {
            BigInteger value = BigInteger.valueOf(i);
            BigInteger r1 = value.multiply(MU3_1.multiply(result).multiply(BigInteger.TEN.multiply(result).add(value)).add(value.pow(2))).subtract(remainder);
            if (r1.compareTo(BigInteger.ZERO) <= 0) {
                yu[0] = r1.abs();
                return BigInteger.valueOf(i);
            }
        }
        yu[0] = remainder;
        return BigInteger.ZERO;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值