Integer long division: Difference between revisions
Content added Content deleted
m (remove unneeded code) |
(→{{header|Wren}}: Amended so can deal with positive integers of unlimited size (limited to 15 digits previously).) |
||
Line 118: | Line 118: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
This is based on the Python code [http://codepad.org/hKboFPd2 here]. |
This is based on the Python code [http://codepad.org/hKboFPd2 here]. |
||
<lang ecmascript> |
<lang ecmascript>import "/big" for BigInt |
||
var divide = Fn.new { |m, n| |
|||
if (m < 0) Fiber.abort("m must not be negative") |
if (m < 0) Fiber.abort("m must not be negative") |
||
if (n <= 0) Fiber.abort("n must be positive.") |
if (n <= 0) Fiber.abort("n must be positive.") |
||
var quotient = (m/n) |
var quotient = (m/n).toString + "." |
||
var c = |
var c = (m % n) * 10 |
||
var zeros = 0 |
var zeros = 0 |
||
while (c > 0 && c < n) { |
while (c > 0 && c < n) { |
||
Line 133: | Line 135: | ||
var i = 0 |
var i = 0 |
||
while (true) { |
while (true) { |
||
var cs = c.toString |
|||
if (passed.containsKey(cs)) { |
|||
⚫ | |||
var |
var prefix = digits[0...passed[cs]] |
||
⚫ | |||
var result = quotient + prefix + "(" + repetend + ")" |
var result = quotient + prefix + "(" + repetend + ")" |
||
result = result.replace("(0)", "").trimEnd(".") |
result = result.replace("(0)", "").trimEnd(".") |
||
Line 149: | Line 152: | ||
return [result + "....", repetend, repetend.count] |
return [result + "....", repetend, repetend.count] |
||
} |
} |
||
var q = |
var q = c / n |
||
var r = c % n |
var r = c % n |
||
passed[ |
passed[cs] = i |
||
digits = digits + q.toString |
digits = digits + q.toString |
||
i = i + 1 |
i = i + 1 |
||
c = |
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) { |
for (test in tests) { |
||
var a = test[0] |
var a = BigInt.new(test[0]) |
||
var b = test[1] |
var b = BigInt.new(test[1]) |
||
var res = divide.call(a, b) |
var res = divide.call(a, b) |
||
System.print("%(a)/%(b) = %(res[0])") |
System.print("%(a)/%(b) = %(res[0])") |
||
Line 170: | Line 176: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
0/1 = 0 |
|||
Repetend is '' |
|||
Period is 0 |
|||
1/1 = 1 |
1/1 = 1 |
||
Repetend is '' |
Repetend is '' |
||
Line 181: | Line 191: | ||
Repetend is '142857' |
Repetend is '142857' |
||
Period is 6 |
Period is 6 |
||
83/60 = 1.383.... |
|||
Repetend is '3' |
|||
Period is 1 |
|||
1/17 = 0.0588235294117647.... |
1/17 = 0.0588235294117647.... |
||
Line 193: | Line 207: | ||
Repetend is '144' |
Repetend is '144' |
||
Period is 3 |
Period is 3 |
||
476837158203125/9223372036854775808 = 0.000051698788284564229679463043254372678347863256931304931640625 |
|||
Repetend is '' |
|||
Period is 0 |
|||
1/149 = 0.0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651.... |
1/149 = 0.0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651.... |
||
Repetend is '0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651' |
Repetend is '0067114093959731543624161073825503355704697986577181208053691275167785234899328859060402684563758389261744966442953020134228187919463087248322147651' |
||
Period is 148 |
Period is 148 |
||
1/5261 = 0.00019007793195210036114807070899068618133434708230374453525945637711461699296711651777228663752138376734461129062915795476145219540011404675917126021668884242539441170880060824938224672115567382626877019578026991066337198251283026040676677437749477285687131724006842805550275613001330545523664702528036494962934803269340429576126211746816194639802318950769815624406006462649686371412279034404105683330165367800798327314198821516821896977760881961604257745675727048089716783881391370461889374643603877589811822847367420642463409998099220680478996388519292910093138186656529176962554647405436228853830070328834822277133624786162326553887093708420452385478045998859532408287397833111575746055882911993917506177532788443261737312298042197300893366280174871697395932332256225052271431286827599315719444972438699866945447633529747196350503706519673065957042387378825318380536019768104923018437559399353735031362858772096559589431666983463219920167268580117848317810302223911803839574225432427295191028321611860862953811062535639612241018817715263257935753659.... |
|||
Repetend is '00019007793195210036114807070899068618133434708230374453525945637711461699296711651777228663752138376734461129062915795476145219540011404675917126021668884242539441170880060824938224672115567382626877019578026991066337198251283026040676677437749477285687131724006842805550275613001330545523664702528036494962934803269340429576126211746816194639802318950769815624406006462649686371412279034404105683330165367800798327314198821516821896977760881961604257745675727048089716783881391370461889374643603877589811822847367420642463409998099220680478996388519292910093138186656529176962554647405436228853830070328834822277133624786162326553887093708420452385478045998859532408287397833111575746055882911993917506177532788443261737312298042197300893366280174871697395932332256225052271431286827599315719444972438699866945447633529747196350503706519673065957042387378825318380536019768104923018437559399353735031362858772096559589431666983463219920167268580117848317810302223911803839574225432427295191028321611860862953811062535639612241018817715263257935753659' |
|||
Period is 1052 |
|||
</pre> |
</pre> |