1

In solving a question related to permutations and probability, I have got stuck at the following problem, where I need to print the reciprocal of a number and the range of the number can go up to 10 105.

Possible Approach which I looked into for printing that number is: -Using own java class/BigInteger as per following ques :

How to handle very large numbers in Java without using java.math.BigInteger

But the limitation is, it still cannot cater to the limit required.

I also looked for another approach but that was in python. For example, following logic in python works fine:

x=int(input()) print x*'0'

If the input is 100000 then the output is : 100000 times 0 written in the console.

What is some other good approach to solve the given issue?

Thanks in Advance.

Deep
  • 929
  • 2
  • 16
  • 32
  • I don't remember - is it permutations of (n) is n factorial? – zlakad Jul 09 '18 at 17:27
  • it is more related to a probability for a small number on large sample set – Deep Jul 09 '18 at 17:41
  • I don't understand your problem, although I studied probability and statistics... What is domain of your quest? What is a set of all possible outcomes? And, so on... Give us some code, please. – zlakad Jul 09 '18 at 17:48
  • FWIW, printing large numbers is slow, no matter how you do it, unless you really only have numbers that are a power of ten. In the latter case, you can speed up things. Note that not the printing is the problem. The problem (bottleneck) is the base conversion from 2 to 10. BigIntegers are stored in binary, and must be converted to base 10 when a string is formed. But even that is well optimized. – Rudy Velthuis Jul 09 '18 at 19:58

2 Answers2

0

I once had to do something similar, it's a real pain. What I ended up doing was representing the massive number into multiple BigIntegers, and storing the parts in a linked list. Hope this has the potential to help.

  • Can you elaborate more or share some piece of code for better understanding. Thanks – Deep Jul 09 '18 at 17:40
  • I don't have the code anymore so I'll try and elaborate. Find a way to build the the integer such that you make one node of the linked list say the first 10 digits, the second node the next 10 digits, and so on. So for example if the number is 123456789 it would look like [123]->[456]->[789] represented as a linked list. Each node's type was a BigInteger. Hope this helps. –  Jul 09 '18 at 17:52
  • You could use a list of longs. A long can contain 19 decimal digits. So divide the number up in parts of 10^19. – Rudy Velthuis Jul 09 '18 at 20:03
0

There is a variety of ways you can solve this and store super large numbers- you can use multiple BigIntegers to store parts of them. Or like you can create your own data structure and wrapper.

class LargeNumber{
   Integer base;
   Integer power;
   LargeNumber(Integer base, Integer power){
      this.base = base; 
      this.power = power;
   }
}
Linkx_lair
  • 569
  • 1
  • 9
  • 21
  • I wont down vote your attempt to answer. My question to you is: what's wrong with `BigInteger`? Memory issue? – zlakad Jul 09 '18 at 17:54
  • BigInteger is good - but i believe has some slight performance issues compared to like Integer. And for number like 100000, the Integer wrapper should suffice. If you want to go much bigger though beyond about 2 billion digits, then you may want to use a Long or BigInteger depending on the size. BigInteger doesn't have a primitive so it is more memory heavy since you are creating a huge object instead – Linkx_lair Jul 09 '18 at 18:06