biginteger

Fastest way to convert a BigInteger to a decimal (Base 10) string?

冷暖自知 提交于 2019-11-30 09:24:40
问题 Answers So Far So here is the code breakdown. //Time: ~7s (linear loop algorithm) //100,000! (456,574 decimal digits) BigInteger bigIntVar = computeFactorial(100000); //The first three here are just for comparison and are not actually Base 10. bigIntVar.ToBase64String() //Time: 00.001s | Base 64 | Tetrasexagesimal bigIntVar.ToString("x") //Time: 00.016s | Base 16 | Hexadecimal bigIntVar.ToBinaryString() //Time: 00.026s | Base 02 | Binary bigIntVar.ToQuickString() //Time: 11.200s | Base 10 |

What algorithm should I use for high-performance large integer division?

人走茶凉 提交于 2019-11-30 07:30:44
问题 I am encoding large integers into an array of size_t . I already have the other operations working (add, subtract, multiply); as well as division by a single digit. But I would like match the time complexity of my multiplication algorithms if possible (currently Toom-Cook). I gather there are linear time algorithms for taking various notions of multiplicative inverse of my dividend. This means I could theoretically achieve division in the same time complexity as my multiplication, because the

Big number in C++

我与影子孤独终老i 提交于 2019-11-30 07:29:13
问题 I am trying to place a big number in a C++ variable. The number is 600851475143 I tried unsigned long long int but got an error saying it the constant was too big. I then tried a bigInt library called BigInt -> http://mattmccutchen.net/bigint/ The problem is I can't compile the code as I get many errors regarding the lib. undefined reference to `BigInteger::BigInteger(int)' <-- lot's of these. Here is my code so far: #include "string" #include "iostream" #include "bigint/NumberlikeArray.hh"

java.math.BigInteger cannot be cast to java.lang.Long

廉价感情. 提交于 2019-11-30 07:08:11
问题 I've got List<Long> dynamics . And I want to get max result using Collections . This is my code: List<Long> dynamics=spyPathService.getDynamics(); Long max=((Long)Collections.max(dynamics)).longValue(); This is my getDynamics : public List<Long> getDynamics() { Session session = null; session = this.sessionFactory.getCurrentSession(); Query query = session .createSQLQuery("SELECT COUNT(*) FROM SpyPath WHERE DATE(time)>=DATE_SUB(CURDATE(),INTERVAL 6 DAY) GROUP BY DATE(time) ORDER BY time;");

How can I convert an absolutely massive number to a string in a reasonable amount of time?

那年仲夏 提交于 2019-11-30 05:59:32
This is quite an odd problem I know, but I'm trying to get a copy of the current largest prime number in a file. Getting the number in integer form is fairly easy. I just run this. prime = 2**74207281 - 1 It takes about half a second and it works just fine. Operations are fairly quick as well. Dividing it by 10 (without decimals) to shift the digits is quick. However, str(prime) is taking a very long time. I reimplemented str like this, and found it was processing about a hundred or so digits per second. while prime > 0: strprime += str(prime%10) prime //= 10 Is there a way to do this more

Check if BigInteger is not a perfect square

╄→гoц情女王★ 提交于 2019-11-30 05:33:30
I have a BigInteger value, let's say it is 282 and is inside the variable x. I now want to write a while loop that states: while b2 isn't a perfect square: a ← a + 1 b2 ← a*a - N endwhile How would I do such a thing using BigInteger? EDIT: The purpose for this is so I can write this method . As the article states one must check if b2 is not square. Compute the integer square root, then check that its square is your number. Here is my method of computing the square root using Heron's method : private static final BigInteger TWO = BigInteger.valueOf(2); /** * Computes the integer square root of

What data-structure should I use to create my own “BigInteger” class?

限于喜欢 提交于 2019-11-30 01:51:33
问题 As an optional assignment, I'm thinking about writing my own implementation of the BigInteger class, where I will provide my own methods for addition, subtraction, multiplication, etc. This will be for arbitrarily long integer numbers, even hundreds of digits long. While doing the math on these numbers, digit by digit isn't hard, what do you think the best datastructure would be to represent my "BigInteger"? At first I was considering using an Array but then I was thinking I could still

How to convert BigInteger to String in java

℡╲_俬逩灬. 提交于 2019-11-29 23:58:27
I converted a String to BigInteger as follows: Scanner sc=new Scanner(System.in); System.out.println("enter the message"); String msg=sc.next(); byte[] bytemsg=msg.getBytes(); BigInteger m=new BigInteger(bytemsg); Now I want my string back. I'm using m.toString() but that's giving me the desired result. Why? Where is the bug and what can I do about it? You want to use BigInteger.toByteArray() String msg = "Hello there!"; BigInteger bi = new BigInteger(msg.getBytes()); System.out.println(new String(bi.toByteArray())); // prints "Hello there!" The way I understand it is that you're doing the

Number of digits of GMP integer

夙愿已清 提交于 2019-11-29 17:00:49
问题 Is there an easy way to determine the number of digits a GMP integer has? I know you can determine it through a log, but I was wondering if there was something built into the library that I'm missing. The only thing I've found in the manual is: _mp_size The number of limbs, or the negative of that when representing a negative integer. Zero is represented by _mp_size set to zero, in which case the _mp_d data is unused. But I'm under the impression that is quite different than what I'm looking