Arbitrary-precision integers (included): Difference between revisions

m (Automated syntax highlighting fixup (second round - minor fixes))
(27 intermediate revisions by 10 users not shown)
Line 607:
<pre>
62060698786608744707...92256259918212890625, length 183231
</pre>
 
 
=={{header|Crystal}}==
<syntaxhighlight lang="Crystal">require "big"
 
z = (BigInt.new(5) ** 4 ** 3 ** 2).to_s
zSize = z.size
puts "5**4**3**2 = #{z[0, 20]} .. #{z[zSize-20, 20]} and has #{zSize} digits"
</syntaxhighlight>
{{out}}
<pre>5**4**3**2 = 62060698786608744707 .. 92256259918212890625 and it has 183231 digits
</pre>
 
Line 1,013 ⟶ 1,025:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Arbitrary-precision_integers_%28included%29}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
In the following script, the result is converted to a string, in order to calculate its size, and its first/last digits.
In '''[https://formulae.org/?example=Arbitrary-precision_integers_%28included%29 this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Arbitrary-precision integers (included) 01.png]]
 
[[File:Fōrmulæ - Arbitrary-precision integers (included) 02.png]]
 
=={{header|GAP}}==
Line 1,184 ⟶ 1,200:
"62060698786608744707"
julia> bigstr[end-2019:end]
"89225625991821289062592256259918212890625"</syntaxhighlight>
 
=={{header|Klong}}==
Line 1,207 ⟶ 1,223:
<pre>
5^4^3^2 = 62060698786608744707...92256259918212890625 and has 183231 digits
</pre>
 
=={{header|Lambdatalk}}==
Just using Javascript's BigInt
<syntaxhighlight lang="Scheme">
 
{def N {BI.** 5 {BI.** 4 {BI.** 3 2}}}} -> N
length: {def L {W.length {N}}} -> L = 183231
20 first digits: {W.slice 0 20 {N}} -> 62060698786608744707
20 last digits: {W.slice -20 {L} {N}} -> 92256259918212890625
 
</syntaxhighlight>
 
=={{header|langur}}==
Arbitrary precision is native in langur.
<syntaxhighlight lang="langur">val .xs = string 5 ^ 4 ^ 3 ^ 2
 
writeln len(.xs), " digits"
 
if len(.xs) > 39 and s2s(.xs, 1..20) == "62060698786608744707" and
s2s(.xs, -20 .. -1) == "92256259918212890625" {
 
writeln "SUCCESS"
}
</syntaxhighlight>
 
{{out}}
<pre>183231 digits
SUCCESS
</pre>
 
Line 1,788 ⟶ 1,833:
<pre>62060698786608744707...92256259918212890625
183231</pre>
 
 
=={{header|Processing}}==
<syntaxhighlight lang="java">import java.math.BigInteger;
 
// Variable definitions
BigInteger _5, _4, powResult;
_5 = BigInteger.valueOf(5);
_4 = BigInteger.valueOf(4);
 
//calculations
powResult = _5.pow(_4.pow(9).intValueExact());
String powStr = powResult.toString();
int powLen = powStr.length();
String powStrStart = powStr.substring(0, 20);
String powStrEnd = powStr.substring(powLen - 20);
 
//output
System.out.printf("5**4**3**2 = %s...%s and has %d digits%n", powStrStart, powStrEnd, powLen);
</syntaxhighlight>
{{out}}
<pre>5**4**3**2 = 62060698786608744707...92256259918212890625 and has 183231 digits
</pre>
 
=={{header|Prolog}}==
Line 1,897 ⟶ 1,965:
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|20152022.1207}}
 
<syntaxhighlight lang="raku" line>given ~[**] 5, 4, 3, 2 {
given [**] 5, 4, 3, 2 {
say "5**4**3**2 = {.substr: 0,20}...{.substr: *-20} and has {.chars} digits";
use Test;
ok /^ 62060698786608744707 <digit>* 92256259918212890625 $/,
'5**4**3**2 has expected first and last twenty digits';
printf 'This number has %d digits', .chars;
}</syntaxhighlight>
{{out}}
<pre>ok 1 - 5**4**3**2 =has 62060698786608744707...92256259918212890625expected first and haslast 183231twenty digits</pre>
This number has 183231 digits</pre>
 
=={{header|REXX}}==
Line 2,099 ⟶ 2,172:
 
=={{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.ftfirst(0,1920), y.ftlast(-20), y.len);</syntaxhighlight>
{{out}}
<pre>
Line 2,259 ⟶ 2,332:
5**4**3**2 has 183231 digits
Value starts with 62060698786608744707, ends with 92256259918212890625
</pre>
 
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
 
MainModule: {
_start:(λ locals: a BigLong(5) ss StringStream() s ""
(textout to: ss (pow a (pow 4 (pow 3 2))))
(= s (str ss))
(with len (size s)
(lout "The number of digits is: " len)
(lout (sub s 0 20) " ... " (sub s (- len 20))))
)
}</syntaxhighlight>
{{out}}
<pre>
The number of digits is: 183231
62060698786608744707 ... 92256259918212890625
</pre>
 
Line 2,391 ⟶ 2,482:
Iterative elasped: 2412.5477 milliseconds.</pre>'''Remarks:''' Not much difference in execution times for three methods. But the exponents are relatively small. If one does need to evaluate an exponent greater than '''Int32.MaxValue''', the execution time will be measured in hours.
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import math.big
import math
 
Line 2,413 ⟶ 2,504:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./big" for BigInt
 
var p = BigInt.three.pow(BigInt.two)
Line 2,437 ⟶ 2,528:
 
pub fn main() !void {
var gpaa = try bigint.initSet(std.heap.GeneralPurposeAllocator(.{}c_allocator, 5){};
try a.pow(&a, try std.math.powi(u32, 4, try std.math.powi(u32, 3, 2)));
const allocator = &gpa.allocator;
defer _ = gpa.deinit();
 
var a = try bigint.initSet(allocator, 5);
try a.pow(a.toConst(), try std.math.powi(u32, 4, try std.math.powi(u32, 3, 2)));
defer a.deinit();
 
var as = try a.toString(allocatorstd.heap.c_allocator, 10, false.lower);
defer allocatorstd.heap.c_allocator.free(as);
 
std.debug.print("{s}...{s}\n", .{ as[0..20], as[as.len - 20 ..] });
890

edits