I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Integer long division

From Rosetta Code
Integer long division is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Write a function that prints the result of the division of two positive integers with infinite precision (only limited by the available memory), stopping before the period starts repeating itself. Return also the length of this period (0 if there is no period).

Demonstrate it with the division 1/149, whose result is 0.0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651, where the last 148 digits repeat endlessly.

The result could be stored as a string or simply output to the screen.
Note that the division of any two integers will always produce a period, but if the numerator is an exact multiple of the denominator, or if the denominator contains only the factors 2 and 5, the period will be 0. In the remaining cases, these possible 2's and 5's of the denominator produce a leading number of digits in the quotient, but have no effect on the period.


References


Related



Common Lisp[edit]

 
(defun $/ (a b)
"Divide a/b with infinite precision printing each digit as it is calculated and return the period length"
; ($/ 1 17) => 588235294117647 ; 16
(assert (and (integerp a) (integerp b) (not (zerop b))))
(do* (c
(i0 (1+ (max (factor-multiplicity b 2) (factor-multiplicity b 5)))) ; the position which marks the beginning of the period
(r a (* 10 r)) ; remainder
(i 0 (1+ i)) ; iterations counter
(rem (if (= i i0) r -1) (if (= i i0) r rem)) ) ; the first remainder against which to check for repeating remainders
((and (= r rem) (not (= i i0))) (- i i0))
(multiple-value-setq (c r) (floor r b))
(princ c) ))
 
 
(defun factor-multiplicity (n factor)
"Return how many times the factor is contained in n"
; (factor-multiplicity 12 2) => 2
(do* ((i 0 (1+ i))
(n (/ n factor) (/ n factor)) )
((not (integerp n)) i)
() ))
 
 
Output:
($/ 1 149) 
00067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651
148

Julia[edit]

function f2d(numr, denr)
dpart, remainders, r = "", Dict{BigInt, Int}(), BigInt(numr) % denr
while (r != 0) && !haskey(remainders, r)
remainders[r] = length(dpart)
r *= 10
partrem, r = divrem(r, denr)
dpart *= string(partrem)
end
return r == 0 ? (0, 0) : (dpart[remainders[r]+1:end], remainders[r])
end
 
overline(s) = mapreduce(c -> "\u0305" * c, *, s)
 
testpairs = [(0, 1), (1, 1), (1, 3), (1, 7), (-83, 60), (1, 17), (10, 13), (3227, 555),
(5^21, Int128(2)^63), (1, 149), (1, 5261)]
 
function testrepeatingdecimals()
for (numr, denr) in testpairs
n = numr < 0 ? -numr : numr
repeated, extra = f2d(n, denr)
if repeated == 0
println(lpad("$numr/$denr", 36), " (Period 0) = ", BigFloat(numr)/denr)
else
prefix, suffix = split(string(BigFloat(numr) / denr)[begin:end-2], ".")
println(lpad("$numr/$denr", 36), " (Period ", rpad("$(length(repeated)))", 6),
" = $prefix.$(suffix[begin:extra])$(overline(repeated))")
end
end
end
 
testrepeatingdecimals()
 
Output:
                                 0/1  (Period 0)     = 0.0
                                 1/1  (Period 0)     = 1.0
                                 1/3  (Period 1)     = 0.̅3
                                 1/7  (Period 6)     = 0.̅1̅4̅2̅8̅5̅7
                              -83/60  (Period 1)     = -1.38̅3
                                1/17  (Period 16)    = 0.̅0̅5̅8̅8̅2̅3̅5̅2̅9̅4̅1̅1̅7̅6̅4̅7
                               10/13  (Period 6)     = 0.̅7̅6̅9̅2̅3̅0
                            3227/555  (Period 3)     = 5.8̅1̅4̅4
 476837158203125/9223372036854775808  (Period 0)     = 5.1698788284564229679463043254372678347863256931304931640625e-05
                               1/149  (Period 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 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

Nim[edit]

Translation of: Wren
Library: bignum
import strformat, strutils, tables
import bignum
 
proc divide(m, n: Int): tuple[repr: string; cycle: string; period: int] =
doAssert m >= 0, "m must not be negative."
doAssert n > 0, "n must be positive."
 
var quotient = &"{m div n}."
var c = m mod n * 10
var zeros = 0
while c > 0 and c < n:
c *= 10
quotient &= '0'
inc zeros
 
var digits = ""
var passed: Table[string, int]
var i = 0
while true:
let cs = $c
if cs in passed:
let idx = passed[cs]
let prefix = digits[0..<idx]
var cycle = digits[idx..^1]
var repr = &"{quotient}{prefix}({cycle})"
repr = repr.replace("(0)", "").strip(leading = false, trailing = true, {'.'})
let index = repr.find('(')
if index < 0: return (repr, "", 0)
repr = repr.multiReplace(("(", ""), (")", ""))
for _ in 1..zeros:
if cycle[^1] == '0':
repr.setLen(repr.len - 1)
cycle = '0' & cycle[0..^2]
else:
break
return (repr & "...", cycle, cycle.len)
 
let q = c div n
let r = c mod n
passed[cs] = i
digits &= $q
inc i
c = r * 10
 
 
const Tests = [("0", "1"), ("1", "1"), ("1", "3"), ("1", "7"),
("83","60"), ("1", "17"), ("10", "13"), ("3227", "555"),
("476837158203125", "9223372036854775808"), ("1", "149"), ("1", "5261")]
 
for test in Tests:
let a = newInt(test[0])
let b = newInt(test[1])
let (repr, cycle, period) = divide(a, b)
echo &"{a}/{b} = {repr}"
echo &"Cycle is <{cycle}>"
echo &"Period is {period}\n"
Output:
0/1 = 0
Cycle is <>
Period is 0

1/1 = 1
Cycle is <>
Period is 0

1/3 = 0.3...
Cycle is <3>
Period is 1

1/7 = 0.142857...
Cycle is <142857>
Period is 6

83/60 = 1.383...
Cycle is <3>
Period is 1

1/17 = 0.0588235294117647...
Cycle is <0588235294117647>
Period is 16

10/13 = 0.769230...
Cycle is <769230>
Period is 6

3227/555 = 5.8144...
Cycle is <144>
Period is 3

476837158203125/9223372036854775808 = 0.000051698788284564229679463043254372678347863256931304931640625
Cycle is <>
Period is 0

1/149 = 0.0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651...
Cycle is <0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651>
Period is 148

1/5261 = 0.00019007793195210036114807070899068618133434708230374453525945637711461699296711651777228663752138376734461129062915795476145219540011404675917126021668884242539441170880060824938224672115567382626877019578026991066337198251283026040676677437749477285687131724006842805550275613001330545523664702528036494962934803269340429576126211746816194639802318950769815624406006462649686371412279034404105683330165367800798327314198821516821896977760881961604257745675727048089716783881391370461889374643603877589811822847367420642463409998099220680478996388519292910093138186656529176962554647405436228853830070328834822277133624786162326553887093708420452385478045998859532408287397833111575746055882911993917506177532788443261737312298042197300893366280174871697395932332256225052271431286827599315719444972438699866945447633529747196350503706519673065957042387378825318380536019768104923018437559399353735031362858772096559589431666983463219920167268580117848317810302223911803839574225432427295191028321611860862953811062535639612241018817715263257935753659...
Cycle is <00019007793195210036114807070899068618133434708230374453525945637711461699296711651777228663752138376734461129062915795476145219540011404675917126021668884242539441170880060824938224672115567382626877019578026991066337198251283026040676677437749477285687131724006842805550275613001330545523664702528036494962934803269340429576126211746816194639802318950769815624406006462649686371412279034404105683330165367800798327314198821516821896977760881961604257745675727048089716783881391370461889374643603877589811822847367420642463409998099220680478996388519292910093138186656529176962554647405436228853830070328834822277133624786162326553887093708420452385478045998859532408287397833111575746055882911993917506177532788443261737312298042197300893366280174871697395932332256225052271431286827599315719444972438699866945447633529747196350503706519673065957042387378825318380536019768104923018437559399353735031362858772096559589431666983463219920167268580117848317810302223911803839574225432427295191028321611860862953811062535639612241018817715263257935753659>
Period is 1052

Perl[edit]

use strict;
use warnings;
use utf8;
binmode(STDOUT, ':utf8');
 
sub long_division {
my($n, $d) = @_;
my %seen;
 
my($numerator,$denominator) = (abs $n, abs $d);
my $negative = ($n < 0 xor $d < 0) ? '-' : '';
 
my $fraction = sprintf '%d.', $numerator / $denominator;
my $position = length $fraction;
$numerator %= $denominator;
while (!$seen{$numerator}) {
return 0, $fraction =~ s/\.$//r unless $numerator;
$seen{$numerator} = $position;
$fraction .= int 10 * $numerator / $denominator;
$numerator = 10 * $numerator % $denominator;
$position++;
}
 
my $period = length($fraction) - $seen{$numerator};
substr($fraction, $seen{$numerator}+(2*$_)+1, 0, "\N{COMBINING OVERLINE}") 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>
Output:
                                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.1̅4̅2̅8̅5̅7̅
                             -83/60 Period is     1 : -1.383̅
                               1/17 Period is    16 : 0.0̅5̅8̅8̅2̅3̅5̅2̅9̅4̅1̅1̅7̅6̅4̅7̅
                              10/13 Period is     6 : 0.7̅6̅9̅2̅3̅0̅
                           3227/555 Period is     3 : 5.81̅4̅4̅
476837158203125/9223372036854775808 Period is     0 : 0.0000516987882845642321427703791414387524127960205078125
                              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̅

Phix[edit]

Translation of the Python code linked to by the Wren entry, modified to cope with negatives.

with javascript_semantics
procedure test(sequence s)
    atom {n,d} = s,
         {numr,denr} = {abs(n),abs(d)},
         c = 10*remainder(numr,denr)
    string sgn = iff(sign(n)*sign(d)=-1?"-":""),
           q = sprintf("%s%d.",{sgn,floor(numr/denr)}),
           digits = ""
    while c and c<denr do
        c *= 10
        q &= '0'
    end while
    integer passed = new_dict()
    while getd_index(c,passed)=NULL do
        integer digit = floor(c/denr)
        assert(digit>=0 and digit<=9)
        digits &= '0'+digit
        setd(c,length(digits),passed)
        c = 10*remainder(c,denr)
    end while
    integer pc = getd(c,passed)
    destroy_dict(passed)
    q &= digits[1..pc-1]
    digits = digits[pc..$]
    if digits="0" then
        q = trim_tail(q,'.')
    else        
        while digits[$] = q[$] do
            {digits,q} = {q[$]&digits[1..$-1],q[1..$-1]}
        end while
        integer ld = length(digits)
        if ld>20 then digits[11..-11] = "..." end if
        q = sprintf("%s{%s} (period %d)",{q,digits,ld})
    end if
    string nod = sprintf("%d/%d",{n,d})
    printf(1,"%40s = %s\n",{nod,q})
end procedure

constant tests = {{0,1},{1,1},{1,3},{-1,3},{1,-3},{-1,-3},{-17,2},{177,16},
                  {1,7},{-83,60},{1,17},{10,13},{3227,555},{1,149},{1,5261},
                  {476837158203125,9223372036854775808}}[1..$-(machine_bits()=32)]
papply(tests,test)
Output:

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.

                                     0/1 = 0
                                     1/1 = 1
                                     1/3 = 0.{3} (period 1)
                                    -1/3 = -0.{3} (period 1)
                                    1/-3 = -0.{3} (period 1)
                                   -1/-3 = 0.{3} (period 1)
                                   -17/2 = -8.5
                                  177/16 = 11.0625
                                     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.{0067114093...8322147651} (period 148)
                                  1/5261 = 0.{0001900779...7935753659} (period 1052)
     476837158203125/9223372036854775808 = 0.000051698788284564229679463043254372678347863256931304931640625

Raku[edit]

It's a built-in.

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
}
Output:
                                0/1 - Period is 0    : 0
                                1/1 - Period is 0    : 1
                                1/3 - Period is 1    : 0.3̅
                                1/7 - Period is 6    : 0.1̅4̅2̅8̅5̅7̅
                             -83/60 - Period is 1    : -1.383̅
                               1/17 - Period is 16   : 0.0̅5̅8̅8̅2̅3̅5̅2̅9̅4̅1̅1̅7̅6̅4̅7̅
                              10/13 - Period is 6    : 0.7̅6̅9̅2̅3̅0̅
                           3227/555 - Period is 3    : 5.81̅4̅4̅
476837158203125/9223372036854775808 - Period is 0    : 0.000051698788284564229679463043254372678347863256931304931640625
                              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̅

Wren[edit]

This is based on the Python code here.

import "/big" for BigInt
 
var divide = Fn.new { |m, n|
if (m < 0) Fiber.abort("m must not be negative")
if (n <= 0) Fiber.abort("n must be positive.")
var quotient = (m/n).toString + "."
var c = (m % n) * 10
var zeros = 0
while (c > 0 && c < n) {
c = c * 10
quotient = quotient + "0"
zeros = zeros + 1
}
var digits = ""
var passed = {}
var i = 0
while (true) {
var cs = c.toString
if (passed.containsKey(cs)) {
var prefix = digits[0...passed[cs]]
var repetend = digits[passed[cs]..-1]
var result = quotient + prefix + "(" + repetend + ")"
result = result.replace("(0)", "").trimEnd(".")
var index = result.indexOf("(")
if (index == -1) return [result, "", 0]
result = result.replace("(", "").replace(")", "")
for (i in 0...zeros) {
if (repetend[-1] == "0") {
result = result[0..-2]
repetend = "0" + repetend[0..-2]
} else break
}
return [result + "....", repetend, repetend.count]
}
var q = c / n
var r = c % n
passed[cs] = i
digits = digits + q.toString
i = i + 1
c = r * 10
}
}
 
var tests = [
[0, 1], [1, 1], [1, 3], [1, 7], [83,60], [1, 17], [10, 13], [3227, 555],
[476837158203125, "9223372036854775808"], [1, 149], [1, 5261]
]
for (test in tests) {
var a = BigInt.new(test[0])
var b = BigInt.new(test[1])
var res = divide.call(a, b)
System.print("%(a)/%(b) = %(res[0])")
System.print("Repetend is '%(res[1])'")
System.print("Period is %(res[2])\n")
}
Output:
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.383....
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.8144....
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