biginteger

BigInteger most time optimized multiplication

a 夏天 提交于 2019-12-29 07:14:20
问题 Hi I want to multiply 2 big integer in a most timely optimized way. I am currently using karatsuba algorithm. Can anyone suggest more optimized way or algo to do it. Thanks public static BigInteger karatsuba(BigInteger x, BigInteger y) { // cutoff to brute force int N = Math.max(x.bitLength(), y.bitLength()); System.out.println(N); if (N <= 2000) return x.multiply(y); // optimize this parameter // number of bits divided by 2, rounded up N = (N / 2) + (N % 2); // x = a + 2^N b, y = c + 2^N d

How to generate a “big” random number in Python?

守給你的承諾、 提交于 2019-12-29 06:35:45
问题 How can I generate a big (more than 64 bits) random integer in Python? 回答1: You can use random.getrandbits(): >>> random.getrandbits(128) 117169677822943856980673695456521126221L As stated in the linked documentation, random.randrange() will also do the trick if random.getrandbits() is available. 来源: https://stackoverflow.com/questions/10012534/how-to-generate-a-big-random-number-in-python

Using BigInteger Multiply operator

大兔子大兔子 提交于 2019-12-29 04:21:06
问题 I was wondering if there was a way to multiply BigInteger variables together, because the * operator cannot be applied to BigInteger . So I was wondering if it was possible to multiply two BigIntegers together without using the * operator. 回答1: You use BigIntegers multiply() method like so: BigInteger int1 = new BigInteger("131224324234234234234313"); BigInteger int2 = new BigInteger("13345663456346435648234313"); BigInteger result = int1.multiply(int2) I should have pointed out a while ago

x86-64 Big Integer Representation?

♀尐吖头ヾ 提交于 2019-12-29 01:16:07
问题 How do hig-performance native big-integer libraries on x86-64 represent a big integer in memory? (or does it vary? Is there a most common way?) Naively I was thinking about storing them as 0-terminated strings of numbers in base 2 64 . For example suppose X is in memory as: [8 bytes] Dn . . [8 bytes] D2 [8 bytes] D1 [8 bytes] D0 [8 bytes] 0 Let B = 2 64 Then X = D n * B n + ... + D 2 * B 2 + D 1 * B 1 + D 0 The empty string (i.e. 8 bytes of zero) means zero. Is this a reasonable way? What are

byte[] to unsigned BigInteger?

只愿长相守 提交于 2019-12-28 06:29:07
问题 Motivation: I would like to convert hashes (MD5/SHA1 etc) into decimal integers for the purpose of making barcodes in Code128C. For simplicity, I prefer all the resulting (large) numbers to be positive. I am able to convert byte[] to BigInteger in C#... Sample from what I have so far: byte[] data; byte[] result; BigInteger biResult; result = shaM.ComputeHash(data); biResult = new BigInteger(result); But (rusty CS here) am I correct that a byte array can always be interpreted in two ways: (A):

byte[] to unsigned BigInteger?

旧时模样 提交于 2019-12-28 06:29:01
问题 Motivation: I would like to convert hashes (MD5/SHA1 etc) into decimal integers for the purpose of making barcodes in Code128C. For simplicity, I prefer all the resulting (large) numbers to be positive. I am able to convert byte[] to BigInteger in C#... Sample from what I have so far: byte[] data; byte[] result; BigInteger biResult; result = shaM.ComputeHash(data); biResult = new BigInteger(result); But (rusty CS here) am I correct that a byte array can always be interpreted in two ways: (A):

What is the best way to represent arbitrarily big numbers in c?

可紊 提交于 2019-12-28 06:24:27
问题 I'm working on a project that requires me to work with numbers larger than the largest numerical datatype in c. I was thinking of using structs with bit fields to represent this, but it's already smelling bad. Anyone got any tips? (Not looking for a library, more of a thought process to go behind doing something like this.) 回答1: I suggest to first check out the GNU MP Bignum library. If licensing is a problem you have to roll your own. My first choice for the data-type would be a simple array

Often big numbers become negative

早过忘川 提交于 2019-12-28 04:24:04
问题 Since I started using eclipse for project euler, I noticed that big numbers sometime become a seemingly random negative numbers. I suppose this has something to do with passing the boudry of the type. I'll be glad if you could explain to me how these negative numbers are generated and what is the logic behind it. Also, how can I avoid them (preferable not with BigInteger class). Danke!=) 回答1: This image shows what you're looking for. In your case it's obviously larger numbers, but the

Calculate square root of a BigInteger (System.Numerics.BigInteger)

坚强是说给别人听的谎言 提交于 2019-12-27 23:41:35
问题 .NET 4.0 provides the System.Numerics.BigInteger type for arbitrarily-large integers. I need to compute the square root (or a reasonable approximation -- e.g., integer square root) of a BigInteger . So that I don't have to reimplement the wheel, does anyone have a nice extension method for this? 回答1: Check if BigInteger is not a perfect square has code to compute the integer square root of a Java BigInteger. Here it is translated into C#, as an extension method. public static BigInteger Sqrt

JavaScript summing large integers

♀尐吖头ヾ 提交于 2019-12-27 11:05:46
问题 In JavaScript I would like to create the binary hash of a large boolean array (54 elements) with the following method: function bhash(arr) { for (var i = 0, L = arr.length, sum = 0; i < L; sum += Math.pow(2,i)*arr[i++]); return sum; } In short: it creates the smallest integer to store an array of booleans in. Now my problem is that javascript apparently uses floats as default. The maximum number I have to create is 2^54-1 but once javascript reaches 2^53 it starts doing weird things: