biginteger

Alternative to BigInteger.ModPow(); in C#

穿精又带淫゛_ 提交于 2019-12-03 23:18:19
i'm looking for an alternative to the BigInteger package of C# which has been introduced with NET 4.x. The mathematical operations with this object are terribly slow, I guess this is caused by the fact that the arithmetics are done on a higher level than the primitive types - or badly optimized, whatever. Int64/long/ulong or other 64bit-numbers are way to small and won't calculate correctly - I'm talking about 64bit-integer to the power of 64-bit integers. Hopefully someone can suggest my something. Thanks in advance. Honestly, if you have extremely large numbers and need to do heavy

Parallel calculation of BigInteger factorial

不羁的心 提交于 2019-12-03 17:26:21
As a part of my BigDecimal library, I need to calculate the factorial of any given non negative integer. So I'm using .Net 4.0 's System.Numerics.BigInteger to be able to store huge numbers. Here is the function I'm using: private BigInteger Factorial(BigInteger x) { BigInteger res = x; x--; while (x > 1) { res *= x; x--; } return res; } It's working but not optimized. Now I want to use parallel computing, so here is what I've tried: (I have no experience with parallel programming) public BigInteger Factorial(long x) { BigInteger res = 1; ParallelLoopResult r = Parallel.For(2L, (x + 1), i =>

What is an efficient way to convert a bignum type structure to a human readable string?

喜你入骨 提交于 2019-12-03 16:47:44
I've got a bit of a problem. In order to grow in my knowledge of C, I've decided to try to implement a basic bigint library. The core of the bigint structure will be an array of 32 bit integers, chosen because they will fit in a register. This will allow me to do operations between digits that will overflow in a 64 bit integer (which will also fit in a register, as I'm on x86-64), and I can bitshift out each part of the result. I've implemented basic addition, and to test that it is working, I have to print the array. For my own testing purposes, it's fine if I use printf() and output each

BigInteger.toByteArray() returns purposeful leading zeros?

…衆ロ難τιáo~ 提交于 2019-12-03 16:22:30
I'm transforming bigints into binary, radix16 and radix64 encoding and seeing mysterious msb zero paddings. Is this a biginteger problem that I can workaround by stripping zero padding or perhaps doing something else? My test code: String s; System.out.printf( "%s length %d\n", s = "123456789A", (new BigInteger( s, 16 )).toByteArray().length ); System.out.printf( "%s length %d\n", s = "F23456789A", (new BigInteger( s, 16 )).toByteArray().length ); Produces output: 123456789A length 5 F23456789A length 6 Of which the longer array has zero padding at the front. Upon inspection of BigInteger

An efficient algorithm to calculate the integer square root (isqrt) of arbitrarily large integers

你。 提交于 2019-12-03 14:31:51
Notice For a solution in Erlang or C / C++ , go to Trial 4 below. Wikipedia Articles Integer square root The definition of "integer square root" could be found here Methods of computing square roots An algorithm that does "bit magic" could be found here [ Trial 1 : Using Library Function ] Code isqrt(N) when erlang:is_integer(N), N >= 0 -> erlang:trunc(math:sqrt(N)). Problem This implementation uses the sqrt() function from the C library, so it does not work with arbitrarily large integers (Note that the returned result does not match the input. The correct answer should be

infinite loop in c++ [duplicate]

a 夏天 提交于 2019-12-03 14:18:49
This question already has answers here : Infinite loop with cin when typing string while a number is expected (4 answers) I'm learning C++ and writing little programs as I go along. The following is one such program: // This program is intended to take any integer and convert to the // corresponding signed char. #include <iostream> int main() { signed char sch = 0; int n = 0; while(true){ std::cin >> n; sch = n; std::cout << n << " --> " << sch << std::endl; } } When I run this program and keep inputs at reasonably small absolute values, it behaves as expected. But when I enter larger inputs,

Increasing Java's BigInteger performance

北城以北 提交于 2019-12-03 12:13:42
问题 How to increase performance of Java's Big Integer? For example, this factorial program: import java.math.*; class Fac { public static void main(String[] args) { BigInteger i = BigInteger.ONE; for(BigInteger z=BigInteger.valueOf(2);z.compareTo(BigInteger.valueOf(99999)) != 0;) { i = i.multiply(z); z = z.add(BigInteger.ONE); } System.out.println( i ); } } That program completed in 31.5 s Where's in C++: #include <iostream> #include <gmpxx.h> using namespace std; int main() { mpz_class r; r = 1;

Possible to safely increment BigInteger in a thread safe way, perhaps with AtomicReference, w/o locking?

不打扰是莪最后的温柔 提交于 2019-12-03 05:51:17
A lot of our code is legacy but we are moving to a "Big Data" back-end and I'm trying to evangelize the newer API calls, encourage the use of the latest Spring libraries etc. One of our problems is application layer ID generation. For reasons I don't understand, a higher authority wants sequential BigInteger's. I would have made them random with re-generate and re-try on failed insertions but I done got vetoed. Grumbling aside, I'm in a position where I need to increment and get a BigInteger across threads and do it in a safe and performant manner. I've never used AtomicReference before but it

SHA256 Hash results different across Android & iOS for Big numbers

怎甘沉沦 提交于 2019-12-03 03:23:39
I'm trying to Hash a BigInteger/BigNum and I'm getting different results in Android/iOS. I need to get the same Hash result so that both the apps work as per the SRP protocol. On closer inspection it is working fine for positive numbers but not working for negative numbers (first nibble greater than 7). Not sure which one is correct and which one is to be adjusted to match with the other. Android: void hashBigInteger(String s) { try { BigInteger a = new BigInteger(s, 16); MessageDigest sha = MessageDigest.getInstance("SHA-256"); byte[] b = a.toByteArray(); sha.update(b, 0, b.length); byte[]

Increasing Java's BigInteger performance

半腔热情 提交于 2019-12-03 02:41:53
How to increase performance of Java's Big Integer? For example, this factorial program: import java.math.*; class Fac { public static void main(String[] args) { BigInteger i = BigInteger.ONE; for(BigInteger z=BigInteger.valueOf(2);z.compareTo(BigInteger.valueOf(99999)) != 0;) { i = i.multiply(z); z = z.add(BigInteger.ONE); } System.out.println( i ); } } That program completed in 31.5 s Where's in C++: #include <iostream> #include <gmpxx.h> using namespace std; int main() { mpz_class r; r = 1; for(int z=2;z<99999;++z) { r *= mpz_class(z); } cout << r << endl; } completed in 1.0 s And Ruby (for