Last active
August 29, 2015 14:22
-
-
Save kdeenanauth/378c141569a185022284 to your computer and use it in GitHub Desktop.
Printing BigIntegers in Eclipse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import org.junit.Assert; | |
import org.junit.Test; | |
public class BigIntegerTest | |
{ | |
@Test | |
public void whyNoPrint() { | |
BigInteger canPrint = BigInteger.ONE; | |
// factorial | |
long i=1; | |
for (; i < 1677; i++) { | |
canPrint = canPrint.multiply(BigInteger.valueOf(i)); | |
} | |
// multiple by 1678 | |
BigInteger wontPrint = canPrint.multiply(BigInteger.valueOf(i)); | |
// divide by 1678 | |
BigInteger backtolast = wontPrint.divide(BigInteger.valueOf(i)); | |
Assert.assertEquals(backtolast, canPrint); // Asserts successful | |
String canPrintNumbers = canPrint.toString(); | |
String wontPrintNumbers = wontPrint.toString(); | |
System.out.println(String.format("String length of BigInteger that prints %d", canPrintNumbers.length())); | |
System.out.println(String.format("String length of BigInteger that won't print %d", wontPrintNumbers.length())); | |
// break up the lengthy BigInt into the length of the longest BigInteger that prints | |
int x = 0; | |
int charactersToOutput = canPrintNumbers.length(); | |
int lineNumber = 1; | |
while (x < wontPrintNumbers.length()) { | |
System.out.println(String.format("%d: %s", lineNumber++, wontPrintNumbers.substring(x, x + charactersToOutput))); | |
x += charactersToOutput; | |
if(x + charactersToOutput > wontPrintNumbers.length()) { | |
charactersToOutput = wontPrintNumbers.length() - x; | |
} | |
} | |
// Output: | |
// > String length of BigInteger that prints 4679 | |
// > String length of BigInteger that won't print 4682 | |
// > 1: 1743724562067078312824928050733452436910119362165867836282005427753389374889207158679815660777..... | |
// > 2: 000 | |
// Max length of an eclipse String must be 4680 characters /shrug! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment