Integer long division: Difference between revisions

m
→‎{{header|RPL}}: Added comments for HP-49 version
m (→‎{{header|Phix}}: Use proper English)
m (→‎{{header|RPL}}: Added comments for HP-49 version)
 
(9 intermediate revisions by 8 users not shown)
Line 15:
*[[Long primes]]
<br><br>
=={{header|C++}}==
{{libheader|GMP}}
<syntaxhighlight lang="cpp">#include <gmpxx.h>
 
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
 
using big_int = mpz_class;
 
std::pair<std::string, size_t> divide(const big_int& n, const big_int& d) {
assert(n >= 0);
assert(d > 0);
std::string result = big_int(n / d).get_str();
result += '.';
big_int c = 10 * (n % d);
size_t digits = 0;
std::map<big_int, size_t> seen;
while (seen.count(c) == 0) {
if (c == 0) {
if (result.back() == '.')
result.pop_back();
return {result, 0};
}
seen[c] = digits++;
if (c < d) {
result += '0';
c *= 10;
} else {
result += big_int(c / d).get_str();
c = 10 * (c % d);
}
}
return {result, digits - seen[c]};
}
 
int main() {
big_int test[][2] = {
{0, 1}, {1, 1}, {1, 5},
{1, 3}, {1, 7}, {83, 60},
{1, 17}, {10, 13}, {3227, 555},
{1, 149}, {1, 5261}, {476837158203125, big_int("9223372036854775808")}};
for (auto [n, d] : test) {
auto [result, period] = divide(n, d);
std::string str = n.get_str();
str += '/';
str += d.get_str();
std::string repetend = result.substr(result.size() - period);
if (repetend.size() > 30)
repetend.replace(15, repetend.size() - 30, "...");
result.resize(result.size() - period);
std::cout << std::setw(35) << str << " = " << result;
if (period != 0)
std::cout << '{' << repetend << "} (period " << period << ')';
std::cout << '\n';
}
}</syntaxhighlight>
 
{{out}}
<pre>
0/1 = 0
1/1 = 1
1/5 = 0.2
1/3 = 0.{3} (period 1)
1/7 = 0.{142857} (period 6)
83/60 = 1.38{3} (period 1)
1/17 = 0.{0588235294117647} (period 16)
10/13 = 0.{769230} (period 6)
3227/555 = 5.8{144} (period 3)
1/149 = 0.{006711409395973...087248322147651} (period 148)
1/5261 = 0.{000190077931952...263257935753659} (period 1052)
476837158203125/9223372036854775808 = 0.000051698788284564229679463043254372678347863256931304931640625
</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defun $/ (a b)
"Divide a/b with infinite precision printing each digit as it is calculated and return the period length"
Line 39 ⟶ 114:
() ))
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 45 ⟶ 120:
00067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651
148
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with gojq, the Go implementation of jq'''
 
gojq supports unbounded-precision integer arithmetic, which makes light work of the task.
 
This entry uses the "(repetend)" convention (e.g. 1/3 => 0.(3)),
partly because the Unicode "overline" might not display properly.
In case the overline style is preferred, simply use the following function in the obvious way:
<syntaxhighlight lang="jq"># "\u0305"
def overline: explode | map(., 773) | implode;</syntaxhighlight>
 
<syntaxhighlight lang="jq"># To take advantage of gojq's support for accurate integer division:
def idivide($j):
. as $i
| ($i % $j) as $mod
| ($i - $mod) / $j ;
 
# If $m >= 0, $n > 0 then emit [ s, repetend ]
# where s is the string representation of $m/$n (possibly including trailing dots);
# repetend is a string giving the repeating part of the decimal if any.
#
def integer_division($m; $n):
if $m < 0 then "numerator must not be negative" | error
elif $n <= 0 then "denominator must be positive" | error
else ($m | idivide($n) | tostring + ".") as $quotient
| {c: (($m % $n) * 10), $quotient }
| until (.c <= 0 or .c >= $n; .c *= 10 | .quotient += "0")
| . + { digits: "", passed: {}, i: 0, emit: false }
| until (.emit;
(.c | tostring) as $cs
| if .passed|has($cs)
then .quotient = .quotient + .digits[: .passed[$cs]]
| .emit = {quotient, repetend: .digits[.passed[$cs] :] }
else .q = (.c | idivide($n))
| .r = .c % $n
| .passed[$cs] = .i
| .digits += (.q|tostring)
| .i += 1
| .c = .r * 10
end
)
end
| .emit
# move zeros from the tail of .repetend if possible
| until ( .repetend[-1:] != "0" or .quotient[-1:] != "0";
.quotient |= .[:-1]
| .repetend |= "0" + .[:-1] )
| if .repetend != "0" and (.repetend|length > 0)
then [.quotient + "(" + .repetend + ")", .repetend]
else [(.quotient | sub("[.]$"; "")),
(.repetend | if . == "0" then "" else . end)]
end ;
 
def examples:
[0, 1], [1, 1], [1, 3], [1, 7], [83,60], [1, 17], [10, 13], [3227, 555],
[476837158203125, 9223372036854775808],
[1, 149], [1, 5261]
;
 
def task:
examples as [$a, $b]
| (integer_division($a; $b)) as [$s, $r]
|"\($a)/\($b) = \($s)",
"Repetend is \($r)",
"Period is \($r|length)\n" ;
 
task</syntaxhighlight>
{{out}}
<pre>
0/1 = 0
Repetend is
Period is 0
 
1/1 = 1
Repetend is
Period is 0
 
1/3 = 0.(3)
Repetend is 3
Period is 1
 
1/7 = 0.(142857)
Repetend is 142857
Period is 6
 
83/60 = 1.38(3)
Repetend is 3
Period is 1
 
1/17 = 0.(0588235294117647)
Repetend is 0588235294117647
Period is 16
 
10/13 = 0.(769230)
Repetend is 769230
Period is 6
 
3227/555 = 5.8(144)
Repetend is 144
Period is 3
 
476837158203125/9223372036854775808 = 0.000051698788284564229679463043254372678347863256931304931640625
Repetend is
Period is 0
 
1/149 = 0.(0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651)
Repetend is 0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651
Period is 148
 
1/5261 = 0.(00019007793195210036114807070899068618133434708230374453525945637711461699296711651777228663752138376734461129062915795476145219540011404675917126021668884242539441170880060824938224672115567382626877019578026991066337198251283026040676677437749477285687131724006842805550275613001330545523664702528036494962934803269340429576126211746816194639802318950769815624406006462649686371412279034404105683330165367800798327314198821516821896977760881961604257745675727048089716783881391370461889374643603877589811822847367420642463409998099220680478996388519292910093138186656529176962554647405436228853830070328834822277133624786162326553887093708420452385478045998859532408287397833111575746055882911993917506177532788443261737312298042197300893366280174871697395932332256225052271431286827599315719444972438699866945447633529747196350503706519673065957042387378825318380536019768104923018437559399353735031362858772096559589431666983463219920167268580117848317810302223911803839574225432427295191028321611860862953811062535639612241018817715263257935753659)
Repetend is 00019007793195210036114807070899068618133434708230374453525945637711461699296711651777228663752138376734461129062915795476145219540011404675917126021668884242539441170880060824938224672115567382626877019578026991066337198251283026040676677437749477285687131724006842805550275613001330545523664702528036494962934803269340429576126211746816194639802318950769815624406006462649686371412279034404105683330165367800798327314198821516821896977760881961604257745675727048089716783881391370461889374643603877589811822847367420642463409998099220680478996388519292910093138186656529176962554647405436228853830070328834822277133624786162326553887093708420452385478045998859532408287397833111575746055882911993917506177532788443261737312298042197300893366280174871697395932332256225052271431286827599315719444972438699866945447633529747196350503706519673065957042387378825318380536019768104923018437559399353735031362858772096559589431666983463219920167268580117848317810302223911803839574225432427295191028321611860862953811062535639612241018817715263257935753659
Period is 1052
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function f2d(numr, denr)
dpart, remainders, r = "", Dict{BigInt, Int}(), BigInt(numr) % denr
while (r != 0) && !haskey(remainders, r)
Line 79 ⟶ 270:
 
testrepeatingdecimals()
</langsyntaxhighlight>{{out}}
<pre>
 
Line 98 ⟶ 289:
{{trans|Wren}}
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils, tables
import bignum
 
Line 153 ⟶ 344:
echo &"{a}/{b} = {repr}"
echo &"Cycle is <{cycle}>"
echo &"Period is {period}\n"</langsyntaxhighlight>
 
{{out}}
Line 201 ⟶ 392:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use utf8;
binmode(STDOUT, ':utf8');
 
sub long_division {
Line 223 ⟶ 416:
 
my $period = length($fraction) - $seen{$numerator};
substr ($fraction, $seen{$numerator}+(2*$_)+1, 0, '"\N{';COMBINING $fractionOVERLINE}") for 0 .=. '}'$period-1;
$period, $negative . $fraction
}
 
printf "%10s Period is %5d : %s\n", $_, long_division split '/'
for <0/1 1/1 1/5 1/3 -1/3 1/7 -83/60 1/17 10/13 3227/555 1/149></langsyntaxhighlight>
{{out}}
<pre> 0/1 Period is 0 : 0
1/1 Period is 0 : 1
1/5 Period is 0 : 0.2
1/3 Period is 1 : 0.{3}
-1/3 Period is 1 : -0.{3}
1/7 Period is 6 : 0.{142857}1̅4̅2̅8̅5̅7̅
-83/60 Period is 31 : -1.38{3}383̅
1/17 Period is 16 : 0.{0588235294117647}0̅5̅8̅8̅2̅3̅5̅2̅9̅4̅1̅1̅7̅6̅4̅7̅
10/13 Period is 6 : 0.{769230}7̅6̅9̅2̅3̅0̅
3227/555 Period is 43 : 5.8{144}81̅4̅4̅
476837158203125/9223372036854775808 Period is 0 : 0.0000516987882845642321427703791414387524127960205078125
1/149 Period is 148 : 0.{0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651}</pre>
1/149 Period is 148 : 0.0̅0̅6̅7̅1̅1̅4̅0̅9̅3̅9̅5̅9̅7̅3̅1̅5̅4̅3̅6̅2̅4̅1̅6̅1̅0̅7̅3̅8̅2̅5̅5̅0̅3̅3̅5̅5̅7̅0̅4̅6̅9̅7̅9̅8̅6̅5̅7̅7̅1̅8̅1̅2̅0̅8̅0̅5̅3̅6̅9̅1̅2̅7̅5̅1̅6̅7̅7̅8̅5̅2̅3̅4̅8̅9̅9̅3̅2̅8̅8̅5̅9̅0̅6̅0̅4̅0̅2̅6̅8̅4̅5̅6̅3̅7̅5̅8̅3̅8̅9̅2̅6̅1̅7̅4̅4̅9̅6̅6̅4̅4̅2̅9̅5̅3̅0̅2̅0̅1̅3̅4̅2̅2̅8̅1̅8̅7̅9̅1̅9̅4̅6̅3̅0̅8̅7̅2̅4̅8̅3̅2̅2̅1̅4̅7̅6̅5̅1̅</pre>
 
=={{header|Phix}}==
Translation of the Python code linked to by the Wren entry, modified to cope with negatives.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 287 ⟶ 481:
<span style="color: #0000FF;">{</span><span style="color: #000000;">476837158203125</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9223372036854775808</span><span style="color: #0000FF;">}}[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">)]</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">,</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
The results below are on 64 bit, not surprisingly the last example is inaccurate past 16 significant digits on 32 bit (ditto p2js), and hence omitted.
Line 311 ⟶ 505:
=={{header|Raku}}==
It's a built-in.
<syntaxhighlight lang="raku" perl6line>for 0/1, 1/1, 1/3, 1/7, -83/60, 1/17, 10/13, 3227/555, 5**21/2**63, 1/149, 1/5261 -> $rat {
printf "%35s - Period is %-5s: %s%s\n", $rat.nude.join('/'), .[1].chars, .[0], (.[1].comb Z~ "\c[COMBINING OVERLINE]" xx *).join
given $rat.base-repeating
}</langsyntaxhighlight>
{{out}}
<pre style="overflow:auto;white-space:revert;"> 0/1 - Period is 0 : 0
Line 327 ⟶ 521:
1/149 - Period is 148 : 0.0̅0̅6̅7̅1̅1̅4̅0̅9̅3̅9̅5̅9̅7̅3̅1̅5̅4̅3̅6̅2̅4̅1̅6̅1̅0̅7̅3̅8̅2̅5̅5̅0̅3̅3̅5̅5̅7̅0̅4̅6̅9̅7̅9̅8̅6̅5̅7̅7̅1̅8̅1̅2̅0̅8̅0̅5̅3̅6̅9̅1̅2̅7̅5̅1̅6̅7̅7̅8̅5̅2̅3̅4̅8̅9̅9̅3̅2̅8̅8̅5̅9̅0̅6̅0̅4̅0̅2̅6̅8̅4̅5̅6̅3̅7̅5̅8̅3̅8̅9̅2̅6̅1̅7̅4̅4̅9̅6̅6̅4̅4̅2̅9̅5̅3̅0̅2̅0̅1̅3̅4̅2̅2̅8̅1̅8̅7̅9̅1̅9̅4̅6̅3̅0̅8̅7̅2̅4̅8̅3̅2̅2̅1̅4̅7̅6̅5̅1̅
1/5261 - Period is 1052 : 0.0̅0̅0̅1̅9̅0̅0̅7̅7̅9̅3̅1̅9̅5̅2̅1̅0̅0̅3̅6̅1̅1̅4̅8̅0̅7̅0̅7̅0̅8̅9̅9̅0̅6̅8̅6̅1̅8̅1̅3̅3̅4̅3̅4̅7̅0̅8̅2̅3̅0̅3̅7̅4̅4̅5̅3̅5̅2̅5̅9̅4̅5̅6̅3̅7̅7̅1̅1̅4̅6̅1̅6̅9̅9̅2̅9̅6̅7̅1̅1̅6̅5̅1̅7̅7̅7̅2̅2̅8̅6̅6̅3̅7̅5̅2̅1̅3̅8̅3̅7̅6̅7̅3̅4̅4̅6̅1̅1̅2̅9̅0̅6̅2̅9̅1̅5̅7̅9̅5̅4̅7̅6̅1̅4̅5̅2̅1̅9̅5̅4̅0̅0̅1̅1̅4̅0̅4̅6̅7̅5̅9̅1̅7̅1̅2̅6̅0̅2̅1̅6̅6̅8̅8̅8̅4̅2̅4̅2̅5̅3̅9̅4̅4̅1̅1̅7̅0̅8̅8̅0̅0̅6̅0̅8̅2̅4̅9̅3̅8̅2̅2̅4̅6̅7̅2̅1̅1̅5̅5̅6̅7̅3̅8̅2̅6̅2̅6̅8̅7̅7̅0̅1̅9̅5̅7̅8̅0̅2̅6̅9̅9̅1̅0̅6̅6̅3̅3̅7̅1̅9̅8̅2̅5̅1̅2̅8̅3̅0̅2̅6̅0̅4̅0̅6̅7̅6̅6̅7̅7̅4̅3̅7̅7̅4̅9̅4̅7̅7̅2̅8̅5̅6̅8̅7̅1̅3̅1̅7̅2̅4̅0̅0̅6̅8̅4̅2̅8̅0̅5̅5̅5̅0̅2̅7̅5̅6̅1̅3̅0̅0̅1̅3̅3̅0̅5̅4̅5̅5̅2̅3̅6̅6̅4̅7̅0̅2̅5̅2̅8̅0̅3̅6̅4̅9̅4̅9̅6̅2̅9̅3̅4̅8̅0̅3̅2̅6̅9̅3̅4̅0̅4̅2̅9̅5̅7̅6̅1̅2̅6̅2̅1̅1̅7̅4̅6̅8̅1̅6̅1̅9̅4̅6̅3̅9̅8̅0̅2̅3̅1̅8̅9̅5̅0̅7̅6̅9̅8̅1̅5̅6̅2̅4̅4̅0̅6̅0̅0̅6̅4̅6̅2̅6̅4̅9̅6̅8̅6̅3̅7̅1̅4̅1̅2̅2̅7̅9̅0̅3̅4̅4̅0̅4̅1̅0̅5̅6̅8̅3̅3̅3̅0̅1̅6̅5̅3̅6̅7̅8̅0̅0̅7̅9̅8̅3̅2̅7̅3̅1̅4̅1̅9̅8̅8̅2̅1̅5̅1̅6̅8̅2̅1̅8̅9̅6̅9̅7̅7̅7̅6̅0̅8̅8̅1̅9̅6̅1̅6̅0̅4̅2̅5̅7̅7̅4̅5̅6̅7̅5̅7̅2̅7̅0̅4̅8̅0̅8̅9̅7̅1̅6̅7̅8̅3̅8̅8̅1̅3̅9̅1̅3̅7̅0̅4̅6̅1̅8̅8̅9̅3̅7̅4̅6̅4̅3̅6̅0̅3̅8̅7̅7̅5̅8̅9̅8̅1̅1̅8̅2̅2̅8̅4̅7̅3̅6̅7̅4̅2̅0̅6̅4̅2̅4̅6̅3̅4̅0̅9̅9̅9̅8̅0̅9̅9̅2̅2̅0̅6̅8̅0̅4̅7̅8̅9̅9̅6̅3̅8̅8̅5̅1̅9̅2̅9̅2̅9̅1̅0̅0̅9̅3̅1̅3̅8̅1̅8̅6̅6̅5̅6̅5̅2̅9̅1̅7̅6̅9̅6̅2̅5̅5̅4̅6̅4̅7̅4̅0̅5̅4̅3̅6̅2̅2̅8̅8̅5̅3̅8̅3̅0̅0̅7̅0̅3̅2̅8̅8̅3̅4̅8̅2̅2̅2̅7̅7̅1̅3̅3̅6̅2̅4̅7̅8̅6̅1̅6̅2̅3̅2̅6̅5̅5̅3̅8̅8̅7̅0̅9̅3̅7̅0̅8̅4̅2̅0̅4̅5̅2̅3̅8̅5̅4̅7̅8̅0̅4̅5̅9̅9̅8̅8̅5̅9̅5̅3̅2̅4̅0̅8̅2̅8̅7̅3̅9̅7̅8̅3̅3̅1̅1̅1̅5̅7̅5̅7̅4̅6̅0̅5̅5̅8̅8̅2̅9̅1̅1̅9̅9̅3̅9̅1̅7̅5̅0̅6̅1̅7̅7̅5̅3̅2̅7̅8̅8̅4̅4̅3̅2̅6̅1̅7̅3̅7̅3̅1̅2̅2̅9̅8̅0̅4̅2̅1̅9̅7̅3̅0̅0̅8̅9̅3̅3̅6̅6̅2̅8̅0̅1̅7̅4̅8̅7̅1̅6̅9̅7̅3̅9̅5̅9̅3̅2̅3̅3̅2̅2̅5̅6̅2̅2̅5̅0̅5̅2̅2̅7̅1̅4̅3̅1̅2̅8̅6̅8̅2̅7̅5̅9̅9̅3̅1̅5̅7̅1̅9̅4̅4̅4̅9̅7̅2̅4̅3̅8̅6̅9̅9̅8̅6̅6̅9̅4̅5̅4̅4̅7̅6̅3̅3̅5̅2̅9̅7̅4̅7̅1̅9̅6̅3̅5̅0̅5̅0̅3̅7̅0̅6̅5̅1̅9̅6̅7̅3̅0̅6̅5̅9̅5̅7̅0̅4̅2̅3̅8̅7̅3̅7̅8̅8̅2̅5̅3̅1̅8̅3̅8̅0̅5̅3̅6̅0̅1̅9̅7̅6̅8̅1̅0̅4̅9̅2̅3̅0̅1̅8̅4̅3̅7̅5̅5̅9̅3̅9̅9̅3̅5̅3̅7̅3̅5̅0̅3̅1̅3̅6̅2̅8̅5̅8̅7̅7̅2̅0̅9̅6̅5̅5̅9̅5̅8̅9̅4̅3̅1̅6̅6̅6̅9̅8̅3̅4̅6̅3̅2̅1̅9̅9̅2̅0̅1̅6̅7̅2̅6̅8̅5̅8̅0̅1̅1̅7̅8̅4̅8̅3̅1̅7̅8̅1̅0̅3̅0̅2̅2̅2̅3̅9̅1̅1̅8̅0̅3̅8̅3̅9̅5̅7̅4̅2̅2̅5̅4̅3̅2̅4̅2̅7̅2̅9̅5̅1̅9̅1̅0̅2̅8̅3̅2̅1̅6̅1̅1̅8̅6̅0̅8̅6̅2̅9̅5̅3̅8̅1̅1̅0̅6̅2̅5̅3̅5̅6̅3̅9̅6̅1̅2̅2̅4̅1̅0̅1̅8̅8̅1̅7̅7̅1̅5̅2̅6̅3̅2̅5̅7̅9̅3̅5̅7̅5̅3̅6̅5̅9̅</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
{{trans|Nim}}
« DUP2 /
'''IF''' DUP FP '''THEN'''
IP "." + ROT ROT <span style="color:grey">@ HP49+: add R→I after IP</span>
ABS SWAP ABS OVER MOD 10 * { }
→ quotient n c passed
« ""
'''WHILE''' c DUP n < AND '''REPEAT'''
10 'c' STO*
'quotient' "0" STO+
'''END'''
'''WHILE''' passed c POS NOT '''REPEAT'''
'passed' c STO+
c n / IP + <span style="color:grey">@ HP49+: replace / IP by IQUOT</span>
c n MOD 10 * 'c' STO
'''END'''
passed c POS DUP2 1 SWAP 1 - SUB
quotient SWAP + ROT ROT
OVER SIZE SUB
'''IF''' DUP "0" == '''THEN''' DROP "" '''END'''
»
'''ELSE''' →STR ROT ROT DROP2 "" '''END'''
» '<span style="color:blue">LDIV</span>' STO <span style="color:grey">@ ''( m n → "quotient.prefix" "repetend" )'' </span>
« DUP2 "/" SWAP + + " = " + ROT ROT
<span style="color:blue">LDIV</span>
'''IF''' DUP SIZE '''THEN''' ")" + SWAP "(" + SWAP + '''ELSE''' DROP '''END'''
+
» '<span style="color:blue">VUDIV</span>' STO <span style="color:grey">@ ''@ ( m n → "m/n = quotient.prefix(repetend)" )'' </span>
 
-3227 555 <span style="color:blue">VUDIV</span>
355 113 <span style="color:blue">VUDIV</span>
{{out}}
<pre>
2: "-3227/555 = -5.8(144)"
1: 355/113 = 3.(1415929203539823008849557522123893805309734513274336283185840707964601769911504424778761061946902654867256637168)
</pre>
 
=={{header|V (Vlang)}}==
{{trans|wren}}
<syntaxhighlight lang="v (vlang)">import math.big
 
const big_ten = big.integer_from_int(10)
 
fn divide(m big.Integer, n big.Integer) ?[]string {
if m < big.zero_int {
return error('m must not be negative')
}
if n <= big.zero_int {
return error('n must be positive')
}
mut quotient := '${(m/n)}.'
mut c := (m % n) * big_ten
mut zeros := 0
for c > big.zero_int && c < n {
c = c * big_ten
quotient = quotient + "0"
zeros ++
}
mut digits := ""
mut passed := map[string]int{}//string:int
mut i := 0
for {
mut cs := c.str()
if cs in passed {
prefix := digits[0..passed[cs]]
mut repetend := digits[passed[cs]..digits.len]
mut result := '$quotient${prefix}(${repetend})'
result = result.replace("(0)", "").trim_right(".")
index := result.index("(") or {-1}
if index == -1 {
return [result, "", '0']
}
result = result.replace("(", "").replace(")", "")
for _ in 0..zeros {
if repetend[repetend.len-1] == 0 {
result = result[0..result.len-1]
repetend = "0" + repetend[0..result.len-1]
} else {
break
}
}
return [result + "....", repetend, repetend.len.str()]
}
q := c / n
r := c % n
passed[cs] = i
digits += q.str()
i++
c = r * big_ten
}
return ['FAIL','','']
}
 
fn main(){
for test in [[0, 1], [1, 1], [1, 3], [1, 7], [83,60], [1, 17], [10, 13], [3227, 555],
[476837158203125, 9223372036854775808], [1, 149], [1, 5261]] {
a := big.integer_from_i64(test[0])
b := big.integer_from_i64(test[1])
res := divide(a,b) or {['Need positive numbers','','']}
println('$a/$b = ${res[0]}')
println("repetend is '${res[1]}'")
println('period is ${res[2]}\n')
}
}</syntaxhighlight>
{{out}}
<pre>
Similar as wren entry</pre>
 
=={{header|Wren}}==
This is based on the Python code [http://codepad.org/hKboFPd2 here].
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
 
var divide = Fn.new { |m, n|
Line 384 ⟶ 689:
System.print("Repetend is '%(res[1])'")
System.print("Period is %(res[2])\n")
}</langsyntaxhighlight>
 
{{out}}
1,150

edits