Arbitrary-precision integers (included): Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 24:
{{trans|Python}}
 
<syntaxhighlight lang="11l">V y = String(BigInt(5) ^ 4 ^ 3 ^ 2)
print(‘5^4^3^2 = #....#. and has #. digits’.format(y[0.<20], y[(len)-20..], y.len))</syntaxhighlight>
 
Line 33:
 
=={{header|8th}}==
<syntaxhighlight lang="forth">
200000 n#
5 4 3 2 bfloat ^ ^ ^
Line 47:
 
=={{header|ACL2}}==
<syntaxhighlight lang=Lisp"lisp">(in-package "ACL2")
 
(include-book "arithmetic-3/floor-mod/floor-mod" :dir :system)
Line 72:
=={{header|Ada}}==
{{libheader|GMP}} Using GMP, Ada bindings provided in GNATColl
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO; use Ada.Text_IO;
with GNATCOLL.GMP; use GNATCOLL.GMP;
with GNATCOLL.GMP.Integers; use GNATCOLL.GMP.Integers;
Line 93:
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN
COMMENT
Line 142:
 
=={{header|Alore}}==
<syntaxhighlight lang=Alore"alore">def Main()
var len as Int
var result as Str
Line 154:
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">num: to :string 5^4^3^2
 
print [first.n: 20 num ".." last.n: 20 num "=>" size num "digits"]</syntaxhighlight>
Line 163:
 
=={{header|bc}}==
<syntaxhighlight lang="bc">/* 5432.bc */
 
y = 5 ^ 4 ^ 3 ^ 2
Line 178:
0m24.81s real 0m24.81s user 0m0.00s system</pre>
 
{{omit from|BBC BASIC}}
 
=={{header|Bracmat}}==
At the prompt type the following one-liner:
<syntaxhighlight lang="bracmat">{?} @(5^4^3^2:?first [20 ? [-21 ?last [?length)&str$(!first "..." !last "\nlength " !length)
{!} 62060698786608744707...92256259918212890625
length 183231
Line 189 ⟶ 188:
=={{header|C}}==
=== {{libheader|GMP}} ===
<syntaxhighlight lang="c">#include <gmp.h>
#include <stdio.h>
#include <string.h>
Line 216 ⟶ 215:
==={{libheader|OpenSSL}}===
OpenSSL is about 17 times slower than GMP (on one computer), but still fast enough for this small task.
<syntaxhighlight lang="c">/* 5432.c */
 
#include <openssl/bn.h> /* BN_*() */
Line 295 ⟶ 294:
<code>System.Numerics.BigInteger</code> was added in C# 4. The exponent of <code>BigInteger.Pow()</code> is limited to a 32-bit signed integer, which is not a problem in this specific task.
{{works with|C sharp|C#|4+}}
<syntaxhighlight lang="csharp">using System;
using System.Diagnostics;
using System.Linq;
Line 328 ⟶ 327:
=== {{libheader|Boost-Multiprecision (GMP Backend)}} ===
To compile link with GMP <code>-lgmp</code>
<syntaxhighlight lang="cpp">#include <iostream>
#include <boost/multiprecision/gmp.hpp>
#include <string>
Line 357 ⟶ 356:
=={{header|Ceylon}}==
Be sure to import ceylon.whole in your module.ceylon file.
<syntaxhighlight lang="ceylon">import ceylon.whole {
wholeNumber,
two
Line 388 ⟶ 387:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(defn exp [n k] (reduce * (repeat k n)))
 
(def big (->> 2 (exp 3) (exp 4) (exp 5)))
Line 403 ⟶ 402:
<pre>output> 62060698786608744707..92256259918212890625 (183231 digits)</pre>
Redefining ''exp'' as follows speeds up the calculation of ''big'' about a hundred times:
<syntaxhighlight lang="clojure">(defn exp [n k]
(cond
(zero? (mod k 2)) (recur (* n n) (/ k 2))
Line 416 ⟶ 415:
with the reference implementation.
 
<syntaxhighlight lang="clu">start_up = proc ()
% Get bigint versions of 5, 4, 3 and 2
five: bigint := bigint$i2bi(5)
Line 454 ⟶ 453:
{{works with|GnuCOBOL}}
{{libheader|GMP}}
<syntaxhighlight lang=COBOL"cobol"> identification division.
program-id. arbitrary-precision-integers.
remarks. Uses opaque libgmp internals that are built into libcob.
Line 602 ⟶ 601:
=={{header|Common Lisp}}==
Common Lisp has arbitrary precision integers, inherited from MacLisp: "[B]ignums—arbitrary precision integer arithmetic—were added [to MacLisp] in 1970 or 1971 to meet the needs of Macsyma users." [''Evolution of Lisp'' [http://dreamsongs.com/Files/Hopl2.pdf], 2.2.2]
<syntaxhighlight lang="lisp">(let ((s (format () "~s" (expt 5 (expt 4 (expt 3 2))))))
(format t "~a...~a, length ~a" (subseq s 0 20)
(subseq s (- (length s) 20)) (length s)))</syntaxhighlight>
Line 611 ⟶ 610:
 
=={{header|D}}==
<syntaxhighlight lang="d">void main() {
import std.stdio, std.bigint, std.conv;
 
Line 623 ⟶ 622:
=={{header|Dart}}==
Originally Dart's integral type '''int''' supported arbitrary length integers, but this is no longer the case; Dart now supports integral type '''BigInt'''.
<syntaxhighlight lang="dart">import 'dart:math' show pow;
 
int fallingPowers(int base) =>
Line 642 ⟶ 641:
=={{header|dc}}==
{{trans|bc}}
<syntaxhighlight lang="dc">[5432.dc]sz
 
5 4 3 2 ^ ^ ^ sy [y = 5 ^ 4 ^ 3 ^ 2]sz
Line 658 ⟶ 657:
{{libheader| System.SysUtils}}
{{libheader| Velthuis.BigIntegers}}Thanks for Rudy Velthuis, BigIntegers Library [https://github.com/rvelthuis/DelphiBigNumbers].
<syntaxhighlight lang=Delphi"delphi">
program Arbitrary_precision_integers;
 
Line 687 ⟶ 686:
=={{header|E}}==
E implementations are required to support arbitrary-size integers transparently.
<syntaxhighlight lang="e">? def value := 5**(4**(3**2)); null
? def decimal := value.toString(10); null
? decimal(0, 20)
Line 699 ⟶ 698:
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
;; to save space and time, we do'nt stringify Ω = 5^4^3^2 ,
;; but directly extract tail and head and number of decimal digits
Line 736 ⟶ 735:
=={{header|Elixir}}==
{{trans|Erlang}}
<syntaxhighlight lang="elixir">defmodule Arbitrary do
def pow(_,0), do: 1
def pow(b,e) when e > 0, do: pow(b,e,1)
Line 765 ⟶ 764:
As of Emacs 27.1, bignums are supported via GMP. However, there is a configurable limit on the maximum bignum size. If the limit is exceeded, an overflow error is raised.
 
<syntaxhighlight lang="lisp">(let* ((integer-width (* 65536 16)) ; raise bignum limit from 65536 bits to avoid overflow error
(answer (number-to-string (expt 5 (expt 4 (expt 3 2)))))
(length (length answer)))
Line 779 ⟶ 778:
 
{{libheader|Calc}}
<syntaxhighlight lang="lisp">(let* ((answer (calc-eval "5**4**3**2"))
(length (length answer)))
(message "%s has %d digits"
Line 797 ⟶ 796:
 
Erlang supports arbitrary precision integers. However, the math:pow function returns a float. This implementation includes an implementation of pow for integers with exponent greater than 0.
<syntaxhighlight lang="erlang">
-module(arbitrary).
-compile([export_all]).
Line 830 ⟶ 829:
* bigint does not support raising to a power of a bigint
* The int type does not support the power method
<syntaxhighlight lang="fsharp">let () =
let answer = 5I **(int (4I ** (int (3I ** 2))))
let sans = answer.ToString()
Line 839 ⟶ 838:
=={{header|Factor}}==
Factor has built-in bignum support. Operations on integers overflow to bignums.
<syntaxhighlight lang="factor">USING: formatting kernel math.functions math.parser sequences ;
IN: rosettacode.bignums
 
Line 879 ⟶ 878:
Here is the solution:
 
<syntaxhighlight lang="forth">INCLUDE big.fth
 
big_digit_pointer *exp \ iteration counter
Line 950 ⟶ 949:
Here is a solution using David M. Smith's FM library, available [http://myweb.lmu.edu/dmsmith/fmlib.html here].
 
<syntaxhighlight lang="fortran">program bignum
use fmzm
implicit none
Line 971 ⟶ 970:
freebasic has it's own gmp static library.
Here, a power function operates via a string and uinteger.
<syntaxhighlight lang=FreeBASIC"freebasic">#Include once "gmp.bi"
Dim Shared As Zstring * 100000000 outtext
 
Line 1,007 ⟶ 1,006:
 
Fun Fact: The drastically faster arbitrary-precision integer operations that landed in Java 8 (for much faster multiplication, exponentiation, and toString) were taken from Frink's implementation and contributed to Java. Another fun fact is that it took employees from Java 11 years to integrate the improvements.
<syntaxhighlight lang="frink">a = 5^4^3^2
as = "$a" // Coerce to string
println["Length=" + length[as] + ", " + left[as,20] + "..." + right[as,20]]</syntaxhighlight>
Line 1,021 ⟶ 1,020:
 
=={{header|GAP}}==
<syntaxhighlight lang="gap">n:=5^(4^(3^2));;
s := String(n);;
m := Length(s);
Line 1,033 ⟶ 1,032:
Using <code>math/big</code>'s
<code>[https://golang.org/pkg/math/big/#Int.Exp Int.Exp]</code>.
<syntaxhighlight lang="go">package main
 
import (
Line 1,058 ⟶ 1,057:
 
=={{header|Golfscript}}==
<syntaxhighlight lang="golfscript">5 4 3 2??? # Calculate 5^(4^(3^2))
`.. # Convert to string and make two copies
20<p # Print the first 20 digits
Line 1,070 ⟶ 1,069:
=={{header|Groovy}}==
Solution:
<syntaxhighlight lang="groovy">def bigNumber = 5G ** (4 ** (3 ** 2))</syntaxhighlight>
Test:
<syntaxhighlight lang="groovy">def bigString = bigNumber.toString()
 
assert bigString[0..<20] == "62060698786608744707"
Line 1,083 ⟶ 1,082:
=={{header|Haskell}}==
Haskell comes with built-in support for arbitrary precision integers. The type of arbitrary precision integers is <tt>Integer</tt>.
<syntaxhighlight lang="haskell">main :: IO ()
main = do
let y = show (5 ^ 4 ^ 3 ^ 2)
Line 1,094 ⟶ 1,093:
 
=={{header|Hoon}}==
<syntaxhighlight lang=Hoon"hoon">
=+ big=(pow 5 (pow 4 (pow 3 2)))
=+ digits=(lent (skip <big> |=(a/* ?:(=(a '.') & |))))
Line 1,107 ⟶ 1,106:
 
Note: It takes far longer to convert the result to a string than it does to do the computation itself.
<syntaxhighlight lang="icon">procedure main()
x := 5^4^3^2
write("done with computation")
Line 1,125 ⟶ 1,124:
=={{header|J}}==
J has built-in support for extended precision integers. See also [[J:Essays/Extended%20Precision%20Functions]].
<syntaxhighlight lang="j"> Pow5432=: 5^4^3^2x
Pow5432=: ^/ 5 4 3 2x NB. alternate J solution
# ": Pow5432 NB. number of digits
Line 1,134 ⟶ 1,133:
=={{header|Java}}==
Java library's <tt>BigInteger</tt> class provides support for arbitrary precision integers.
<syntaxhighlight lang="java">import java.math.BigInteger;
 
class IntegerPower {
Line 1,152 ⟶ 1,151:
 
BigInt is already implemented by Chrome and Firefox, but not yet by Explorer or Safari.
<syntaxhighlight lang="javacript">>>> const y = (5n**4n**3n**2n).toString();
>>> console.log(`5**4**3**2 = ${y.slice(0,20)}...${y.slice(-20)} and has ${y.length} digits`);
5**4**3**2 = 62060698786608744707...92256259918212890625 and has 183231 digits</syntaxhighlight>
Line 1,160 ⟶ 1,159:
 
There is a BigInt.jq library for jq, but gojq, the Go implementation of jq, supports unbounded-precision integer arithmetic, so the output shown below is that produced by gojq.
<syntaxhighlight lang="jq">
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
Line 1,176 ⟶ 1,175:
Julia includes built-in support for arbitrary-precision arithmetic using the [http://gmplib.org/ GMP] (integer) and [http://www.mpfr.org/ GNU MPFR] (floating-point) libraries, wrapped by the built-in <code>BigInt</code> and <code>BigFloat</code> types, respectively.
 
<syntaxhighlight lang="julia">julia> @elapsed bigstr = string(BigInt(5)^4^3^2)
0.017507363
Line 1,196 ⟶ 1,195:
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import java.math.BigInteger
 
fun main(args: Array<String>) {
Line 1,212 ⟶ 1,211:
=={{header|Lasso}}==
Interestingly, we have to define our own method for integer powers.
<syntaxhighlight lang="lasso">define integer->pow(factor::integer) => {
#factor <= 0
? return 0
Line 1,238 ⟶ 1,237:
 
Note the brackets are needed to enforce the desired order of exponentiating.
<syntaxhighlight lang="lb">a$ = str$( 5^(4^(3^2)))
print len( a$)
print left$( a$, 20); "......"; right$( a$, 20)</syntaxhighlight>
Line 1,247 ⟶ 1,246:
=={{header|Lua}}==
Pure/native off-the-shelf Lua does not include support for arbitrary-precision arithmetic. However, there are a number of optional libraries that do, including several from an author of the language itself - one of which this example will use. (citing the "..''may'' be used instead" allowance)
<syntaxhighlight lang="lua">bc = require("bc")
-- since 5$=5^4$, and IEEE754 can handle 4$, this would be sufficient:
-- n = bc.pow(bc.new(5), bc.new(4^3^2))
Line 1,259 ⟶ 1,258:
=={{header|Maple}}==
Maple supports large integer arithmetic natively.
<syntaxhighlight lang=Maple"maple">
> n := 5^(4^(3^2)):
> length( n ); # number of digits
Line 1,269 ⟶ 1,268:
</syntaxhighlight>
In the Maple graphical user interface it is also possible to set things up so that only (say) the first and last 20 digits of a large integer are displayed explicitly. This is done as follows.
<syntaxhighlight lang=Maple"maple">
> interface( elisiondigitsbefore = 20, elisiondigitsafter = 20 ):
> 5^(4^(3^2)):
Line 1,278 ⟶ 1,277:
Mathematica can handle arbitrary precision integers on almost any size without further declarations.
To view only the first and last twenty digits:
<syntaxhighlight lang=Mathematica"mathematica">s:=ToString[5^4^3^2];
Print[StringTake[s,20]<>"..."<>StringTake[s,-20]<>" ("<>ToString@StringLength@s<>" digits)"];</syntaxhighlight>
{{out}}
Line 1,285 ⟶ 1,284:
=={{header|MATLAB}}==
Using the [http://www.mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic Variable Precision Integer] library this task is accomplished thusly:
<syntaxhighlight lang=MATLAB"matlab">>> answer = vpi(5)^(vpi(4)^(vpi(3)^vpi(2)));
>> numDigits = order(answer) + 1
 
Line 1,300 ⟶ 1,299:
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">block([s, n], s: string(5^4^3^2), n: slength(s), print(substring(s, 1, 21), "...", substring(s, n - 19)), n);
/* 62060698786608744707...92256259918212890625
183231 */</syntaxhighlight>
Line 1,306 ⟶ 1,305:
=={{header|Nanoquery}}==
Integer values are arbitrary-precision by default in Nanoquery.
<syntaxhighlight lang=Nanoquery"nanoquery">value = str(5^(4^(3^2)))
 
first20 = value.substring(0,20)
Line 1,329 ⟶ 1,328:
=={{header|Nemerle}}==
{{trans|C#}}
<syntaxhighlight lang=Nemerle"nemerle">using System.Console;
using System.Numerics;
using System.Numerics.BigInteger;
Line 1,356 ⟶ 1,355:
=
=={{header|NewLisp}}==
<syntaxhighlight lang=NewLisp"newlisp">
;;; No built-in big integer exponentiation
(define (exp-big x n)
Line 1,387 ⟶ 1,386:
=={{header|NetRexx}}==
=== Using Java's BigInteger Class ===
<syntaxhighlight lang=NetRexx"netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols
Line 1,432 ⟶ 1,431:
</pre>
=== Using Java's BigDecimal Class ===
<syntaxhighlight lang=NetRexx"netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols
Line 1,481 ⟶ 1,480:
==== Note ====
{{trans|REXX}}
<syntaxhighlight lang=NetRexx"netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols
Line 1,525 ⟶ 1,524:
 
{{libheader|bigints}}
<syntaxhighlight lang="nim">import bigints
 
var x = 5.pow 4.pow 3.pow 2
Line 1,540 ⟶ 1,539:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">open Num
open Str
open String
Line 1,553 ⟶ 1,552:
 
A more readable program can be obtained using [http://forge.ocamlcore.org/projects/pa-do/ Delimited Overloading]:
<syntaxhighlight lang="ocaml">let () =
let answer = Num.(5**4**3**2) in
let s = Num.(to_string answer) in
Line 1,567 ⟶ 1,566:
Oforth handles arbitrary precision integers :
 
<syntaxhighlight lang=Oforth"oforth">import: mapping
 
5 4 3 2 pow pow pow >string dup left( 20 ) . dup right( 20 ) . size . </syntaxhighlight>
Line 1,577 ⟶ 1,576:
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
(define x (expt 5 (expt 4 (expt 3 2))))
(print
Line 1,594 ⟶ 1,593:
{{trans|REXX}}
 
<syntaxhighlight lang=ooRexx"oorexx">
--REXX program to show arbitrary precision integers.
numeric digits 200000
Line 1,627 ⟶ 1,626:
 
=={{header|Oz}}==
<syntaxhighlight lang="oz">declare
Pow5432 = {Pow 5 {Pow 4 {Pow 3 2}}}
S = {Int.toString Pow5432}
Line 1,642 ⟶ 1,641:
=={{header|PARI/GP}}==
PARI/GP natively supports integers of arbitrary size, so one could just use <code>N=5^4^3^2</code>. But this would be foolish (using a lot of unneeded memory) if the task is to get just the number of, and the first and last twenty digits. The number of and the leading digits are given as 1 + the integer part, resp. 10^(fractional part + offset), of the logarithm to base 10 (not as log(N) with N=A^B, but as B*log(A); one needs at least 20 correct decimals of the log, on 64 bit machines the default precision is 39 digits, but on 32 bit architecture one should set <code>default(realprecision,30)</code> to be on the safe side). To get the trailing digits, one would use modular exponentiation which is also built-in and very efficient even for extremely huge exponents:
<syntaxhighlight lang="parigp">num_first_last_digits(a=5,b=4^3^2,n=20)={ my(L = b*log(a)/log(10), m=Mod(a,10^n)^b);
[L\1+1, 10^frac(L)\10^(1-n), lift(m)] \\ where x\y = floor(x/y) but more efficient
}
Line 1,651 ⟶ 1,650:
 
An alternate but much slower method for counting decimal digits is <code>#Str(n)</code>. Note that <code>sizedigit</code> is not exact&mdash;in particular, it may be off by one (thus the function below).
<syntaxhighlight lang="parigp">digits(x)={
my(s=sizedigit(x)-1);
if(x<10^s,s,s+1)
Line 1,666 ⟶ 1,665:
{{libheader|GMP}}
FreePascal comes with a header unit for gmp. Starting from the C program, this is a Pascal version:
<syntaxhighlight lang="pascal">program GMP_Demo;
 
uses
Line 1,701 ⟶ 1,700:
=={{header|Perl}}==
Perl's <tt>Math::BigInt</tt> core module handles big integers:
<syntaxhighlight lang="perl">use Math::BigInt;
my $x = Math::BigInt->new('5') ** Math::BigInt->new('4') ** Math::BigInt->new('3') ** Math::BigInt->new('2');
my $y = "$x";
printf("5**4**3**2 = %s...%s and has %i digits\n", substr($y,0,20), substr($y,-20), length($y));</syntaxhighlight>
You can enable "transparent" big integer support by enabling the <tt>bigint</tt> pragma:
<syntaxhighlight lang="perl">use bigint;
my $x = 5**4**3**2;
my $y = "$x";
Line 1,713 ⟶ 1,712:
<tt>Math::BigInt</tt> is very slow. Perl 5.10 was about 120 times slower than Ruby 1.9.2 (on one computer); Perl used more than one minute, but Ruby used less than one second.
{{out}}
<syntaxhighlight lang="perl">$ time perl transparent-bigint.pl
5**4**3**2 = 62060698786608744707...92256259918212890625 and has 183231 digits
1m4.28s real 1m4.30s user 0m0.00s system</syntaxhighlight>
Line 1,720 ⟶ 1,719:
{{libheader|Phix/basics}}
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,739 ⟶ 1,738:
 
The first is the BC library.[http://us3.php.net/manual/en/book.bc.php] It represents the integers as strings, so may not be very efficient. The advantage is that it is more likely to be included with PHP.
<syntaxhighlight lang="php"><?php
$y = bcpow('5', bcpow('4', bcpow('3', '2')));
printf("5**4**3**2 = %s...%s and has %d digits\n", substr($y,0,20), substr($y,-20), strlen($y));
Line 1,751 ⟶ 1,750:
 
=={{header|Picat}}==
<syntaxhighlight lang=Picat"picat">main =>
X = to_string(5**4**3**2),
Y = len(X),
Line 1,760 ⟶ 1,759:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(let L (chop (** 5 (** 4 (** 3 2))))
(prinl (head 20 L) "..." (tail 20 L))
(length L) )</syntaxhighlight>
Line 1,768 ⟶ 1,767:
 
=={{header|Pike}}==
<syntaxhighlight lang=Pike"pike">> string res = (string)pow(5,pow(4,pow(3,2)));
> res[..19] == "62060698786608744707";
Result: 1
Line 1,777 ⟶ 1,776:
 
=={{header|PowerShell}}==
<syntaxhighlight lang=PowerShell"powershell"># Perform calculation
$BigNumber = [BigInt]::Pow( 5, [BigInt]::Pow( 4, [BigInt]::Pow( 3, 2 ) ) )
Line 1,794 ⟶ 1,793:
{{works with|SWI-Prolog|6.6}}
 
<syntaxhighlight lang="prolog">
task(Length) :-
N is 5^4^3^2,
Line 1,807 ⟶ 1,806:
Query like so:
 
<syntaxhighlight lang="prolog">
?- task(N).
N = 183231 ;
Line 1,817 ⟶ 1,816:
 
Using [http://www.purebasic.fr/english/viewtopic.php?p=309763#p309763 Decimal.pbi], e.g. the same included library as in [[Long multiplication#PureBasic]], this task is solved as below.
<syntaxhighlight lang=PureBasic"purebasic">IncludeFile "Decimal.pbi"
 
;- Declare the variables that will be used
Line 1,842 ⟶ 1,841:
=={{header|Python}}==
Python comes with built-in support for arbitrary precision integers. The type of arbitrary precision integers is <tt>[http://docs.python.org/library/stdtypes.html#typesnumeric long]</tt> in Python 2.x (overflowing operations on <tt>int</tt>'s are automatically converted into <tt>long</tt>'s), and <tt>[http://docs.python.org/3.1/library/stdtypes.html#typesnumeric int]</tt> in Python 3.x.
<syntaxhighlight lang="python">>>> y = str( 5**4**3**2 )
>>> print ("5**4**3**2 = %s...%s and has %i digits" % (y[:20], y[-20:], len(y)))
5**4**3**2 = 62060698786608744707...92256259918212890625 and has 183231 digits</syntaxhighlight>
Line 1,871 ⟶ 1,870:
=={{header|R}}==
R does not come with built-in support for arbitrary precision integers, but it can be implemented with the GMP library.
<syntaxhighlight lang="rsplus">library(gmp)
large <- pow.bigz(5, pow.bigz(4, pow.bigz(3, 2)))
largestr <- as.character(large)
Line 1,883 ⟶ 1,882:
 
=={{header|Racket}}==
<syntaxhighlight lang=Racket"racket">#lang racket
 
(define answer (number->string (foldr expt 1 '(5 4 3 2))))
Line 1,900 ⟶ 1,899:
{{works with|Rakudo|2015.12}}
 
<syntaxhighlight lang=perl6"raku" line>given ~[**] 5, 4, 3, 2 {
say "5**4**3**2 = {.substr: 0,20}...{.substr: *-20} and has {.chars} digits";
}</syntaxhighlight>
Line 1,925 ⟶ 1,924:
:::* &nbsp; ROO
:::* &nbsp; ooRexx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (tested by Walter Pachl)
<syntaxhighlight lang="rexx">/*REXX program calculates and demonstrates arbitrary precision numbers (using powers). */
numeric digits 200000 /*two hundred thousand decimal digits. */
 
Line 1,950 ⟶ 1,949:
 
===automatic setting of decimal digits===
<syntaxhighlight lang="rexx">/*REXX program calculates and demonstrates arbitrary precision numbers (using powers). */
numeric digits 5 /*just use enough digits for 1st time. */
 
Line 1,978 ⟶ 1,977:
Ruby comes with built-in support for arbitrary precision integers.
 
<syntaxhighlight lang="ruby">y = ( 5**4**3**2 ).to_s
puts "5**4**3**2 = #{y[0..19]}...#{y[-20..-1]} and has #{y.length} digits"
</syntaxhighlight>
Line 1,987 ⟶ 1,986:
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">x$ = str$( 5^(4^(3^2)))
print "Length:";len( x$)
print left$( x$, 20); "......"; right$( x$, 20)</syntaxhighlight>
Line 1,997 ⟶ 1,996:
This is accomplished via the `num` crate. This used to be part of the standard library, but was relegated to an external crate when Rust hit 1.0. It is still owned and maintained by members of the Rust core team and is the de-facto library for numerical generics and arbitrary precision arithmetic.
 
<syntaxhighlight lang="rust">extern crate num;
use num::bigint::BigUint;
use num::FromPrimitive;
Line 2,020 ⟶ 2,019:
 
=={{header|Sather}}==
<syntaxhighlight lang="sather">class MAIN is
main is
r:INTI;
Line 2,048 ⟶ 2,047:
=={{header|Scala}}==
Scala does not come with support for arbitrary precision integers powered to arbitrary precision integers, except if performed on a module. It can use arbitrary precision integers in other ways, including powering them to 32-bits integers.
<syntaxhighlight lang="scala">scala> BigInt(5) modPow (BigInt(4) pow (BigInt(3) pow 2).toInt, BigInt(10) pow 20)
res21: scala.math.BigInt = 92256259918212890625
 
Line 2,072 ⟶ 2,071:
=={{header|Scheme}}==
[http://people.csail.mit.edu/jaffer/r4rs_8.html#SEC52 R<sup>4</sup>RS] and [http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2.3 R<sup>5</sup>RS] encourage, and [http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-6.html#node_sec_3.4 R<sup>6</sup>RS] requires, that exact integers be of arbitrary precision.
<syntaxhighlight lang="scheme">(define x (expt 5 (expt 4 (expt 3 2))))
(define y (number->string x))
(define l (string-length y))
Line 2,081 ⟶ 2,080:
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
 
Line 2,100 ⟶ 2,099:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var x = 5**(4**(3**2));
var y = x.to_s;
printf("5**4**3**2 = %s...%s and has %i digits\n", y.ft(0,19), y.ft(-20), y.len);</syntaxhighlight>
Line 2,111 ⟶ 2,110:
{{incomplete|SIMPOL|Number of digits in result not given.}}
SIMPOL supports arbitrary precision integers powered to arbitrary precision integers. This is the only integer data type in SIMPOL. SIMPOL supports conversion from its integer data type to other formats when calling external library functions.
<syntaxhighlight lang="simpol">constant FIRST20 "62060698786608744707"
constant LAST20 "92256259918212890625"
 
Line 2,137 ⟶ 2,136:
 
A very simple approach:
<syntaxhighlight lang="smalltalk">|num|
num := (5 raisedTo: (4 raisedTo: (3 raisedTo: 2))) asString.
Transcript
Line 2,147 ⟶ 2,146:
digits: 183231</pre>
And a more advanced one:
<syntaxhighlight lang="smalltalk">|num numstr|
num := (2 to: 5) fold: [:exp :base| base raisedTo: exp].
numstr := num asString.
Line 2,160 ⟶ 2,159:
 
=={{header|SPL}}==
<syntaxhighlight lang="spl">t = #.str(5^(4^(3^2)))
n = #.size(t)
#.output(n," digits")
Line 2,171 ⟶ 2,170:
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">let
val answer = IntInf.pow (5, IntInf.toInt (IntInf.pow (4, IntInf.toInt (IntInf.pow (3, 2)))))
val s = IntInf.toString answer
Line 2,184 ⟶ 2,183:
=== mLite ===
mLite does not have a logarithm function so one was constructed (see fun log10)
<syntaxhighlight lang="sml">
fun
ntol (0, x) = if len x < 1 then [0] else x
Line 2,249 ⟶ 2,248:
Tcl supports arbitrary precision integers (and an exponentiation operator) from 8.5 onwards.
{{works with|Tcl|8.5}}
<syntaxhighlight lang="tcl">set bigValue [expr {5**4**3**2}]
puts "5**4**3**2 has [string length $bigValue] digits"
if {[string match "62060698786608744707*92256259918212890625" $bigValue]} {
Line 2,263 ⟶ 2,262:
 
=={{header|TXR}}==
<syntaxhighlight lang="txr">@(bind (f20 l20 ndig)
@(let* ((str (tostring (expt 5 4 3 2)))
(len (length str)))
Line 2,281 ⟶ 2,280:
 
===Usage===
<syntaxhighlight lang="ursa">import "unbounded_int"
decl unbounded_int x
x.set ((x.valueof 5).pow ((x.valueof 4).pow ((x.valueof 3).pow 2)))
Line 2,318 ⟶ 2,317:
 
There is no distinction between ordinary and arbitrary precision integers, but the binary converted decimal representation used here is more efficient than the usual binary representation in calculations that would otherwise be dominated by the conversion to decimal output.
<syntaxhighlight lang=Ursala"ursala">#import std
#import nat
#import bcd
Line 2,333 ⟶ 2,332:
{{libheader|System.Numerics}}
Addressing the issue of the '''BigInteger.Pow()''' function having the exponent value limited to '''Int32.MaxValue''' (2147483647), here are a couple of alternative implementations using a '''BigInteger''' for the exponent.
<syntaxhighlight lang="vbnet">Imports System.Console
Imports BI = System.Numerics.BigInteger
 
Line 2,393 ⟶ 2,392:
 
=={{header|Vlang}}==
<syntaxhighlight lang="vlang">import math.big
import math
 
Line 2,414 ⟶ 2,413:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/big" for BigInt
 
Line 2,434 ⟶ 2,433:
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
const bigint = std.math.big.int.Managed;
 
Line 2,460 ⟶ 2,459:
=={{header|zkl}}==
Using the GNU big num library:
<syntaxhighlight lang="zkl">var BN=Import("zklBigNum");
n:=BN(5).pow(BN(4).pow(BN(3).pow(2)));
s:=n.toString();
Line 2,471 ⟶ 2,470:
</pre>
 
{{omit from|AWK|Only has double-precision floating-point numbers.}}
{{omit from|AutoHotkey}}
{{omit from|AWK|Only has double-precision floating-point numbers.}}
{{omit from|Batch File}}
{{omit from|BBC BASIC}}
{{omit from|Brainf***}}
{{omit from|PostScript}}
10,327

edits