简体   繁体   中英

How can I convert a long function into a Biginteger into a string?

I'm trying to change my long variables into BigInteger because I'm storing numbers of the fibonacci series in it and then ultimately turn them into a string. Here is my original code.

public class Fib {
    public static void main(String args[])
    {
        long n1=0,n2=1,n3,i,count = 100;
        System.out.print(n1+" "+n2);//printing 0 and 1

        for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
             {
                n3=n1+n2;
                System.out.print(" "+n3);
                n1=n2;
                n2=n3;
            }

        }} :

and I've tried to convert it into a BigInteger AND a string afterwards like this...

public class Fib {
    public static void main(String args[])
    {
        long n1=0,n2=1,n3,i,count = 100;
        BigInteger n1,n2,n3,i,count = BigInteger.valueOf(n1,n2,n3,i,count)
        System.out.print(n1+" "+n2);//printing 0 and 1

        for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
             {
                String n3= n1+n2;
                System.out.print(" "+n3);
                n1=n2;
                n2=n3;
            }

        }}

However, an error occurs. I've tried to read the syntax for converting long variables into BigIntegers and BigIntegers into strings and I understand that what I've done is wrong.

However, I can't seem to figure out the right way to implement what I want to do, which is why I've came to StackOverflow to seek help.

If anyone can help me with this, It would be very much appreciated.

The java spec defines rigidly what operators can be applied to what and what it means. Specifically, all the usual mathy operators ( + , - , etc) all apply only to primitive numbers , with the one exception being + which can show up between strings and anything else too, but then it's a completely different thing (string concatenation instead of numeric addition).

They cannot be applied to BigInteger AT ALL - the spec simply doesn't list what that would mean, and java does not have operator overloading.

The direct replacements are methods confusingly named add , subtract , multiply , divide . So,

BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.ONE;
BigInteger c = a + b; // wrong
BigInteger c = a.add(b); // correct

The better names would have been plus , minus , times , and dividedBy . In the sense that if I write on a blackboard a = b + c , and I ask you to read it out loud, you'd say: "a equals b plus c". Not "a equals b add c". But, it is what it is - just remember, a.add(b) will calculate the sum of a and b and return a brand new BigInteger with the result. It does not add b to a . (BigInteger is immutable; it is not possible to change its value by calling methods on it, or any other way outside of hackery).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM