Integer long division: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 17: Line 17:
=={{header|C++}}==
=={{header|C++}}==
{{libheader|GMP}}
{{libheader|GMP}}
<lang cpp>#include <gmpxx.h>
<syntaxhighlight lang="cpp">#include <gmpxx.h>


#include <iomanip>
#include <iomanip>
Line 72: Line 72:
std::cout << '\n';
std::cout << '\n';
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 91: Line 91:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
(defun $/ (a b)
(defun $/ (a b)
"Divide a/b with infinite precision printing each digit as it is calculated and return the period length"
"Divide a/b with infinite precision printing each digit as it is calculated and return the period length"
Line 114: Line 114:
() ))
() ))


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 132: Line 132:
partly because the Unicode "overline" might not display properly.
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:
In case the overline style is preferred, simply use the following function in the obvious way:
<lang jq># "\u0305"
<syntaxhighlight lang="jq"># "\u0305"
def overline: explode | map(., 773) | implode;</lang>
def overline: explode | map(., 773) | implode;</syntaxhighlight>


<lang jq># To take advantage of gojq's support for accurate integer division:
<syntaxhighlight lang="jq"># To take advantage of gojq's support for accurate integer division:
def idivide($j):
def idivide($j):
. as $i
. as $i
Line 190: Line 190:
"Period is \($r|length)\n" ;
"Period is \($r|length)\n" ;


task</lang>
task</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 239: Line 239:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>function f2d(numr, denr)
<syntaxhighlight lang="julia">function f2d(numr, denr)
dpart, remainders, r = "", Dict{BigInt, Int}(), BigInt(numr) % denr
dpart, remainders, r = "", Dict{BigInt, Int}(), BigInt(numr) % denr
while (r != 0) && !haskey(remainders, r)
while (r != 0) && !haskey(remainders, r)
Line 270: Line 270:


testrepeatingdecimals()
testrepeatingdecimals()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>


Line 289: Line 289:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|bignum}}
{{libheader|bignum}}
<lang Nim>import strformat, strutils, tables
<syntaxhighlight lang="nim">import strformat, strutils, tables
import bignum
import bignum


Line 344: Line 344:
echo &"{a}/{b} = {repr}"
echo &"{a}/{b} = {repr}"
echo &"Cycle is <{cycle}>"
echo &"Cycle is <{cycle}>"
echo &"Period is {period}\n"</lang>
echo &"Period is {period}\n"</syntaxhighlight>


{{out}}
{{out}}
Line 392: Line 392:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use utf8;
use utf8;
Line 421: Line 421:


printf "%10s Period is %5d : %s\n", $_, long_division split '/'
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></lang>
for <0/1 1/1 1/5 1/3 -1/3 1/7 -83/60 1/17 10/13 3227/555 1/149></syntaxhighlight>
{{out}}
{{out}}
<pre> 0/1 Period is 0 : 0
<pre> 0/1 Period is 0 : 0
Line 438: Line 438:
=={{header|Phix}}==
=={{header|Phix}}==
Translation of the Python code linked to by the Wren entry, modified to cope with negatives.
Translation of the Python code linked to by the Wren entry, modified to cope with negatives.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: 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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{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.
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: Line 505:
=={{header|Raku}}==
=={{header|Raku}}==
It's a built-in.
It's a built-in.
<lang perl6>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 {
<syntaxhighlight lang="raku" line>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
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
given $rat.base-repeating
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="overflow:auto;white-space:revert;"> 0/1 - Period is 0 : 0
<pre style="overflow:auto;white-space:revert;"> 0/1 - Period is 0 : 0
Line 524: Line 524:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|wren}}
{{trans|wren}}
<lang vlang>import math.big
<syntaxhighlight lang="vlang">import math.big


const big_ten = big.integer_from_int(10)
const big_ten = big.integer_from_int(10)
Line 588: Line 588:
println('period is ${res[2]}\n')
println('period is ${res[2]}\n')
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 595: Line 595:
=={{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>import "/big" for BigInt
<syntaxhighlight lang="ecmascript">import "/big" for BigInt


var divide = Fn.new { |m, n|
var divide = Fn.new { |m, n|
Line 649: Line 649:
System.print("Repetend is '%(res[1])'")
System.print("Repetend is '%(res[1])'")
System.print("Period is %(res[2])\n")
System.print("Period is %(res[2])\n")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}