Integer long division: Difference between revisions

m (→‎{{header|Perl}}: cosmetic upgrade)
Line 45:
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:
<lang jq># "\u0305"
def overline: explode | map(., 773) | implode;</lang>
 
<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</lang>
{{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>
 
2,446

edits