Arithmetic derivative: Difference between revisions
m
syntax highlighting fixup automation
(Initial Haskell version.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 39:
=={{header|C++}}==
{{libheader|Boost}}
<
#include <iostream>
Line 89:
<< ") / 7 = " << arithmetic_derivative(p) / 7 << '\n';
}
}</
{{out}}
Line 140:
Using ''float64'' (finessed a little) to avoid the unpleasantness of ''math/big'' for the stretch goal.
Assumes that ''int'' type is 64 bit.
<
import (
Line 185:
fmt.Printf("D(10^%-2d) / 7 = %.0f\n", m, D(pow)/7)
}
}</
{{out}}
Line 233:
=={{header|Haskell}}==
<
import Data.List (intercalate)
import Data.List.Split (chunksOf)
Line 264:
let q = 7
n = arithderiv (10^i) `quot` q
in printf "D(10^%-2d) / %d = %d\n" i q n</
{{out}}
<pre>
Line 313:
=={{header|J}}==
Implementation:
<
In other words: find the sum of the argument divided by each of the sequence of prime factors of its absolute value (with a special case for zero -- we use the maximum of either 1 or that absolute value when finding the sequence of prime factors).
Line 319:
Task example:
<
_75 _77 _1 _272 _24 _49 _34 _96 _20 _123
_1 _140 _32 _45 _22 _124 _1 _43 _108 _176
Line 339:
1 156 1 39 55 80 18 71 1 176
108 43 1 124 22 45 32 140 1 123
20 96 34 49 24 272 1 77 75 140</
Also, it seems like it's worth verifying that order of evaluation does not create an ambiguity for the value of D:
<
31 31 31</
Stretch task:
<
1 20 300 4000 50000
600000 7000000 80000000 900000000 10000000000
110000000000 1200000000000 13000000000000 140000000000000 1500000000000000
16000000000000000 170000000000000000 1800000000000000000 19000000000000000000 200000000000000000000</
=={{header|Julia}}==
<
D(n) = n < 0 ? -D(-n) : n < 2 ? zero(n) : isprime(n) ? one(n) : typeof(n)(sum(e * n ÷ p for (p, e) in eachfactor(n)))
Line 365:
println("D for 10^", rpad(m, 3), "divided by 7 is ", D(Int128(10)^m) ÷ 7)
end
</
<pre>
-75 -77 -1 -272 -24 -49 -34 -96 -20 -123
Line 413:
{{trans|J}}
{{libheader|ntheory}}
<
use bigint;
no warnings 'uninitialized';
Line 430:
say table 10, map { D $_ } -99 .. 100;
say join "\n", map { sprintf('D(10**%-2d) / 7 == ', $_) . D(10**$_) / 7 } 1 .. 20;
</syntaxhighlight>
{{out}}
<pre> -75 -77 -1 -272 -24 -49 -34 -96 -20 -123
Line 475:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 516:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D(10^%d)/7 = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 563:
=={{header|Python}}==
<
def D(n):
Line 583:
print('(D for 10**{}) divided by 7 is {}'.format(m, D(10 ** m) // 7))
</
<pre>
-75 -77 -1 -272 -24 -49 -34 -96 -20 -123
Line 629:
=={{header|Raku}}==
<
multi D (0) { 0 }
Line 642:
put '';
put join "\n", (1..20).map: { sprintf "D(10**%-2d) / 7 == %d", $_, D(10**$_) / 7 }</
{{out}}
<pre> -75 -77 -1 -272 -24 -49 -34 -96 -20 -123
Line 690:
{{libheader|Wren-fmt}}
As integer arithmetic in Wren is inaccurate above 2^53 we need to use BigInt here.
<
import "./fmt" for Fmt
Line 710:
for (m in 1..20) {
Fmt.print("D(10^$-2d) / 7 = $i", m, D.call(BigInt.ten.pow(m))/7)
}</
{{out}}
|