Integer long division: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 17:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <gmpxx.h>
 
#include <iomanip>
Line 72:
std::cout << '\n';
}
}</langsyntaxhighlight>
 
{{out}}
Line 91:
 
=={{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 114:
() ))
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 132:
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:
<langsyntaxhighlight lang="jq"># "\u0305"
def overline: explode | map(., 773) | implode;</langsyntaxhighlight>
 
<langsyntaxhighlight lang="jq"># To take advantage of gojq's support for accurate integer division:
def idivide($j):
. as $i
Line 190:
"Period is \($r|length)\n" ;
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 239:
 
=={{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 270:
 
testrepeatingdecimals()
</langsyntaxhighlight>{{out}}
<pre>
 
Line 289:
{{trans|Wren}}
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils, tables
import bignum
 
Line 344:
echo &"{a}/{b} = {repr}"
echo &"Cycle is <{cycle}>"
echo &"Period is {period}\n"</langsyntaxhighlight>
 
{{out}}
Line 392:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use utf8;
Line 421:
 
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
Line 438:
=={{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 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 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 524:
=={{header|Vlang}}==
{{trans|wren}}
<langsyntaxhighlight lang="vlang">import math.big
 
const big_ten = big.integer_from_int(10)
Line 588:
println('period is ${res[2]}\n')
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 595:
=={{header|Wren}}==
This is based on the Python code [http://codepad.org/hKboFPd2 here].
<langsyntaxhighlight lang="ecmascript">import "/big" for BigInt
 
var divide = Fn.new { |m, n|
Line 649:
System.print("Repetend is '%(res[1])'")
System.print("Period is %(res[2])\n")
}</langsyntaxhighlight>
 
{{out}}
10,327

edits