我需要一个可变版本的BigInteger,但我无法访问mutablebiginteger

时间:2018-06-07 09:12:27

标签: java performance biginteger

我正在写一篇关于我写的代码的文章。我正在计算超过变量长度的值,我还需要比较这些值,所以我使用的是BigInteger。

问题是,BigInteger是不可变的,所以每次新的计算发生时,我的程序运行得越来越慢,导致在数组中添加一个新的BigInteger。

经过一些研究后,我了解了MutableBigInteger并试图使用它,但我无法导入这个类,因为它是java.util.Math中的私有。我找到了一个使用MutableBigInteger Performance of MutableBigInteger

的主题

问题是,我无法理解他的代码,因此我不知道如何使用BigInteger的可变版本。我也读了一些关于BitSet的内容,但我认为在这种情况下它不会对我有所帮助。

想象一下n个数> 0的时钟。你需要将这个时钟“切”为2(想象一个有15个数字的时钟,我会把数字放在1号而另一个放在数字7上)现在我需要检查1到7之间的数字之和是否是等于7和1之间的总和(7到n然后这个结果'直到削减1)

这是我的代码:

public class LinePuzzle {
private static int tam;
private static int cont = 0;
private static int t1parte, t2parte, t3parte, t4parte, ajuda, ajuda2;


static int geraCortes(int tam) {
    Thread tt1 = new Thread(t1);
    Thread tt2 = new Thread(t2);
    Thread tt3 = new Thread(t3);
    Thread tt4 = new Thread(t4);

    if(tam % 2 == 0){ 
        ajuda = tam/4;
        t1parte = ajuda;
        t2parte = ajuda*2;
        t3parte = ajuda*3;
        t4parte = ajuda*4;
    }else{ 
        ajuda2 = tam%4;
        ajuda = (tam-ajuda2)/4;
        t1parte = ajuda;
        t2parte = (ajuda*2);
        t3parte = (ajuda*3);
        t4parte = (ajuda*4) + ajuda2;
    }

     // "Starts"
    tt1.start();
    tt2.start();
    tt3.start();
    tt4.start();

    try {
        // 
        tt1.join();
        tt2.join();
        tt3.join();
        tt4.join();
    } catch (Exception ex) {
        System.out.println("Finalizado");
    }
    return cont;
}

private static Runnable t1 = new Runnable()  {
@Override
public void run() {
long soma1, soma2;
    for (int i = 0; i<= t1parte; i++) { // 1º cut
        for (int j = i + 1; j <= tam; j++) { // 2º cut
            if (i == j || i == j - 1) { // tests
                continue;
            }

            soma1 = (((i + 1) + (j - 1)) * (j - i - 1)) / 2; 
            soma2 = (((j + 1) + tam) * (tam - j) + (1 + (i - 1)) * (i - 1)) / 2;

            if (soma1 == soma2 && soma1 != 0) {
                BigInteger bi, bi2; 

                bi = BigInteger.valueOf(soma1);
                bi2 = BigInteger.valueOf(soma2);

                if(bi.equals(bi2)){
                System.out.printf("Equals: cut1 = %d and cut2 = %d -> result: %s / %s\n", i, j, bi, bi2);
                    cont++; 
                    break;
                }
            }
        }
    }
}
};
private static Runnable t2 = new Runnable() {
@Override
public void run() {
long soma1, soma2;
    for (int i = t1parte; i<= t2parte; i++) { 
        for (int j = i + 1; j <= tam; j++) { 
            if (i == j || i == j - 1) {
                continue;
            }

            soma1 = (((i + 1) + (j - 1)) * (j - i - 1)) / 2;
            soma2 = (((j + 1) + tam) * (tam - j) + (1 + (i - 1)) * (i - 1)) / 2;

            if (soma1 == soma2 && soma1 != 0) {
               BigInteger bi, bi2; 

                bi = BigInteger.valueOf(soma1);
                bi2 = BigInteger.valueOf(soma2);

                if(bi.equals(bi2)){
                System.out.printf("Equals: cut1 = %d and cut2 = %d -> result: %s / %s\n", i, j, bi, bi2);
                    cont++; 
                    break;
                }
            }
        }
    }
}
};

private static Runnable t3 = new Runnable() {
@Override
public void run() {
long soma1, soma2;
    for (int i = t2parte; i<= t3parte; i++) { // 1º corte
        for (int j = i + 1; j <= tam; j++) { // 2º corte
            if (i == j || i == j - 1) {
                continue;
            }

            soma1 = (((i + 1) + (j - 1)) * (j - i - 1)) / 2;
            soma2 = (((j + 1) + tam) * (tam - j) + (1 + (i - 1)) * (i - 1)) / 2;

            if (soma1 == soma2 && soma1 != 0) {
                BigInteger bi, bi2; 

                bi = BigInteger.valueOf(soma1);
                bi2 = BigInteger.valueOf(soma2);
                if(bi.equals(bi2)){
                System.out.printf("Equals: cut1 = %d and cut2 = %d -> result: %s / %s\n", i, j, bi, bi2);
                    cont++; 
                    break;
                }
            }
        }
    } 
}
};

private static Runnable t4 = new Runnable() {
@Override
public void run() {
long soma1, soma2;
    for (int i = t3parte; i<= t4parte; i++) { // 1º corte
        for (int j = i + 1; j <= tam; j++) { // 2º corte
            if (i == j || i == j - 1) {
                continue;
            }

            soma1 = (((i + 1) + (j - 1)) * (j - i - 1)) / 2;
            soma2 = (((j + 1) + tam) * (tam - j) + (1 + (i - 1)) * (i - 1)) / 2;

            if (soma1 == soma2 && soma1 != 0) {
                BigInteger bi, bi2; 

                bi = BigInteger.valueOf(soma1);
                bi2 = BigInteger.valueOf(soma2);
                if(bi.equals(bi2)){
                System.out.printf("Equals: cut1 = %d and cut2 = %d -> result: %s / %s\n", i, j, bi, bi2);
                    cont++; 
                    break;
                }
            }
        }
    }
}
};   

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    do{
        System.out.println("Clock length: ");
        tam = scan.nextInt();
    }while(tam < 2);

    System.out.println("Numbers of equals: " + geraCortes(tam)); // executar o geracortes e os threads
}

}

1 个答案:

答案 0 :(得分:3)

您需要了解如何使用BigInteger进行计算。

下面是两个计算的示例(假设import React from "react"; import { withRouter } from "react-router-dom" class LoginForm extends React.Component { constructor (super) { this.state = { email: null, password: null } this.login = this.login.bind(this) } login() { const { email, password } = this.state firebase .auth() .signInWithEmailAndPassword(email, password) .then(() => this.props.history.push('/questions')) .catch(e => console.log(e)) } render() { <form onSubmit={this.login}> // Controlled inputs <input name='email' /> <input name='password' /> <input type='submit' value='Submit' /> </form> } } // Please checkout: https://stackoverflow.com/a/42716055/4312466 export default withRouter(LoginForm) ij为长):

tam