How to use BigInteger?
BigInteger
is immutable. The javadocs states that add() "[r]eturns a BigInteger whose value is (this + val)." Therefore, you can't change sum
, you need to reassign the result of the add
method to sum
variable.
sum = sum.add(BigInteger.valueOf(i));
How do you use BigInteger with for loops and modding BigIntegers?
To use BigInteger
correctly, you need to its methods https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigInteger.html.
So in your case.
for (BigInteger x = new BigInteger("2"); x.pow(2).compareTo(number) < 0; x = x.add(BigInteger.ONE)) {
if (number.mod(x).compareTo(BigInteger.ZERO) == 0) {
System.out.println("not a prime dividable by" + x);
}
}
How to use BigInteger class?
Use BigInteger
in your factorial
function, not "after you've done the calculation".
Note also that when multiplying BigInteger
s, use the BigInteger.multiply(BigInteger val)
method instead of *
.
Here's the changed method, which is exactly the same as yours except it uses BigInteger
instead of int
:
public static BigInteger factorial(int n){
int index = n;
BigInteger total = BigInteger.valueOf(1);
while(index > 0){
total = total.multiply(BigInteger.valueOf(index));
index --;
}
return total;
}
Note that also you don't need to convert the return value of the method to a BigInteger
anymore, e.g. just do:
BigInteger big = factorial(index);
Here's the output:
30: 265252859812191058636308480000000
29: 8841761993739701954543616000000
28: 304888344611713860501504000000
27: 10888869450418352160768000000
26: 403291461126605635584000000
25: 15511210043330985984000000
24: 620448401733239439360000
23: 25852016738884976640000
22: 1124000727777607680000
21: 51090942171709440000
20: 2432902008176640000
19: 121645100408832000
18: 6402373705728000
17: 355687428096000
16: 20922789888000
15: 1307674368000
14: 87178291200
13: 6227020800
12: 479001600
11: 39916800
10: 3628800
9: 362880
8: 40320
7: 5040
6: 720
5: 120
4: 24
3: 6
2: 2
1: 1
Loop Adding BigInteger java
BigInteger
is immutable - calling sum.add(...)
does nothing to sum
but returns a new BigInteger
.
So you need to change your code to:
sum = sum.add(numbers[i]);
How to use BigInteger in VS 2010
Add a reference to the System.Numerics assembly to your project.
a. In Solution Explorer, right-click the project node and click Add Reference.
b. In the Add Reference dialog box, select the .NET tab.
c. Select System.Numerics, and then click OK.
Add a using directive importing the System.Numerics namespace:
using System.Numerics;
Use the BigInteger structure:
var i = new BigInteger(934157136952);
Increasing performance when doing calculations with huge numbers (BigInteger)
I need to calculate the product of two catalan sequences for every
single n-value between 0 and 5000 and then summarize those products.
Well, this is exactly an alternative definition of a Catalan number.
Cn+1 = SUMi=0..n ( Ci * Cn-i )
So, what you basically need is to calculate C5001. To calculate it fast, you may use another recurrence relation:
Cn+1 = 2*(2n+1) / (n+2) * Cn
Here is the program:
public static void main(String[] args) {
int n = 5000;
BigInteger Cn = BigInteger.ONE;
for (int i = 0; i <= n; i++) {
Cn = Cn.multiply(BigInteger.valueOf(4 * i + 2)).divide(BigInteger.valueOf(i + 2));
}
System.out.println(Cn);
}
Works less than 0.04 sec on my laptop. Enjoy!
Lambda - Question regarding BigInteger and Lambda Usage in this specific application
Try this.
interface BigOperation {
BigInteger operation(BigInteger x, BigInteger y);
}
static class Iterative {
static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
BigInteger product = BigInteger.ONE;
for ( ; x.compareTo(BigInteger.ZERO) > 0; x = op2.operation(x, BigInteger.TEN))
product = op1.operation(product, op3.operation(x, BigInteger.TEN));
return product;
}
}
static class Recursive {
static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
if (x.compareTo(BigInteger.TEN) < 0)
return x;
else
return op1.operation(op3.operation(x, BigInteger.TEN),
product(op2.operation(x, BigInteger.TEN), op1, op2, op3));
}
}
and
static void test(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
System.out.println("Given bit integer " + x);
System.out.println("(Iterative) " + Iterative.product(x, op1, op2, op3));
System.out.println("(Recursive) " + Recursive.product(x, op1, op2, op3));
}
public static void main(String[] args) {
BigOperation multiply = BigInteger::multiply;
BigOperation divide = BigInteger::divide;
BigOperation remainder = BigInteger::remainder;
test(new BigInteger("8584803"), multiply, divide, remainder);
test(new BigInteger("12345"), multiply, divide, remainder);
}
output:
Given bit integer 8584803
(Iterative) 0
(Recursive) 0
Given bit integer 12345
(Iterative) 120
(Recursive) 120
Related Topics
Mock a Constructor with Parameter
Using Internal Sun Classes with Javac
Java Webservice Client (Best Way)
Convert JSON String to Pretty Print JSON Output Using Jackson
Spring Jdbc Template for Calling Stored Procedures
Printing Java Collections Nicely (Tostring Doesn't Return Pretty Output)
Log4J: Log Output of a Specific Class to a Specific Appender
Calling Outer Class Function from Inner Class
Embedded Mongodb When Running Integration Tests
Can You Split a Stream into Two Streams
How to Create an Asynchronous Http Request in Java
Why Can't Enum's Constructor Access Static Fields
Isn't the Size of Character in Java 2 Bytes
Communication Between Two Separate Java Desktop Applications
Debug a Java Application Without Starting the Jvm with Debug Arguments
Java.Util.Date Format Ssssss: If Not Microseconds What Are the Last 3 Digits