Try the raw Biginteger and Bigrational libraries, short description with some examples below.
Try the raw Biginteger and Bigrational libraries, short description with some examples below.
Most of the functions, especially the basic functions are implemented as prototypes to the Bigintgers and Bigrationals respectively, so a simple addition of one and one to get two as the expected result (we assume decimal output here) needs the following, ready to be put into the textarea above:
var one = new Bigint(1); var result = one.add(one); result.toString();
You can give the Bigint
constructor small numbers (up to 2^{26}) directly,
larger numbers (up to 2^{53}) need to be given indirectly:
var num = 134217728; var onehundredthirtyfourmilliontwohundredseventeenthousandsevenhundredtwentyeight = num.toBigint(); onehundredthirtyfourmilliontwohundredseventeenthousandsevenhundredtwentyeight.toString();
Even larger numbers need to be given as strings:
var largenumberasastring = "82756729834529384756298356298375629834756298345623894756"; var largenumber = largenumberasastring.toBigint(); largenumber.toString();
The rational number follow the same style with the exception of the second format. You can give small number directly:
var smallfraction = new Bigrational(234,432); smallfraction.toString();
Caution: the constructor does no reducing! You have to do it by hand, either in-place
with bigrat.normalize()
or on a copy with bigrat.reduce()
var smallfraction = new Bigrational(234,432); smallfraction.normalize(); smallfraction.toString();
Larger numbers as strings:
var largefractionasastring = "984756398475t3987539/5847698w347349857649569487"; largefraction = largefractionasastring.toBigrational(); largefraction.normalize(); largefraction.toString();
Larger numbers as two Bigintegers:
var numerator = "984756398475t39875395847698w347349857649569487"; var denominator = "8736452873452873465287346528365823756873465t823746"; numerator = numerator.toBigint(); denominator = denominator.toBigint(); largefraction = new Bigrational(numerator, denominator); largefraction.normalize(); largefraction.toString();
Bigrational as two Bigints:
var numerator = "-984756398475t39875395847698w347349857649569487"; var denominator = "8736452873452873465287346528365823756873465t823746"; numerator = numerator.toBigint(); denominator = denominator.toBigint(); largefraction = new Bigrational(numerator, denominator); largefraction.normalize(); numerator2 = largefraction.num; denominator2 = largefraction.den; sign = largefraction.sign; alert(numerator2.toString() + "\r\n" + denominator2.toString() + "\r\n" + sign);
Chaining is possible but does not work between the Bigints and Bigrationals automatically;
these are the raw libraries. You need to do it manually as described above.
Otherwise:
var a = new Bigint(123); var b = new Bigint(243); /* c = a * a + b * b */ var c = a.mul(a).add(b.mul(b)); /* c = (a * a + b) * b */ var d = a.mul(a).add(b).mul(b); "c = " + c.toString() + "\r\nd = " + d.toString();
Finaly, compute the Riemann Zeta function at s = 3 (Apéry's constant) to 100
decimal digits precision.
Please be patient, this may last a while. Most of the time is spent in the
function D()
b.t.w. which is to the highest extend unoptimised.
Borwein, Peter. An efficient algorithm for the Riemann zeta function.
Canadian Mathematical Society Conference Proceedings. Vol. 27. 2000
function D(n) { var ret, sum, i; var ret = new Array(n + 1); var sum = new Bigrational(0, 1); var N = new Bigint(n); var num, den; for (i = 0; i <= n; i++) { num = factorial(n + i - 1).lShift(2 * i); den = factorial(n - i).mul(factorial(2 * i)); num = new Bigrational(num, den); num.normalize(); sum = sum.add(num) ret[i] = N.mul(sum.num).div(sum.den); } return ret; } function zeta(s, n) { var Ds, sum, k; Ds = D(n); sum = new Bigrational(0, 1); var sign = 1; var t1, t2, t3, t4, sm1; for (k = 0; k < n; k++) { t1 = Ds[k].sub(Ds[n]); t1.sign = (sign > 0) ? MP_ZPOS : MP_NEG; sign = -sign; // here is the reason why large s do need more time t2 = (k + 1).bigpow(s); t3 = new Bigrational(t1, t2); t3.normalize(); sum = sum.add(t3); } sm1 = 1 - s; if (sm1 < 0) { t3 = new Bigint(1); t3.lShiftInplace(-sm1); t4 = new Bigint(1) t3 = new Bigrational(t4, t3); t2 = (new Bigrational(1, 1)).sub(t3) } else { //not yet implemented although Bernoulli numbers are return (new Bigrational()).setNaN(); } t3 = new Bigrational(Ds[n], new Bigint(1)) t1 = t3.mul(t2) t1.inverse(); sum = sum.mul(t1); return sum; } var ret = zeta(3,Math.ceil(100*1.3)); var multiplier = (10).bigpow(100); var t = ret.num.mul(multiplier); var rs = t.div(ret.den).toString(); rs.slice(0,1)+"."+rs.slice(1,rs.length)
Yes, the last two lines do not really fit, but if we had a working implementation of an arbitray precision floating point number, we wouldn't have to do it so complicated ;-)
Number
.oldbignumber = null;
Number
Number
Number
Number
Number
Bigint
function toString
and hence the Bigrational
one, too, accepts more bases (2-62) than the native one of the Number
object (2-36).
fill(amount)
amount
. Allows for skipping of the
automated adapting of the sieve-size if amount
is chosen high enough.
undefined
in case of an error
grow(amount)
grow(amount)
amount
. It just builds a new prime sieve with
a different limit (if higher then the already existing limit) for now. Mostly
used internally.
undefined
in case of an error
fill(amount)
isSmallPrime(number)
number
is a prime. Automatically builds a sieve if number
is larger then the existing sieve.
true
or false
if number
is a prime or not, respectively or undefined
in case of an error.
Primesieve.raiseLimit(limit)
nextPrime(number)
number
. Automatically builds a
sieve if number
is larger then the existing sieve.
Number
or undefined
in case of an error
precPrime(number)
number
. Does not automatically build a sieve if number
is larger then the existing sieve. Should it?
Number
or undefined
in case of an error
primePiApprox(number)
number
Number
or undefined
in case of an error
primePi(number)
number
. Automatically
builds a sieve if number
is larger then the existing sieve.
Number
or undefined
in case of an error
primeRange(low,high)
low
and high
. It will detect if low
and high
are reversed and reruns itself with the arguments reversed.
Automatically builds a sieve if number
is larger then the existing sieve.
Array
or undefined
in case of an error
primes(number)
number
. Automatically builds a sieve if
number
is larger then the existing sieve.
Array
or undefined
in case of an error
raiseLimit(number)
primesieve
has a limit for the maximum size for the automatic
sieve building. It is pre-defined at 0x800000
which is one megabyte. This
function can raise it to the manager.
undefined
in case of an error
sieve()
sterror()
String
error
error
strerror()
c = a&b
this
Bigint
for later use this
(like i--;
) in-place divrem
when some of the arguments are negativedivrem
but the second argument can be a small integer this
this
this.used
. Returns the new length of the digit array Number
highBit() + 1
) base
this
(like i++;
) in-place Number
Number
c = (1<<n)-1
Number
c = ~a
a = ~a
c = a|b
this
to the power of a small integer or a Bigintbits
length in bits and optional small integer as the seed to the underlying PRNG (Bob Jenkins' small PRNG (with the extra round))this
c = a^b