Arbitrary-precision integers (included): Difference between revisions
Content deleted Content added
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}}
'''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]]
[[File:Fōrmulæ - Arbitrary-precision integers (included) 02.png]]
=={{header|GAP}}==
Line 1,196 ⟶ 1,278:
"62060698786608744707"
julia> bigstr[end-
"
=={{header|Klong}}==
Line 1,233 ⟶ 1,315:
=={{header|langur}}==
Arbitrary precision is native in langur.
<syntaxhighlight lang="langur">val xs = string(5 ^ 4 ^ 3 ^ 2)
writeln
s2s(xs, -20 .. -1) == "92256259918212890625" {
writeln "SUCCESS"
}
</syntaxhighlight>
Line 1,968 ⟶ 2,045:
{{works with|Rakudo|2022.07}}
<syntaxhighlight lang="raku" line>
given [**] 5, 4, 3, 2 {
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 expected first and last twenty digits
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 =
{{out}}
<pre>
Line 2,500 ⟶ 2,582:
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="
import "./big" for BigInt
var p = BigInt.three.pow(BigInt.two)
|