biginteger

Python long multiplication

不问归期 提交于 2019-12-19 08:21:10
问题 I'm in need of an algorithm faster than the current normal Python long multiplication. I tried to find a decent Karatsuba implementation, but I can't. def main(): a=long(raw_input()) if(a<0): a=a*-1 a=((a*(a+1)/2)-1) print(-a) else: a=(a*(a+1))/2 print(a) main() As you see, it's nothing complicated, just a few multiplications. But it has to handle numbers with up to 100000 digits in under 2.5 sec. I'd like some snippet of a function or just a link to some implementation of a faster

How do you raise a Java BigInteger to the power of a BigInteger without doing modular arithmetic?

独自空忆成欢 提交于 2019-12-19 06:03:33
问题 I'm doing some large integer computing, and I need to raise a BigInteger to the power of another BigInteger. The .pow() method does what I want, but takes an int value as an argument. The .modPow method takes a BigInteger as an argument, but I do not want an answer congruent to the value I'm trying to compute. My BigInteger exponent is too large to be represented as an int, can someone suggest a way to work around this limitation? 回答1: You shouldn't try to calculate the power of an extremely

I need very big array length(size) in C#

只谈情不闲聊 提交于 2019-12-19 03:44:27
问题 public double[] result = new double[ ??? ]; I am storing results and total number of the results are bigger than the 2,147,483,647 which is max int32. I tried biginteger, ulong etc. but all of them gave me errors. How can I extend the size of the array that can store > 50,147,483,647 results (double) inside it? Thanks... 回答1: An array of 2,147,483,648 double s will occupy 16GB of memory. For some people, that's not a big deal. I've got servers that won't even bother to hit the page file if I

Arbitrary-Precision Math in PHP

淺唱寂寞╮ 提交于 2019-12-18 19:07:25
问题 I'm currently trying to figure out how to work with arbitrary-precision numbers in PHP. So I guess my first question would be what exactly is arbitrary-precision math. I tried Googling for a good definition but for some reason nobody can put it in simple enough words. Second, what are the differences between the BCMath and GMP libraries in PHP? I've heard claims that GMP's API is "fresher", but idk. Is one better? And my final question would be what type of numbers BCMath/GMP takes. Obviously

How do you convert A binary number to a BigInteger in Java?

天大地大妈咪最大 提交于 2019-12-18 18:54:10
问题 I needed to convert a very big binary value into its decimal equivalent. As it is a big integer I was using BigInteger. So how do I convert this binary number to a BigInteger? 回答1: If you have the String representation of your binary number, provide it to this overloaded BigInteger constructor to create an instance: BigInteger(String val, int radix); In your case, radix is clearly 2, i.e. you can use something like this: BigInteger yourNumber = new BigInteger("101000101110...1010", 2); 回答2:

Division of Large numbers

為{幸葍}努か 提交于 2019-12-18 07:11:22
问题 Is there any faster method for division of large integers(having 1000 digits or more) other than the school method? 回答1: 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

% operator for BigInteger in java

风格不统一 提交于 2019-12-18 04:39:10
问题 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? 回答1: 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

How do I generate a random n digit integer in Java using the BigInteger class?

[亡魂溺海] 提交于 2019-12-18 03:56:35
问题 I am unsure about how to generate a random n digit integer in Java using the BigInteger class. 回答1: private static Random rnd = new Random(); public static String getRandomNumber(int digCount) { StringBuilder sb = new StringBuilder(digCount); for(int i=0; i < digCount; i++) sb.append((char)('0' + rnd.nextInt(10))); return sb.toString(); } And then you can use it: new BigInteger(getRandomNumber(10000)) 回答2: According to the docs, there is a constructor to do what you want in java 6: BigInteger

MySQL: bigint Vs int

☆樱花仙子☆ 提交于 2019-12-17 23:33:18
问题 I have been using int(10) and just noticed that Wordpress uses bigint(20) - What is different to use bigint(20) and int(10) for id auto increment? Which one should I use for id column? `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, Vs `id` int(10) unsigned NOT NULL AUTO_INCREMENT, Thanks. 回答1: The difference is purely in the maximum value which can be stored (18,446,744,073,709,551,615 for the bigint(20) and 4,294,967,295 for the int(10), I believe), as per the details on the MySQL Numeric

How do I do big integers in Fortran?

 ̄綄美尐妖づ 提交于 2019-12-17 20:27:41
问题 I need to generate some big integers. See example below. Input Result 40 165580141 80 37889062373143906 120 8670007398507948658051921 160 1983924214061919432247806074196061 200 453973694165307953197296969697410619233826 Here is my Fortran code: program cycle use iso_fortran_env implicit none character(200) :: str integer :: n integer(kind=int64) :: x1, result, x2, x3 do n = 40, 500, 40 x1 = n result = 1 x2 = 0 x3 = 1 do if (x1 > 1) then x2 = result result = result + x3 x3 = x2 x1 = x1 - 1