Integer long division: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 17: | Line 17: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|GMP}} |
{{libheader|GMP}} |
||
< |
<syntaxhighlight lang="cpp">#include <gmpxx.h> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 72: | Line 72: | ||
std::cout << '\n'; |
std::cout << '\n'; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 91: | Line 91: | ||
=={{header|Common Lisp}}== |
=={{header|Common 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: |
||
< |
<syntaxhighlight lang="jq"># "\u0305" |
||
def overline: explode | map(., 773) | implode;</ |
def overline: explode | map(., 773) | implode;</syntaxhighlight> |
||
< |
<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</ |
task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 239: | Line 239: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<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() |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Line 289: | Line 289: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|bignum}} |
{{libheader|bignum}} |
||
< |
<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"</ |
echo &"Period is {period}\n"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 392: | Line 392: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<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></ |
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. |
||
<!--< |
<!--<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> |
||
<!--</ |
<!--</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 |
<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 |
||
}</ |
}</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}} |
||
< |
<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') |
||
} |
} |
||
}</ |
}</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]. |
||
< |
<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") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |