Arithmetic derivative: Difference between revisions

m
syntax highlighting fixup automation
(Initial Haskell version.)
m (syntax highlighting fixup automation)
Line 39:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang=cpp>#include <iomanip>
#include <iostream>
 
Line 89:
<< ") / 7 = " << arithmetic_derivative(p) / 7 << '\n';
}
}</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang=go>package main
 
import (
Line 185:
fmt.Printf("D(10^%-2d) / 7 = %.0f\n", m, D(pow)/7)
}
}</langsyntaxhighlight>
 
{{out}}
Line 233:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>import Control.Monad (forM_)
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</langsyntaxhighlight>
{{out}}
<pre>
Line 313:
=={{header|J}}==
Implementation:
<langsyntaxhighlight lang=J>D=: {{ +/y%q:1>.|y }}"0</langsyntaxhighlight>
 
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:
 
<langsyntaxhighlight lang=J> D _99+i.20 10
_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</langsyntaxhighlight>
 
Also, it seems like it's worth verifying that order of evaluation does not create an ambiguity for the value of D:
 
<langsyntaxhighlight lang=J> 15 10 6 + 2 3 5 * D 15 10 6
31 31 31</langsyntaxhighlight>
 
Stretch task:
 
<langsyntaxhighlight lang=j> (D 10x^1+i.4 5)%7
1 20 300 4000 50000
600000 7000000 80000000 900000000 10000000000
110000000000 1200000000000 13000000000000 140000000000000 1500000000000000
16000000000000000 170000000000000000 1800000000000000000 19000000000000000000 200000000000000000000</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang=ruby>using Primes
 
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
</langsyntaxhighlight>{{out}}
<pre>
-75 -77 -1 -272 -24 -49 -34 -96 -20 -123
Line 413:
{{trans|J}}
{{libheader|ntheory}}
<langsyntaxhighlight lang=perl>use v5.36;
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>
</lang>
{{out}}
<pre> -75 -77 -1 -272 -24 -49 -34 -96 -20 -123
Line 475:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 563:
 
=={{header|Python}}==
<langsyntaxhighlight lang=python>from sympy.ntheory import factorint
 
def D(n):
Line 583:
print('(D for 10**{}) divided by 7 is {}'.format(m, D(10 ** m) // 7))
 
</langsyntaxhighlight>{{out}}
<pre>
-75 -77 -1 -272 -24 -49 -34 -96 -20 -123
Line 629:
 
=={{header|Raku}}==
<langsyntaxhighlight lang=perl6>use Prime::Factor;
 
multi D (0) { 0 }
Line 642:
put '';
 
put join "\n", (1..20).map: { sprintf "D(10**%-2d) / 7 == %d", $_, D(10**$_) / 7 }</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang=ecmascript>import "./big" for BigInt
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)
}</langsyntaxhighlight>
 
{{out}}
10,333

edits