bignum

Big Number Subtraction in C

人走茶凉 提交于 2019-12-06 02:21:51
问题 I just finished my exam in an introductory C course about 20 minutes ago. The first question on the exam caught me somewhat off guard, and involved finding the difference two large numbers. The goal was to take two structures (N1 and N2) by value, and store the difference in a structure passed by reference (N3). We were allowed to assume N3 was initiated with all '0's. The MAX size can be anything, so the solution still has to work if the numbers are over 100 digits. Here's the base code

Why does division become faster with very large numbers

风格不统一 提交于 2019-12-05 21:29:34
I wanted to see how constant the division operation is, so I ran this code import time def di(n): n/101 i = 10 while i < 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000: start = time.clock() di(i) end = time.clock() print("On " + str(i) + " " + str(end-start)) i *= 10000 And this was the output I got On 10 5.98714047756e-06 On 100000 4.7041818038e-06 On 1000000000 2.56591734753e-06 On 10000000000000 2.99357023878e-06 On 100000000000000000 2.99357023878e-06 On 1000000000000000000000 2.99357023879e-06 On 10000000000000000000000000 2.99357023878e-06

Accessing the highest digits of large numbers from Python long

岁酱吖の 提交于 2019-12-05 18:05:30
I'm working with numbers with tens of thousands of digits in python. The long type works beautifully in performing math on these numbers, however I'm unable to access the highest digits of these numbers in a sufficiently fast way. Note that I don't know exactly how many digits the number contains. The "highest digits" refers to the digits in the most significant place, the lowest digits can be accessed quickly using modulus. I can think of two ways to access these digits in python but they're both too slow for my purposes. I have tried converting to a string and accessing digits through array

Speed up x64 assembler ADD loop

时光毁灭记忆、已成空白 提交于 2019-12-05 15:44:21
问题 I'm working on arithmetic for multiplication of very long integers (some 100,000 decimal digits). As part of my library I to add two long numbers. Profiling shows that my code runs up to 25% of it's time in the add() and sub() routines, so it's important they are as fast as possible. But I don't see much potential, yet. Maybe you can give me some help, advice, insight or ideas. I'll test them and get back to you. So far my add routine does some setup and then uses a 8-times unrolled loop: mov

Division of a big number of 100 digits stored as string

半世苍凉 提交于 2019-12-05 09:13:53
I have a 100 digit number stored as string. I want to divide this number with an integer less than 10. How do I efficiently divide a big integer stored as a string with an integer? You can check the big integer library . You can use this library in a C++ program to do arithmetic on integers of size limited only by your computer's memory. The library provides BigUnsigned and BigInteger classes that represent nonnegative integers and signed integers, respectively. Most of the C++ arithmetic operators are overloaded for these classes, so big-integer calculations are as easy as: #include

Openssl, Invalid arguments ' Candidates are: int BN_set_word(bignum_st *, ?) '

*爱你&永不变心* 提交于 2019-12-04 19:30:14
I am using OpenSSL for a cuda project. I just imported all the project from win to linux (Eclipse) I solved all the dependencies except this annoying error: Invalid arguments ' Candidates are: int BN_set_word(bignum_st *, ?) ' for this line: BN_set_word(two, 2); and the function itself says in the bn.h int BN_set_word(BIGNUM *a, BN_ULONG w); Where BN_ULONG is defined as: #define BN_ULONG unsigned long Neither it works if I do something like unsigned long q = 2; BN_set_word(two, q); Because it returns Invalid arguments ' Candidates are: int BN_set_word(bignum_st *, ?) ' or BN_ULONG q = 2; BN

How can I printf a Perl bignum without losing precision?

瘦欲@ 提交于 2019-12-04 12:19:18
#!/usr/bin/perl use strict; use warnings; my $s = "1234567890.123456789"; { no bignum; printf "bignum==%s\n", bignum::in_effect() // 0; my $x = $s; printf "%29s\n", $x; printf "%29.9f\n\n", $x; } { use bignum; printf "bignum==%s\n", bignum::in_effect() // 0; my $x = $s; printf "%29s\n", $x; printf "%29.9f\n\n", $x; } My Perl's printf (ActiveState v5.10.1 built for darwin-thread-multi-2level) using the %f conversion doesn't honor my value past the 1e-6 digit, even when using bignum: $ t.pl bignum==0 1234567890.123456789 1234567890.123456717 bignum==1 1234567890.123456789 1234567890.123456717

Big Number Subtraction in C

妖精的绣舞 提交于 2019-12-04 07:33:36
I just finished my exam in an introductory C course about 20 minutes ago. The first question on the exam caught me somewhat off guard, and involved finding the difference two large numbers. The goal was to take two structures (N1 and N2) by value, and store the difference in a structure passed by reference (N3). We were allowed to assume N3 was initiated with all '0's. The MAX size can be anything, so the solution still has to work if the numbers are over 100 digits. Here's the base code (original may be slightly different, this is from memory) #include <stdio.h> #include <stdlib.h> /* MAX can

Speed up x64 assembler ADD loop

独自空忆成欢 提交于 2019-12-04 02:30:04
I'm working on arithmetic for multiplication of very long integers (some 100,000 decimal digits). As part of my library I to add two long numbers. Profiling shows that my code runs up to 25% of it's time in the add() and sub() routines, so it's important they are as fast as possible. But I don't see much potential, yet. Maybe you can give me some help, advice, insight or ideas. I'll test them and get back to you. So far my add routine does some setup and then uses a 8-times unrolled loop: mov rax, QWORD PTR [rdx+r11*8-64] mov r10, QWORD PTR [r8+r11*8-64] adc rax, r10 mov QWORD PTR [rcx+r11*8

custom data type in C

こ雲淡風輕ζ 提交于 2019-12-03 14:07:17
I am working with cryptography and need to use some really large numbers. I am also using the new Intel instruction for carryless multiplication that requires m128i data type which is done by loading it with a function that takes in floating point data as its arguments. I need to store 2^1223 integer and then square it and store that value as well. I know I can use the GMP library but I think it would be faster to create two data types that both store values like 2^1224 and 2^2448. It will have less overhead.I am going to using karatsuba to multiply the numbers so the only operation I need to