Long multiplication: Difference between revisions
Content added Content deleted
(→{{header|Scheme}}: added output (!!)) |
(→{{header|Phix}}: added string a builtin versions) |
||
Line 3,957: | Line 3,957: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
=== base 10^9 === |
|||
{{Trans|Euphoria}} |
{{Trans|Euphoria}} |
||
Simple longhand multiplication. To keep things as simple as possible, this does not handle negative numbers.<br> |
Simple longhand multiplication. To keep things as simple as possible, this does not handle negative numbers.<br> |
||
Line 4,021: | Line 4,022: | ||
a*a*a*a is 340282366920938463488374607488768211456 |
a*a*a*a is 340282366920938463488374607488768211456 |
||
</pre> |
</pre> |
||
=== string === |
|||
<lang Phix>function mul(string a, b) |
|||
bool bSign = false |
|||
if a[1]='-' then {bSign,a} = {not bSign, a[2..$]} end if |
|||
if b[1]='-' then {bSign,b} = {not bSign, b[2..$]} end if |
|||
string res = repeat('0',length(a)+length(b)) |
|||
for i=1 to length(a) do |
|||
integer j=1, k=i, c=0 |
|||
while j<=length(b) or c do |
|||
c += res[-k]-'0' |
|||
if j<=length(b) then |
|||
c += (a[-i]-'0')*(b[-j]-'0') |
|||
j += 1 |
|||
end if |
|||
res[-k] = remainder(c,10)+'0' |
|||
c = floor(c/10) |
|||
k += 1 |
|||
end while |
|||
end for |
|||
res = trim_head(res,"0") |
|||
if bSign then res = '-'&res end if |
|||
return res |
|||
end function |
|||
?mul("18446744073709551616","18446744073709551616")</lang> |
|||
{{out}} |
|||
<pre> |
|||
"340282366920938463488374607488768211456" |
|||
</pre> |
|||
=== builtin === |
|||
(same output as immediately above) |
|||
<lang Phix>include mpfr.e |
|||
mpz a = mpz_init("18446744073709551616") -- or: |
|||
--mpz a = mpz_init(); mpz_ui_pow_ui(a,2,64) |
|||
mpz_mul(a,a,a) |
|||
?mpz_get_str(a)</lang> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |