biginteger

Multiply two large numbers in Java [closed]

*爱你&永不变心* 提交于 2019-12-03 01:21:56
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . public long Fib(int a1, int a2, int i){ if(i==1){ return a1; }else if(i==2){ return a2; }else{ long l1 = Fib(a1, a2, i-1); long l2 = Fib(a1, a2, i-2); long val = l2 + (l1*l1); return val; } } I wrote this code to find a Modified Fibonacci of function of t(i+2) = t(i) + t(i+1)^2 where t(i) is the answer when we

Java Mutable BigInteger Class

旧城冷巷雨未停 提交于 2019-12-02 22:08:01
I am doing calculations with BigIntegers that uses a loop that calls multiply() about 100 billion times, and the new object creation from the BigInteger is making it very slow. I was hoping somebody had written or found a MutableBigInteger class. I found the MutableBigInteger in the java.math package, but it is private and when I copy the code into a new class, many errors come up, most of which I don't know how to fix. What implementations exist of a Java class like MutableBigInteger that allows modifying the value in place? Is their any particular reason you cannot use reflection to gain

Multiply two large numbers in Java [closed]

人走茶凉 提交于 2019-12-02 15:05:02
public long Fib(int a1, int a2, int i){ if(i==1){ return a1; }else if(i==2){ return a2; }else{ long l1 = Fib(a1, a2, i-1); long l2 = Fib(a1, a2, i-2); long val = l2 + (l1*l1); return val; } } I wrote this code to find a Modified Fibonacci of function of t(i+2) = t(i) + t(i+1)^2 where t(i) is the answer when we find ith iterartion. But when I find t(10), it gives a separate answer. But I got correct answers till t(9). I tried BigInteger method but it gives errors. You should modify your code a little if you want to use BigInteger s. Something like: public class ModifiedFib { static BigInteger

How to save/load BigInteger array

喜欢而已 提交于 2019-12-02 14:09:25
I want to save/load a BigInteger array into/from the SharedPreferences. How can it be done? For example for the following array: private BigInteger[] dataCreatedTimes = new BigInteger[20]; Using Gson you can convert to a json String and back, which then of course makes it trivial to save in preferences: import com.google.gson.Gson; import java.math.BigInteger; public final class BigIntegerArrayJson { private BigIntegerArrayJson(){} public static String toJson(BigInteger[] array) { return new Gson().toJson(array); } public static BigInteger[] fromJson(String json) { return new Gson().fromJson

How can I decrease the time complexity for this question?

与世无争的帅哥 提交于 2019-12-02 10:34:14
I am solving a problem such that for every given number between range (inclusive of boundary integers), I calculate the sum of the modified value of those numbers. For example, x = 388,822,442 f(388,822,442)=3800200402 i.e; makes zeros for the identical digits. f(x) is modified value. I iterated through each digit of an integer and made it zero if repeated. BufferedReader bf = new BufferedReader(newInputStreamReader(System.in)); String s[], s1[]; s = bf.readLine().trim().split("\\s+"); s1 = bf.readLine().trim().split("\\s+"); BigInteger sb = new BigInteger(s[1]); BigInteger sb1 = new

How can I fix this “plus” method in Polynomial class using BigInteger

旧时模样 提交于 2019-12-02 08:50:49
问题 I appreciate the help. I was able to finish modifying everything in this class into BigInteger format except for the compose method. Can anyone help me with this last part as to why it is not working correctly? I really appreciate it, thanks. import java.math.BigInteger; public class Polynomial { private BigInteger[] coef; // coefficients private int deg; // degree of polynomial (0 for the zero polynomial) /** Creates the constant polynomial P(x) = 1. */ public Polynomial(){ coef = new

BigInteger -> byte[] -> Biginteger. Looks equal but if statement fails

心不动则不痛 提交于 2019-12-02 08:37:55
I am playing around with an idea I have for storing a public key for myself. For this I would need to transform the BigInteger in some sort of a variable and then recreate the BigInteger from that value. I have searched through Stackoverflow to find the best way to do this is with byte[]. This is my code in Eclipse: import java.math.BigInteger; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.interfaces.RSAPublicKey; public class Vaja2 { public static void main(String[] args){ try { // Create RSA Keypair (to obtain a BigInteger) KeyPairGenerator kpg =

Error assigning to an an element in an array of BigInteger

橙三吉。 提交于 2019-12-02 07:22:23
问题 This is my code. It shows an error when I create an array of BigInteger and try to assign a value. package test; import java.math.*; import java.lang.*; import java.util.*; public class Test { public static void main(String[] args) { BigInteger[] coef = new BigInteger[78]; int a=24; coef[a]=676557656534345345654645654654645645645645665656567; // Error comes here why System.out.println(coef[a]); } } 回答1: KEEP IN MIND ALWAYS All numbers greater then 2147483647 will not be allowed as input

How to convert a BigInteger to a scientific notation string and back?

社会主义新天地 提交于 2019-12-02 07:01:30
问题 I want to convert BigInteger number into a small scientific notation like 1.86e+6 and again reconvert that scientific notation into BigInteger number in Java. Please help. 回答1: The easiest way is to use a BigDecimal to parse or output the scientific notation string. You can can do something like: BigDecimal bd = new BigDecimal("1.86E+6"); BigInteger bi = bd.toBigInteger(); and reverse: bd = new BigDecimal(bi); String s = bd.toString(); Update If you need more user-defined output, then you can

How can I multiply and divide 64-bit ints accurately?

回眸只為那壹抹淺笑 提交于 2019-12-02 04:46:36
问题 I have a C function: int64_t fn(int64_t a, int32_t b, int32_t c, int32_t d) { /* should return (a * b * c)/d */ } It is possible for a to be near INT64_MAX, but for the final result not to overflow, for instance if b = 1, c = d = 40. However, I am having trouble figuring out how to compute this so that I never lose data to rounding (by doing the division first) or have an intermediate result overflow. If I had access to a large enough datatype to fit the whole product of a, b, and c, I would