biginteger

BigInteger to byte[]

我的梦境 提交于 2019-12-17 19:33:27
问题 I need to convert a Java BigInteger instance to its value in bytes. From the API, I get this method toByteArray() , that returns a byte[] containing the two's-complement representation of this BigInteger. Since all my numbers are positive 128 bits (16 bytes) integer, I don't need the 2's-complement form that give me 128 bits + sign bit (129 bits)... Is there a way to get the standard (without the 2's-complement form) representation directly from a BigInteger? If not, how can I right shift the

How can I quickly load a large txt file into BigInteger?

陌路散爱 提交于 2019-12-17 14:52:34
问题 I'm importing a large text file, 17 million digits long and I'm using this code: BufferedReader reader = new BufferedReader(new FileReader("test2.txt")); String line = reader.readLine(); System.out.println("Done"); BigInteger num = new BigInteger(line); System.out.println("Done Again"); It loads the file pretty much instantly and prints out 'Done' but it takes a long time (about an hour) for the String to be converted into a BigInteger, is there anything I can do to speed this up and quickly

Calculate a*a mod n without overflow

落花浮王杯 提交于 2019-12-17 09:56:32
问题 I need to calculate a*a mod n but a is fairly large, resulting in overflow when I square it. Doing ((a % n)*(a % n)) % n doesn't work because (n-1) 2 can overflow. This is in C++ and I'm using int64_t Edit: Example value: a = 821037907258 and n = 800000000000, which overflows if you square it. I am using DevCPP and I've already tried getting big-integer libraries working to no avail. Edit 2: No, there's no pattern to these numbers. 回答1: If you can't use a big-integer library, and you don't

BigInteger: count the number of decimal digits in a scalable method

只谈情不闲聊 提交于 2019-12-17 09:52:20
问题 I need the count the number of decimal digits of a BigInteger . For example: 99 returns 2 1234 returns 4 9999 returns 4 12345678901234567890 returns 20 I need to do this for a BigInteger with 184948 decimal digits and more . How can I do this fast and scalable? The convert-to-String approach is slow: public String getWritableNumber(BigInteger number) { // Takes over 30 seconds for 184948 decimal digits return "10^" + (number.toString().length() - 1); } This loop-devide-by-ten approach is even

Bigint (bigbit) library

烂漫一生 提交于 2019-12-17 04:34:08
问题 I'm looking for a c++ class/library that provides 1024 bit and bigger integers and bit operations like: - bit shifting, - bitwise OR/AND, - position first zero bit speed is crucial, so it would have to be implemented with some SIMD assembly. 回答1: There are several, including GMP, but for speed, the best is likely TTmath. TTmath's design decision to use templated fixed lengths at compiletime lets it be quite fast. 回答2: try gmp library. It is a C library. Beginning with GMP 4.0 a C++ wrapper is

C++ handling very large integers

白昼怎懂夜的黑 提交于 2019-12-17 03:41:38
问题 I am using the RSA Algorithm for encryption/decryption, and in order to decrypt the files you have to deal with some pretty big values. More specifically, things like P = C^d % n = 62^65 % 133 Now that is really the only calculations that ill be doing. I have tried using Matt McCutchen's BigInteger Library, but I am getting a lot of compiler errors during linking, such as: encryption.o(.text+0x187):encryption.cpp: undefined reference to `BigInteger::BigInteger(int)' encryption.o(.text+0x302)

`xrange(2**100)` -> OverflowError: long int too large to convert to int

北城以北 提交于 2019-12-17 03:20:11
问题 xrange function doesn't work for large integers: >>> N = 10**100 >>> xrange(N) Traceback (most recent call last): ... OverflowError: long int too large to convert to int >>> xrange(N, N+10) Traceback (most recent call last): ... OverflowError: long int too large to convert to int Python 3.x: >>> N = 10**100 >>> r = range(N) >>> r = range(N, N+10) >>> len(r) 10 Is there a backport of py3k builtin range() function for Python 2.x? Edit I'm looking for a complete implementation of "lazy" range()

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

一笑奈何 提交于 2019-12-17 02:25:45
问题 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. 回答1: I think a programmer should have implemented his own bignum-library once, so welcome here. (Of course, later you'll get that BigInteger is better, and use this, but it is a valuable learning experience.) (You can follow the source code of this course life on github. Also, I

Big numbers library in c++ [closed]

爷,独闯天下 提交于 2019-12-17 02:24:30
问题 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 . I'm doing a project which requires really big numbers, up to 100 digits. I have read that java supports big integers ( java.Math.BigInteger ), and I want to know if there is something like that in C++. So, here is my question: Is there a standard or non-standard c++ library which implements big integers? Note:

Randomizing a BigInteger

ε祈祈猫儿з 提交于 2019-12-14 04:18:01
问题 I'm looking to randomize a BigInteger. The intent is to pick a number from 1 to 8180385048. Though, from what I noticed, the BigInteger(BitLen, Random) does it from n to X 2 -1, I'd want some unpredictable number. I tried to make a method that would do it, but I keep running into bugs and have finally given in to asking on here. :P Does anyone have any suggestions on how to do this? 回答1: Judging from the docs of Random.nextInt(int n) which obviously needs to solve the same problem, they seem