bignum

Square root of bignum using GMP

[亡魂溺海] 提交于 2019-11-30 15:31:14
问题 I need to get the square root of a 210 digit number accurately, I thought GMP was the right tool for the job, what am I doing wrong? #include <stdlib.h> #include <stdio.h> #include "gmp.h" int main (int argc, char *argv[]) { mpz_t sq_me, sq_out, test; mpz_init(sq_me); mpz_init(sq_out); mpz_init(test); mpz_set_str (sq_me, argv[1], 10); mpz_sqrt(sq_out, sq_me); mpz_mul(test,sq_out,sq_out); gmp_printf ("%Zd\n\n", sq_out); gmp_printf ("%Zd\n\n", test); return 0; } Input:

Square root of bignum using GMP

妖精的绣舞 提交于 2019-11-30 15:04:15
I need to get the square root of a 210 digit number accurately, I thought GMP was the right tool for the job, what am I doing wrong? #include <stdlib.h> #include <stdio.h> #include "gmp.h" int main (int argc, char *argv[]) { mpz_t sq_me, sq_out, test; mpz_init(sq_me); mpz_init(sq_out); mpz_init(test); mpz_set_str (sq_me, argv[1], 10); mpz_sqrt(sq_out, sq_me); mpz_mul(test,sq_out,sq_out); gmp_printf ("%Zd\n\n", sq_out); gmp_printf ("%Zd\n\n", test); return 0; } Input: 24524664490027821197651766357308801846702678767833275974341445171506160083003858

Fastest way to convert binary to decimal?

狂风中的少年 提交于 2019-11-29 02:19:06
I've got four unsigned 32-bit integers representing an unsigned 128-bit integer, in little endian order: typedef struct { unsigned int part[4]; } bigint_t; I'd like to convert this number into its decimal string representation and output it to a file. Right now, I'm using a bigint_divmod10 function to divide the number by 10, keeping track of the remainder. I call this function repeatedly, outputting the remainder as a digit, until the number is zero. It's pretty slow. Is this the fastest way to do it? If so, is there a clever way to implement this function that I'm not seeing? I've tried

Large integers in javascript (more the 2^53-1)

六眼飞鱼酱① 提交于 2019-11-28 12:38:41
What is general principals to operate with large integers in javascript? Like in libraries for bigint? How i do it by myself? You may take a look at this implementation . You may also find other implementations useful. Another option I've used in the past, is to hand off those operations to a calculation server via jsonp. If you're working with such large numbers, you likely want the improved performance and precision this can give you. 来源: https://stackoverflow.com/questions/3579153/large-integers-in-javascript-more-the-253-1

How to Code a Solution To Deal With Large Numbers?

狂风中的少年 提交于 2019-11-28 08:31:27
I'm doing some Project Euler problems and most of the time, the computations involve large numbers beyond int, float, double etc. Firstly, I know that I should be looking for more efficient ways of calculation so as to avoid the large number problem. I've heard of the Bignum libraries. But, for academics interests, I'd like to know how to code my own solution to this problem. Can any expert please help me out? (My language is C) You need to store the big numbers in a base that your computer can easily handle with its native types, and then store the digits in a variable length array. I'd

Efficient Exponentiation For HUGE Numbers (I'm Talking Googols)

Deadly 提交于 2019-11-28 05:50:07
问题 I am in the midst of solving a simple combination problem whose solution is 2^(n-1). The only problem is 1 <= n <= 2^31 -1 (max value for signed 32 bit integer) I tried using Java's BigInteger class but It times out for numbers 2^31/10^4 and greater, so that clearly doesn't work out. Furthermore, I am limited to using only built-in classes for Java or C++. Knowing I require speed, I chose to build a class in C++ which does arithmetic on strings. Now, when I do multiplication, my program

How to implement long division for enormous numbers (bignums)

和自甴很熟 提交于 2019-11-27 18:29:11
问题 I'm trying to implement long division for bignums. I can't use a library like GMP unfortunately due to the limitations of embedded programming. Besides, i want the intellectual exercise of learning how to implement it. So far i've got addition and multiplication done using any-length arrays of bytes (so each byte is like a base-256 digit). I'm just trying to get started on implementing division / modulus and i want to know where to start? I've found lots of highly-optimised (aka unreadable)

Fastest way to convert binary to decimal?

北战南征 提交于 2019-11-27 16:38:53
问题 I've got four unsigned 32-bit integers representing an unsigned 128-bit integer, in little endian order: typedef struct { unsigned int part[4]; } bigint_t; I'd like to convert this number into its decimal string representation and output it to a file. Right now, I'm using a bigint_divmod10 function to divide the number by 10, keeping track of the remainder. I call this function repeatedly, outputting the remainder as a digit, until the number is zero. It's pretty slow. Is this the fastest way

What does GCC __attribute__((mode(XX)) actually do?

我们两清 提交于 2019-11-27 15:42:15
问题 This arose from a question earlier today on the subject of bignum libraries and gcc specific hacks to the C language. Specifically, these two declarations were used: typedef unsigned int dword_t __attribute__((mode(DI))); On 32 bit systems and typedef unsigned int dword_t __attribute__((mode(TI))); On 64-bit systems. I assume given this is an extension to the C language that there exists no way to achieve whatever it achieves in current (C99) standards. So my questions are simple: is that

Can long integer routines benefit from SSE?

有些话、适合烂在心里 提交于 2019-11-27 14:21:09
I'm still working on routines for arbitrary long integers in C++. So far, I have implemented addition/subtraction and multiplication for 64-bit Intel CPUs. Everything works fine, but I wondered if I can speed it a bit by using SSE. I browsed through the SSE docs and processor instruction lists, but I could not find anything I think I can use and here is why: SSE has some integer instructions, but most instructions handle floating point. It doesn't look like it was designed for use with integers (e.g. is there an integer compare for less?) The SSE idea is SIMD (same instruction, multiple data),