biginteger

Why to use higher base for implementing BigInt?

馋奶兔 提交于 2019-11-29 15:43:10
I'm trying to implement BigInt and have read some threads and articles regarding it, most of them suggested to use higher bases(256 or 2^32 or even 2^64). Why higher bases are good for this purpose? Other question I have is how am I supposed to convert a string into higher base(>16). I read there is no standard way, except for base64. And the last question, how do I use those higher bases. Some examples would be great. The CPU cycles spent multiplying or adding a number that fits in a register tend to be identical. So you will get the least number of iterations, and best performance, by using

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

≯℡__Kan透↙ 提交于 2019-11-29 14:52:18
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 | String Version bigIntVar.ToQuickString() //Time: 12.500s | Base 10 | StringBuilder Version bigIntVar

Java BigInteger vs Mono .net BigInteger

僤鯓⒐⒋嵵緔 提交于 2019-11-29 14:42:10
I use in .Net project mono implementation of BigInteger ( link ) In Java I use java.math.BigInteger. The same code produces different results in Java. .Net code String inputBytes = "8E5BD77F0DCC30864634C134E28BFB42A149675A320786B616F4530708350D270353C30A40450325801B7AFED12BCCA274B8187072A89CC0CC3F95A24A8251243C1835898246F4D64CA3AC61DB841518F0E8FBC8996A40EB626153AE7F0BB87FD713FAC522719431428DE178E780A3FA45788A72C431926AED990E6DA268D2CC"; String modulus =

Division of Large numbers

陌路散爱 提交于 2019-11-29 12:05:19
Is there any faster method for division of large integers(having 1000 digits or more) other than the school method? Wikipedia lists multiple division algorithms . See Computational complexity of mathematical operations which lists Schoolbook long division as O(n^2) and Newton's method as M(n) where M is the complexity of the multiplication algorithm used, which could be as good as O(n log n 2^( log* n)) asymptotically. Note from the discussion of one of the multiplication algorithms that the best algorithm asymptotically is not necessarily the fastest for "small" inputs: In practice the

Converting a big integer to decimal string

放肆的年华 提交于 2019-11-29 12:01:43
At the risk of having this question voted as a duplicate, or even to have it closed, I had this question has come up. Background In "normal" data types such as int, long long, etc..., to convert from the binary numeric value to a decimal string, you would do the following (in pseudo code): Set length = 0 Set divisor to largest base10 value the data type will hold (Divisor). Loop Divide number in question by divisor. Place result in a string at position length. Increment the length by 1. Divide the divisor by 10. Reverse the string. Print the string. The actual implementation in (most) any

Log of a very large number

旧巷老猫 提交于 2019-11-29 11:21:25
I'm dealing with the BigInteger class with numbers in the order of 2 raised to the power 10,000,000. The BigInteger Log function is now the most expensive function in my algorithm and I am desperately looking for an alternative. Since I only need the integral part of the log, I came across this answer which seems brilliant in terms of speed but for some reason I am not getting accurate values. I do not care about the decimal part but I do need to get an accurate integral part whether the value is floored or ceiled as long as I know which. Here is the function I implemented: public static

What should I use for a BigInt class in .NET?

徘徊边缘 提交于 2019-11-29 11:03:44
Until BCL finally ships System.Numeric.BigInt , what do you guys use for arbitrary precision integers? You could try mine on codeplex: BigInteger Or here's another: codeproject F# has Microsoft.FSharp.Math.BigInt and Microsoft.FSharp.Math.BigNum . Judah Gabriel Himango Also, the J# library includes a BigInt type . Thankfully, all this will be sorted out when the BCL finally ships a standardized BigInt. UPDATE 2012 System.Numberics.BigInteger is now included in the .NET framework. Have a look at IronRuby.StandardLibrary.BigDecimal.BigDecimal 来源: https://stackoverflow.com/questions/567753/what

Difference between BigInteger.probablePrime() and other primality algorithms in java

ⅰ亾dé卋堺 提交于 2019-11-29 10:51:26
I am implementing an RSA encryption program using Java. Right now I am using BigInteger.probablePrime(1024, rnd) to get prime numbers. Here rnd is a random number generated by Random rnd = new Random() . I need to test various speeds of encryption. My questions are: what algorithm does the BigInteger.probablePrime(1024, rnd) use? what is the difference between the algorithm above and other algorithms: like Rabin-Miller, Fermats, Lucas-Lehmer? Thank you. BigInteger 's probable prime methods use both the Miller-Rabin and Lucas-Lehmer algorithms to test primality. See the internal method

BitShifting with BigIntegers in Java

不想你离开。 提交于 2019-11-29 10:31:54
I am implementing DES Encryption in Java with use of BigIntegers. I am left shifting binary keys with Java BigIntegers by doing the BigInteger.leftShift(int n) method. Key of N (Kn) is dependent on the result of the shift of Kn-1. The problem I am getting is that I am printing out the results after each key is generated and the shifting is not the expected out put. The key is split in 2 Cn and Dn (left and right respectively). I am specifically attempting this: "To do a left shift, move each bit one place to the left, except for the first bit, which is cycled to the end of the block. " It

% operator for BigInteger in java

£可爱£侵袭症+ 提交于 2019-11-29 09:33:26
How to use a%b with big integers? like ... BigInteger val = new BigInteger("1254789363254125"); ... boolean odd(val){ if(val%2!=0) return true; return false; ... Eclipse says that operator % is undefined for BigInteger. Any ideas? Like this: BigInteger val = new BigInteger("1254789363254125"); public boolean odd(BigInteger val) { if(!val.mod(new BigInteger("2")).equals(BigInteger.ZERO)) return true; return false; } Or as user Duncan suggested in a comment, we can take out the if statement altogether like so: BigInteger val = new BigInteger("1254789363254125"); public boolean odd(BigInteger val