user503409
user503409

Reputation: 33

java using BigInteger and While

I have a problem

import java.math.BigInteger;
import java.io.*;
import java.util.*;
import java.lang.*;

public class medici {
   public static void main(String[] arg)  {
{     

  BigInteger zac = new BigInteger("3");
  zac = zac.pow(399);

  BigInteger kon = new BigInteger("3");
  kon = kon.pow(400); 

BigInteger nul = new BigInteger("0");
BigInteger jed = new BigInteger("1");
BigInteger detel = new BigInteger("3");


for (BigInteger a = zac; a.compareTo( kon ) <= 0; a = a.add(jed)) {


 cis = a ;      // THIS A PROBLEM
String retez = "";

 while ( cis > 0 );      // THIS IS A PROBLEM

retez = ( cis.mod(detel) ) + retez;

cis = cis.divide(detel);

         System.out.println(retez);

}      
}
}
}

I've tried this formula BigInteger cis = new BigInteger("a"); for this cis = a ;

and while ( cis.compareTo( nul ) > 0 ); for this while ( cis > 0 );

but it doesn't work and I don't know why.

When I use this formula, this is the same, but I used only integer when I use the same for Big Integer it doesn't work

import java.io.*;
import java.util.*;
import java.lang.*;

public class netik {
   public static void main(String[] arg)  {
{          
int a ;
int cis;
int detel = 3;

for ( a = 567880; a <= 567890; a++ ){

cis = a;

String retez = "";

      while (cis > 0)  {

      retez = (cis % detel) + retez;

      cis /= detel;

      }
System.out.println(retez);    
}
}
}
}

Upvotes: 3

Views: 21419

Answers (2)

jjnguy
jjnguy

Reputation: 138922

To declare cis and store a into cis see the following:

BigInteger cis = new BigInteger(""+a);

Assuming this code is the main cause of your problems, and assuming cis is a BigInteger:

while (cis > 0)  {
    retez = (cis % detel) + retez;
    cis /= detel;
}

This should instead be: (This assumes everything is a BigInteger.

while (cis.compareTo(new BigInteger("0")) > 0)  {
    retez = (cis.mod(detel)).add(retez);
    cis = cis.divide(detel);
}

The following code runs for me:

public static void main(String[] arg) {

    BigInteger zac = new BigInteger("3");
    zac = zac.pow(399);

    BigInteger kon = new BigInteger("3");
    kon = kon.pow(400);

    BigInteger nul = new BigInteger("0");
    BigInteger jed = new BigInteger("1");
    BigInteger detel = new BigInteger("3");

    for (BigInteger a = zac; a.compareTo(kon) <= 0; a = a.add(jed)) {

        BigInteger cis = a; // THIS A PROBLEM
        String retez = "";

        while (cis.compareTo(new BigInteger("0")) >= 0) {
            retez = (cis.mod(detel)) + retez;
            cis = cis.divide(detel);
            System.out.println(retez);
        }
    }
}

It doesn't produce the neatest results. But it runs.

Upvotes: 1

Neil
Neil

Reputation: 5780

Wow, that's hard to read.
From what I've gathered, cis isn't declared. I assume you meant to have BigInteger cis; up there somewhere. Please be more specific. What type of problem are you receiving? Compilation error? Problem in what the program actually does?

Upvotes: 0

Related Questions