biginteger

How can I decrease the time complexity for this question?

回眸只為那壹抹淺笑 提交于 2019-12-20 07:17:27
问题 I am solving a problem such that for every given number between range (inclusive of boundary integers), I calculate the sum of the modified value of those numbers. For example, x = 388,822,442 f(388,822,442)=3800200402 i.e; makes zeros for the identical digits. f(x) is modified value. I iterated through each digit of an integer and made it zero if repeated. BufferedReader bf = new BufferedReader(newInputStreamReader(System.in)); String s[], s1[]; s = bf.readLine().trim().split("\\s+"); s1 =

Calculating factorials with numbers bigger than ints and longs in java?

孤街浪徒 提交于 2019-12-20 02:31:33
问题 Been searching here and google for a couple of days as well as asking my programming friends. Unfortunately, i still don't understand how to change my code... My program calculates the factorial of a given number. It then provides a number which represents how many digits the factorials answer includes. Then it sums the values of those digits together to give a total. My program works for any number between 1! and 31!... If you put in anything over 31! (for example 50! or 100!) it doesn't

Printing very big BigIntegers

ⅰ亾dé卋堺 提交于 2019-12-19 17:36:22
问题 I'm trying to figure out the following issue related to BigIntegers in Java 7 x64. I am attempting to calculate a number to an extremely high power. Code is below, followed by a description of the problem. import java.math.BigInteger; public class main { public static void main(String[] args) { // Demo calculation; Desired calculation: BigInteger("4096").pow(800*600) BigInteger images = new BigInteger("2").pow(15544); System.out.println( "The number of possible 16 bpc color 800x600 images is:

Does BigInteger ever overflows?

落花浮王杯 提交于 2019-12-19 13:45:11
问题 The API docs says All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation. Does this implies BigInteger will never overflow assuming you have sufficient memory available ? If so why do we let some "type" overflow and some don't ? As the language evolves, will it favor the types which hide overflowing mechanism from the programmers ? 回答1: BigInteger will never overflow assuming you have enough

Implementation of Fermat's primality test

≡放荡痞女 提交于 2019-12-19 10:49:26
问题 Who wants to help me with my homework? I'm try to implement Fermat's primality test in Java using BigIntegers. My implementation is as follows, but unfortunately it doesn't work. Any ideas? public static boolean checkPrime(BigInteger n, int maxIterations) { if (n.equals(BigInteger.ONE)) return false; BigInteger a; Random rand = new Random(); for (int i = 0; i < maxIterations; i++) { a = new BigInteger(n.bitLength() - 1, rand); a = a.modPow(n.subtract(BigInteger.ONE), n); if (!a.equals

How to create BigInteger of 256 bits with all bits set

核能气质少年 提交于 2019-12-19 10:12:19
问题 How can I create a BigInteger with 256 bits that are all set ? I've already tried the following: BigInteger.valueOf(0xFFFFFFFFFFFFFFFFL) But it doesn't give me the desired result: int bitCount = b.bitCount();// 0 int bitLength = b.bitLength();// 0 What I basically need is a number containing 256 bits which are all set. Tnx! 回答1: Try this: BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE) It calculates first 2^256, which is in binary a one followed by 256 zeroes, and then subtracts one

Convert BigInteger to Shorter String in Java

北城余情 提交于 2019-12-19 08:23:07
问题 I'm looking for a way to convert a BigInteger into a very short String (shortest possible). The conversion needs to be reversible. The security of the conversion is not a big deal in this case. Would anyone have recommendations or samples of how they would go about solving this problem? 回答1: One easy way is to use BigInteger.toString(Character.MAX_RADIX) . To reverse, use the following constructor: BigInteger(String val, int radix) . 回答2: You can use a Base64 encoding. Note that this example

Python long multiplication

空扰寡人 提交于 2019-12-19 08:22:50
问题 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

Convert BigInteger to Shorter String in Java

北城以北 提交于 2019-12-19 08:22:19
问题 I'm looking for a way to convert a BigInteger into a very short String (shortest possible). The conversion needs to be reversible. The security of the conversion is not a big deal in this case. Would anyone have recommendations or samples of how they would go about solving this problem? 回答1: One easy way is to use BigInteger.toString(Character.MAX_RADIX) . To reverse, use the following constructor: BigInteger(String val, int radix) . 回答2: You can use a Base64 encoding. Note that this example

Convert BigInteger to Shorter String in Java

白昼怎懂夜的黑 提交于 2019-12-19 08:22:00
问题 I'm looking for a way to convert a BigInteger into a very short String (shortest possible). The conversion needs to be reversible. The security of the conversion is not a big deal in this case. Would anyone have recommendations or samples of how they would go about solving this problem? 回答1: One easy way is to use BigInteger.toString(Character.MAX_RADIX) . To reverse, use the following constructor: BigInteger(String val, int radix) . 回答2: You can use a Base64 encoding. Note that this example