Questions tagged [biginteger]

BigInteger is an arbitrary-precision arithmetic type in Java, C#, and other languages. It behaves like a signed integer whose size is limited only by available memory.

BigInteger is an arbitrary-precision arithmetic type in the .NET Framework, Java, and other languages. It behaves like a signed integer whose size is limited only by available memory.

1971 questions
154
votes
10 answers

How to use BigInteger?

I have this piece of code, which is not working: BigInteger sum = BigInteger.valueOf(0); for(int i = 2; i < 5000; i++) { if (isPrim(i)) { sum.add(BigInteger.valueOf(i)); } } The sum variable is always 0. What am I doing wrong?
cc.
  • 5,533
  • 14
  • 37
  • 46
145
votes
3 answers

Converting from Integer, to BigInteger

I was wondering if there was any way to convert a variable of type Integer, to BigInteger. I tried typecasting the Integer variable, but i get an error that says inconvertible type.
Steffan Harris
  • 9,106
  • 30
  • 75
  • 101
106
votes
8 answers

Arbitrary-precision arithmetic Explanation

I'm trying to learn C and have come across the inability to work with REALLY big numbers (i.e., 100 digits, 1000 digits, etc.). I am aware that there exist libraries to do this, but I want to attempt to implement it myself. I just want to know if…
TT.
  • 1,339
  • 3
  • 13
  • 13
104
votes
6 answers

Large Numbers in Java

How would I go about doing calculations with extremely large numbers in Java? I have tried long but that maxes out at 9223372036854775807, and when using an integer it does not save enough digits and therefore is not accurate enough for what I…
Petey B
  • 11,439
  • 25
  • 81
  • 101
103
votes
3 answers

Is there an upper bound to BigInteger?

Possible Duplicate: What does BigInteger having no limit mean? The Javadoc for BigInteger does not define any maximum or minimum. However, it does say: (emphasis added) Immutable arbitrary-precision integers Is there such a maximum, even in…
asteri
  • 11,402
  • 13
  • 60
  • 84
98
votes
6 answers

How do I convert a String to a BigInteger?

I'm trying to read some really big numbers from standard input and add them together. However, to add to BigInteger, I need to use BigInteger.valueOf(long);: private BigInteger sum = BigInteger.valueOf(0); private void sum(String newNumber) { …
Twinone
  • 2,949
  • 4
  • 28
  • 39
90
votes
5 answers

What are the best (portable) cross-platform arbitrary-precision math libraries?

I’m looking for a good arbitrary precision math library in C or C++. Could you please give me some advices or suggestions? The primary requirements: It must handle arbitrarily big integers—my primary interest is on integers. In case that you don’t…
83
votes
14 answers

How to implement big int in C++

I'd like to implement a big int class in C++ as a programming exercise—a class that can handle numbers bigger than a long int. I know that there are several open source implementations out there already, but I'd like to write my own. I'm trying to…
oneself
  • 38,641
  • 34
  • 96
  • 120
72
votes
7 answers

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

How would I go about doing arithmetic, + - / * % !, with arbitrarily large integers without using java.math.BigInteger? For instance, the factorial of 90 returns 0 in Java. I would like to be able to solve that.
frodosamoa
  • 973
  • 2
  • 11
  • 13
70
votes
8 answers

How to generate a random BigInteger value in Java?

I need to generate arbitrarily large random integers in the range 0 (inclusive) to n (exclusive). My initial thought was to call nextDouble and multiply by n, but once n gets to be larger than 253, the results would no longer be uniformly…
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
66
votes
1 answer

Long vs BigInteger

I understand that both java.lang.Long and java.math.BigIntegercan hold very large natural numbers. I also know Long's max value, but what is the max value for BigInteger? And aside from capacity, would BigInteger ever perform better when working…
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
63
votes
13 answers

Big integers in C#

Currently I am borrowing java.math.BigInteger from the J# libraries as described here. Having never used a library for working with large integers before, this seems slow, on the order of 10 times slower, even for ulong length numbers. Does anyone…
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
59
votes
3 answers

MySQL: bigint Vs int

I have been using int(10) and just noticed that Wordpress uses bigint(20) - What is different to use bigint(20) and int(10) for id auto increment? Which one should I use for id column? `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, Vs `id`…
Run
  • 54,938
  • 169
  • 450
  • 748
59
votes
4 answers

What does BigInteger having no limit mean?

I looked into this stackoverflow question relating to Big Integer and specifically I do not understand this line (the words in italics): In the BigInteger class, I have no limits and there are some helpful functions there but it is pretty…
Geek
  • 26,489
  • 43
  • 149
  • 227
58
votes
20 answers

How can I find the Square Root of a Java BigInteger?

Is there a library that will find the square root of a BigInteger? I want it computed offline - only once, and not inside any loop. So even computationally expensive solution is okay. I don't want to find some algorithm and implement. A readily…
user529141
1
2 3
99 100