biginteger

is there anyway to convert from Double to BigInteger?

情到浓时终转凉″ 提交于 2019-11-29 09:24:37
Is there anyway to convert from double value to BigInteger ? double doubleValue = 64654679846513164.2; BigInteger bigInteger = (BigInteger) doubleValue; I try to cast it but it didn't work. BigInteger is made for holding arbitrary precision integer numbers, not decimals. You can use the BigDecimal class to hold a double. BigDecimal k = BigDecimal.valueOf(doublevalue); In general, you cannot type cast a Java primitive into another class. The exceptions that I know about are the classes extending Number , such as the Long and Integer wrapper classes, which allow you to cast an int value into an

BigInteger most time optimized multiplication

此生再无相见时 提交于 2019-11-29 08:02:24
Hi I want to multiply 2 big integer in a most timely optimized way. I am currently using karatsuba algorithm. Can anyone suggest more optimized way or algo to do it. Thanks public static BigInteger karatsuba(BigInteger x, BigInteger y) { // cutoff to brute force int N = Math.max(x.bitLength(), y.bitLength()); System.out.println(N); if (N <= 2000) return x.multiply(y); // optimize this parameter // number of bits divided by 2, rounded up N = (N / 2) + (N % 2); // x = a + 2^N b, y = c + 2^N d BigInteger b = x.shiftRight(N); BigInteger a = x.subtract(b.shiftLeft(N)); BigInteger d = y.shiftRight(N

How to work with big numbers in PHP?

这一生的挚爱 提交于 2019-11-29 07:18:56
How to work with big numbers in PHP? such as (6*27^0+17*27^1+11*27^2+18*27^3+25*27^4+4*27^5)^65537 You can go for BCMath to work with big numbers. neubert GMP's actually faster than BCMath for bigintegers if you have it installed. If you have neither BCMath or GMP installed you can use phpseclib's pure-php biginteger implementation . That implementation uses GMP or BCmath if they're available, in that order, and it's own internal implementation otherwise. There are many options: The GMP extension The BCMath Extension Litipk PHP BigNumbers ( https://github.com/litipk/php-bignumbers ) Flourish

C# A random BigInt generator

南笙酒味 提交于 2019-11-29 06:10:26
I'm about to implement the DSA algorithm , but there is a problem: choose "p", a prime number with L bits, where 512 <= L <= 1024 and L is a multiple of 64 How can I implement a random generator of that number? Int64 has "only" 63 bits length. You can generate a random number with n bits using this code: var rng = new RNGCryptoServiceProvider(); byte[] bytes = new byte[n / 8]; rng.GetBytes(bytes); BigInteger p = new BigInteger(bytes); The result is, of course, random and not necessarily a prime. The BigInteger class was introduced in the .NET 4.0 Framework. For generating large prime numbers,

How can I convert an absolutely massive number to a string in a reasonable amount of time?

混江龙づ霸主 提交于 2019-11-29 05:21:55
问题 This is quite an odd problem I know, but I'm trying to get a copy of the current largest prime number in a file. Getting the number in integer form is fairly easy. I just run this. prime = 2**74207281 - 1 It takes about half a second and it works just fine. Operations are fairly quick as well. Dividing it by 10 (without decimals) to shift the digits is quick. However, str(prime) is taking a very long time. I reimplemented str like this, and found it was processing about a hundred or so digits

How to check for division by 7 for big number in C++?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 04:45:16
I have to check, if given number is divisible by 7, which is usualy done just by doing something like n % 7 == 0 , but the problem is, that given number can have up to 100000000, which doesn't fit even in long long . Another constrain is, that I have only few kilobytes of memory available, so I can't use an array. I'm expecting the number to be on stdin and output to be 1 / 0 . This is an example 34123461273648125348912534981264376128345812354821354127346821354982135418235489162345891724592183459321864592158 0 It should be possible to do using only about 7 integer variables and cin.get() . It

Big number in C++

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 04:03:03
I am trying to place a big number in a C++ variable. The number is 600851475143 I tried unsigned long long int but got an error saying it the constant was too big. I then tried a bigInt library called BigInt -> http://mattmccutchen.net/bigint/ The problem is I can't compile the code as I get many errors regarding the lib. undefined reference to `BigInteger::BigInteger(int)' <-- lot's of these. Here is my code so far: #include "string" #include "iostream" #include "bigint/NumberlikeArray.hh" #include "bigint/BigUnsigned.hh" #include "bigint/BigInteger.hh" #include "bigint/BigIntegerAlgorithms

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

妖精的绣舞 提交于 2019-11-29 03:34:57
I am unsure about how to generate a random n digit integer in Java using the BigInteger class. 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)) Carl According to the docs, there is a constructor to do what you want in java 6: BigInteger(int, java.util.Random) To that, you need only add a randomly selected 5000th digit-i.e. Use the rng

Check if BigInteger is not a perfect square

断了今生、忘了曾经 提交于 2019-11-29 03:12:42
问题 I have a BigInteger value, let's say it is 282 and is inside the variable x. I now want to write a while loop that states: while b2 isn't a perfect square: a ← a + 1 b2 ← a*a - N endwhile How would I do such a thing using BigInteger? EDIT: The purpose for this is so I can write this method. As the article states one must check if b2 is not square. 回答1: Compute the integer square root, then check that its square is your number. Here is my method of computing the square root using Heron's

MySQL: bigint Vs int

穿精又带淫゛_ 提交于 2019-11-28 21:03:12
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. 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 Types manual page. Incidentally, the use of (20) and (10) is largely irrelevant unless you're using