Arbitrary-precision integers (included): Difference between revisions

Content deleted Content added
Amarty (talk | contribs)
adding lambdatalk
Langurmonkey (talk | contribs)
 
(21 intermediate revisions by 7 users not shown)
Line 1,022:
println["Length=" + length[as] + ", " + left[as,20] + "..." + right[as,20]]</syntaxhighlight>
This prints <CODE>Length=183231, 62060698786608744707...92256259918212890625</CODE>
 
=={{header|FutureBasic}}==
Translated from c
 
Thanks, Ken
 
<syntaxhighlight lang="futurebasic">
/*
 
Uses GMP for Multiple Precision Arithmetic
Install GMP using terminal and Homebrew command, "brew install gmp"
before running this app in FutureBasic.
 
Homebrew available here, https://brew.sh
 
*/
 
 
include "NSLog.incl"
 
void local fn GMPoutput
CFStringRef sourcePath = fn StringByAppendingPathComponent( @"/tmp/", @"temp.m" )
CFStringRef executablePath = fn StringByAppendingPathComponent( @"/tmp/", @"temp" )
CFStringRef gmpStr = @"#import <Foundation/Foundation.h>\n#import <gmp.h>\n¬
int main(int argc, const char * argv[]) {\n¬
@autoreleasepool {\n¬
mpz_t a;\n¬
mpz_init_set_ui(a, 5);\n¬
mpz_pow_ui(a, a, 1 << 18);\n¬
size_t len = mpz_sizeinbase(a, 10);\n¬
printf(\"GMP says size is: %zu\\n\", len);\n¬
char *s = mpz_get_str(0, 10, a);\n¬
size_t trueLen = strlen(s);\n¬
printf(\" Actual size is: %zu\\n\", trueLen);\n¬
printf(\"First & Last 20 digits: %.20s…%s\\n\", s, s + trueLen - 20);\n¬
}\n¬
return 0;\n¬
}"
fn StringWriteToURL( gmpStr, fn URLFileURLWithPath( sourcePath ), YES, NSUTF8StringEncoding, NULL )
TaskRef task = fn TaskInit
TaskSetExecutableURL( task, fn URLFileURLWithPath( @"usr/bin/clang" ) )
CFArrayRef arguments = @[@"-o", executablePath, sourcePath, @"-lgmp", @"-fobjc-arc"]
TaskSetArguments( task, arguments )
PipeRef pipe = fn PipeInit
TaskSetStandardInput( task, pipe )
fn TaskLaunch( task, NULL )
TaskWaitUntilExit( task )
if ( fn TaskTerminationStatus( task ) == 0 )
TaskRef executionTask = fn TaskInit
TaskSetExecutableURL( executionTask, fn URLFileURLWithPath( executablePath ) )
PipeRef executionPipe = fn PipeInit
TaskSetStandardOutput( executionTask, executionPipe )
FileHandleRef executionFileHandle = fn PipeFileHandleForReading( executionPipe )
fn TaskLaunch( executionTask, NULL )
TaskWaitUntilExit( executionTask )
CFDataRef outputData = fn FileHandleReadDataToEndOfFile( executionFileHandle, NULL )
CFStringRef outputStr = fn StringWithData( outputData, NSUTF8StringEncoding )
NSLog( @"%@", outputStr )
else
alert 1,, @"GMP required but not installed"
end if
fn FileManagerRemoveItemAtURL( fn URLFileURLWithPath( sourcePath ) )
fn FileManagerRemoveItemAtURL( fn URLFileURLWithPath( executablePath ) )
end fn
 
fn GMPoutput
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
GMP says size is: 183231
Actual size is: 183231
First & Last 20 digits: 62060698786608744707…92256259918212890625
</pre>
 
=={{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'''
 
In the following script, the result is converted to a string, in order to calculate its size, and its first/last digits.
 
[[File:Fōrmulæ - Arbitrary-precision integers (included) 01.png]]
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.
 
[[File:Fōrmulæ - Arbitrary-precision integers (included) 02.png]]
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.
 
=={{header|GAP}}==
Line 1,196 ⟶ 1,278:
"62060698786608744707"
julia> bigstr[end-2019:end]
"89225625991821289062592256259918212890625"</syntaxhighlight>
 
=={{header|Klong}}==
Line 1,233 ⟶ 1,315:
 
=={{header|langur}}==
Arbitrary precision is native in langur.
<syntaxhighlight lang="langur">val .x = 5 ^ 4 ^ 3 ^ 2
<syntaxhighlight lang="langur">val xs = string(5 ^ 4 ^ 3 ^ 2)
val .xs = toString .x
val .len = len(.xs)
 
writeln .len(xs), " digits"
 
valif .first20,len(xs) .last20> =39 and s2s(.xs, 1..20), s2s(.xs,== .len-19"62060698786608744707" to .len)and
s2s(xs, -20 .. -1) == "92256259918212890625" {
 
if .first20 == "62060698786608744707" and .last20 == "92256259918212890625" {
writeln "SUCCESS"
} else {
writeln "FAIL; first 20 and/or last 20 digits not matching expected digits"
writeln "first 20: ", .first20
writeln "last 20: ", .last20
}
</syntaxhighlight>
Line 1,968 ⟶ 2,045:
{{works with|Rakudo|2022.07}}
 
<syntaxhighlight lang="raku" line>use Test;
given [**] 5, 4, 3, 2 {
ok ~([**] 5, 4, 3, 2) ~~ /^ '62060698786608744707' <digit>* '92256259918212890625' $/,
use Test;
'5**4**3**2 has expected first and last twenty digits';</syntaxhighlight>
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 expected first and last twenty digits</pre>
This number has 183231 digits</pre>
 
=={{header|REXX}}==
Line 2,168 ⟶ 2,250:
 
=={{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,500 ⟶ 2,582:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./big" for BigInt
 
var p = BigInt.three.pow(BigInt.two)