biginteger

Arbitrary-Precision Math in PHP

一笑奈何 提交于 2019-11-30 17:48:55
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 it takes normal integers in string form (e.g. "5.34"), but I've seen implementations where BCMath

What data-structure should I use to create my own “BigInteger” class?

可紊 提交于 2019-11-30 17:47:39
As an optional assignment, I'm thinking about writing my own implementation of the BigInteger class, where I will provide my own methods for addition, subtraction, multiplication, etc. This will be for arbitrarily long integer numbers, even hundreds of digits long. While doing the math on these numbers, digit by digit isn't hard, what do you think the best datastructure would be to represent my "BigInteger"? At first I was considering using an Array but then I was thinking I could still potentially overflow (run out of array slots) after a large add or multiplication. Would this be a good case

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

﹥>﹥吖頭↗ 提交于 2019-11-30 17:40:24
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? 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); amicngh If you have binary String you can convert it to BigInteger like this: String binaryString =

How can I create a random BigDecimal in Java?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 17:13:10
问题 This question: How to generate a random BigInteger describes a way to achieve the same semantics as Random.nextInt(int n) for BigIntegers. I would like to do the same for BigDecimal and Random.nextDouble(). One answer in the above question suggests creating a random BigInteger and then creating a BigDouble from it with a random scale. A very quick experiment shows this to be a very bad idea :) My intuition is that using this method would require the integer to be scaled by something like n

What complexity are operations on Java 7's BigInteger?

心已入冬 提交于 2019-11-30 14:11:15
问题 What complexity are the methods multiply , divide and pow in BigInteger currently? There is no mention of the computational complexity in the documentation (nor anywhere else). 回答1: If you look at the code for BigInteger (provided with JDK), it appears to me that multiply(..) has O(n^2) (actually the method is multiplyToLen(..) ). The code for the other methods is a bit more complex, but you can see yourself. Note: this is for Java 6. I assume it won't differ in Java 7. 回答2: There is a new

What algorithm should I use for high-performance large integer division?

谁说胖子不能爱 提交于 2019-11-30 13:00:26
I am encoding large integers into an array of size_t . I already have the other operations working (add, subtract, multiply); as well as division by a single digit. But I would like match the time complexity of my multiplication algorithms if possible (currently Toom-Cook). I gather there are linear time algorithms for taking various notions of multiplicative inverse of my dividend. This means I could theoretically achieve division in the same time complexity as my multiplication, because the linear-time operation is "insignificant" by comparison anyway. My question is, how do I actually do

Number of digits of GMP integer

牧云@^-^@ 提交于 2019-11-30 11:42:10
Is there an easy way to determine the number of digits a GMP integer has? I know you can determine it through a log, but I was wondering if there was something built into the library that I'm missing. The only thing I've found in the manual is: _mp_size The number of limbs, or the negative of that when representing a negative integer. Zero is represented by _mp_size set to zero, in which case the _mp_d data is unused. But I'm under the impression that is quite different than what I'm looking for. i.e 124839 = 6 digits. You can use size_t mpz_sizeinbase (mpz_t op, int base) to get the number of

How do i compare values of BigInteger to be used as a condition in a loop?

白昼怎懂夜的黑 提交于 2019-11-30 11:21:24
I am trying to compare if the value of one BigInteger(base) is > the value of another BigInteger(prime) and if the value of 'a' is not equal to one. If value of a is not 1, it should break out of the loop. How should i compare them? Random ran = new Random(); BigInteger prime = new BigInteger(16,ran); BigInteger base,a,one; one = new BigInteger("1"); for (int i = 0; i < 65535; i++){ while (base>prime){ base = new BigInteger(16,ran); } a = base.modPow(prime.subtract(one),prime); System.out.println("a: "+a); if (a != one){ break; } } You can compare them using BigInteger.compareTo(BigInteger) .

How to convert BigInteger to String in java

六眼飞鱼酱① 提交于 2019-11-30 11:21:17
问题 I converted a String to BigInteger as follows: Scanner sc=new Scanner(System.in); System.out.println("enter the message"); String msg=sc.next(); byte[] bytemsg=msg.getBytes(); BigInteger m=new BigInteger(bytemsg); Now I want my string back. I'm using m.toString() but that's giving me the desired result. Why? Where is the bug and what can I do about it? 回答1: You want to use BigInteger.toByteArray() String msg = "Hello there!"; BigInteger bi = new BigInteger(msg.getBytes()); System.out.println

What complexity are operations on Java 7's BigInteger?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 09:38:59
What complexity are the methods multiply , divide and pow in BigInteger currently? There is no mention of the computational complexity in the documentation (nor anywhere else). If you look at the code for BigInteger (provided with JDK), it appears to me that multiply(..) has O(n^2) (actually the method is multiplyToLen(..) ). The code for the other methods is a bit more complex, but you can see yourself. Note: this is for Java 6. I assume it won't differ in Java 7. There is a new "better" BigInteger class that is not being used by the sun jdk for conservateism and lack of useful regression