-1

So for no particular reason I wanted to know what the largest number you can store in a gigabyte of memory. So I used an arbitrary precision library to calculate it, but the trouble is trying to output this number to a file, since a string can only store int.max character.

Apint a = new Apint(2);
    a = ApintMath.pow(a, 8589934591l);
    a = a.subtract(new Apint(1));
    File file = new File("theNumber.txt");
    PrintWriter pls = new PrintWriter(file);
    a.writeTo(pls, true);
    pls.close();
DPE
  • 13
  • 2
  • 2
    If you want to store the largest number possible in `x` bytes in a file just write `x` `0xFF` bytes. – tkausl Aug 26 '17 at 04:15
  • @tkausl I think that will select a slightly too large number because an arbitrary precision library will have to use some of the bytes to record additional information about the number like how long the number is so that the library can support an arbitrary precision. – Algorithmic Canary Aug 26 '17 at 04:23
  • @DPE whats not working here? – Naman Aug 26 '17 at 04:25

2 Answers2

0

You should convert that int number to 4 bytes with little endian or big endian style, and then save 4 bytes to file.

And with this method we can store a very very big number. ex: 8 bytes, 16 bytes...

Update: Try to use BigInteger class and toByteArray() function when writing bytes to file.

dotrinh PM
  • 903
  • 1
  • 7
  • 19
-1

(Untested method; may not work)

Use the mod operator % with a power of 10 to select the right most digits. Write those digits to a file on a line. Then divide by the same power of 10. Now your number is N digits shorter. Repeat writing each group of digits into the file on separate lines.

Now copy the lines in reverse order into another file, either using java or tac if you are on Linux.

You could join each line together, though I would discourage that because many programs will hang if you try to load on very long line of text into them but can handle many lines of text.

  • Please read [answer]. Guesses at answers are frowned upon here, as this is not a discussion forum. If you're not sure about your answer, don't post it until you've coded it, tested it and verified that it answers the OP's question. – Jim Garrison Aug 26 '17 at 04:39
  • Understood, I apologize for my laziness – Algorithmic Canary Aug 26 '17 at 04:41