biginteger

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

自作多情 提交于 2019-12-05 01:14:49
问题 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,

infinite loop in c++ [duplicate]

本小妞迷上赌 提交于 2019-12-05 00:04:15
问题 This question already has answers here : Infinite loop with cin when typing string while a number is expected (4 answers) Closed 9 months ago . 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

Sum a list of BigIntegers

筅森魡賤 提交于 2019-12-04 22:56:12
I've looked all over but can't figure this out. How do you sum a list of BigIntegers? Using System.Numerics; Using System.Linq; List<BigInteger> bigInts = new List<BigInteger>(); BigInteger sum = bigInts.Sum(); // doesn't work BigInteger sum = bigInts.Sum<BigInteger>(); // doesn't work BigInteger sum = bigInts.Sum(x => x); // doesn't work Do you have to do this? BigInteger sum = new BigInteger(0); foreach(BigInteger bigint in bigInts) sum += bigint; Aggregate function is more general version of Sum: var bigInts = new List<System.Numerics.BigInteger>(); bigInts.Add(new System.Numerics

How to pass BigInteger to a Signature function

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 21:31:36
Here I'm implementing digital signature using RSA. I read a plain text from a file and get MD5 i.e instance of a MessageDigest of the plain text and converting that plain text to BigInteger here this bigInteger should be signed. MessageDigest m1 = MessageDigest.getInstance("MD5"); m1.update(bFile); byte [] digest1 = m1.digest(); for(int i=0; i < digest1.length ; i++){ System.out.println("b["+i+"]="+digest1[i]); } BigInteger bi = new BigInteger(digest1); //here I dont know how to pass BigInteger to Signature function. Could someone please help me with it. You don't. You get the bytes out of the

BigInteger numbers implementation and performance

99封情书 提交于 2019-12-04 19:44:56
I have written a BigInteger class in C++ that should be able to do operations on all numbers with any size. Currently I am trying to achieve a very fast multiplication method by comparing the existing algorithms and test for which amount of digits they work best and I ran into very unexpected results.I tried to do 20 multiplications of 500-digit and I timed them. This was the result: karatsuba: 14.178 seconds long multiplication: 0.879 seconds Wikipedia told me It follows that, for sufficiently large n, Karatsuba's algorithm will perform fewer shifts and single-digit additions than longhand

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

◇◆丶佛笑我妖孽 提交于 2019-12-04 10:25:19
问题 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

128 bit arithmetic on x64 in C

有些话、适合烂在心里 提交于 2019-12-04 10:04:59
When implementing bignums on x86, obviously the most efficient choice for digit size is 32 bits. However, you need arithmetic up to twice the digit size (i.e. 32+32=33, 32*32=64, 64/32=32). Fortunately, not only does x86 provide this, but it's also accessible from portable C (uint64_t). Similarly, on x64 it would be desirable to use 64-bit digits. This would require 128 bit arithmetic (i.e. 64+64=65, 64*64=128, 128/64=64). Fortunately, x64 provides this. Unfortunately, it's not accessible from portable C, though obviously one could dip into assembly. So my question is whether it's accessible

Java Mutable BigInteger Class

杀马特。学长 韩版系。学妹 提交于 2019-12-04 08:16:35
问题 I am doing calculations with BigIntegers that uses a loop that calls multiply() about 100 billion times, and the new object creation from the BigInteger is making it very slow. I was hoping somebody had written or found a MutableBigInteger class. I found the MutableBigInteger in the java.math package, but it is private and when I copy the code into a new class, many errors come up, most of which I don't know how to fix. What implementations exist of a Java class like MutableBigInteger that

Why doesn't my processor have built-in BigInt support?

一个人想着一个人 提交于 2019-12-04 03:09:37
As far as I understood it, BigInts are usually implemented in most programming languages as arrays containing digits, where, eg.: when adding two of them, each digit is added one after another like we know it from school, e.g.: 246 816 * * ---- 1062 Where * marks that there was an overflow. I learned it this way at school and all BigInt adding functions I've implemented work similar to the example above. So we all know that our processors can only natively manage ints from 0 to 2^32 / 2^64 . That means that most scripting languages in order to be high-level and offer arithmetics with big

How to work on big integers that don't fit into any of language's data structures

这一生的挚爱 提交于 2019-12-04 01:33:56
I'm trying to solve a programming contest's preliminary problems and for 2 of the problems I have to calculate and print some very big integers(like 100!, 2^100). I also need a fast way to calculate powers of this big integers. Can you advice me some algorithms or data structures for this?(btw, I read C Interfaces and Implementations 'arbitrary precision arithmetic' section but it doesn't help for pow()) EDIT: I think exponentiation by squaring method and bit-shifting will work for power but I also need a fast way to calculate factorials for this ints. Thanks. EDIT2: For those who are