Faulhaber's formula: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: added syntax colouring, marked p2js compatible |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 21: | Line 21: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Modula-2}} |
{{trans|Modula-2}} |
||
< |
<syntaxhighlight lang="c">#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 189: | Line 189: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 204: | Line 204: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
namespace FaulhabersFormula { |
namespace FaulhabersFormula { |
||
Line 383: | Line 383: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 399: | Line 399: | ||
{{trans|D}} |
{{trans|D}} |
||
Uses C++17 |
Uses C++17 |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <numeric> |
#include <numeric> |
||
#include <sstream> |
#include <sstream> |
||
Line 568: | Line 568: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 583: | Line 583: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="d">import std.algorithm : fold; |
||
import std.exception : enforce; |
import std.exception : enforce; |
||
import std.format : formattedWrite; |
import std.format : formattedWrite; |
||
Line 732: | Line 732: | ||
faulhaber(i); |
faulhaber(i); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 746: | Line 746: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(lib 'math) ;; for bernoulli numbers |
(lib 'math) ;; for bernoulli numbers |
||
(string-delimiter "") |
(string-delimiter "") |
||
Line 764: | Line 764: | ||
(define (Faulcomp n p) |
(define (Faulcomp n p) |
||
(printf "Σ(1..%d) n^%d = %d" n p (/ (poly n (Faulhaber p)) (1+ p) ))) |
(printf "Σ(1..%d) n^%d = %d" n p (/ (poly n (Faulhaber p)) (1+ p) ))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 794: | Line 794: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: formatting kernel math math.combinatorics math.extras |
||
math.functions regexp sequences ; |
math.functions regexp sequences ; |
||
Line 812: | Line 812: | ||
: poly>str ( seq -- str ) (poly>str) clean-up ; |
: poly>str ( seq -- str ) (poly>str) clean-up ; |
||
10 [ dup faulhaber poly>str "%d: %s\n" printf ] each-integer</ |
10 [ dup faulhaber poly>str "%d: %s\n" printf ] each-integer</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 838: | Line 838: | ||
Straightforward implementation using GAP polynomials, and two different formulas: one based on Stirling numbers of the second kind (sum1, see Python implementation below in this page), and the usual Faulhaber formula (sum2). No optimization is made (one could compute Stirling numbers row by row, or the product in sum1 may be kept from one call to the other). Notice the Bernoulli term in the first formula is here only to correct the value of sum1(0), which is off by one because sum1 computes sums from 0 to n. |
Straightforward implementation using GAP polynomials, and two different formulas: one based on Stirling numbers of the second kind (sum1, see Python implementation below in this page), and the usual Faulhaber formula (sum2). No optimization is made (one could compute Stirling numbers row by row, or the product in sum1 may be kept from one call to the other). Notice the Bernoulli term in the first formula is here only to correct the value of sum1(0), which is off by one because sum1 computes sums from 0 to n. |
||
< |
<syntaxhighlight lang="gap">n := X(Rationals, "n"); |
||
sum1 := p -> Sum([0 .. p], k -> Stirling2(p, k) * Product([0 .. k], j -> n + 1 - j) / (k + 1)) + 2 * Bernoulli(2 * p + 1); |
sum1 := p -> Sum([0 .. p], k -> Stirling2(p, k) * Product([0 .. k], j -> n + 1 - j) / (k + 1)) + 2 * Bernoulli(2 * p + 1); |
||
sum2 := p -> Sum([0 .. p], j -> (-1)^j * Binomial(p + 1, j) * Bernoulli(j) * n^(p + 1 - j)) / (p + 1); |
sum2 := p -> Sum([0 .. p], j -> (-1)^j * Binomial(p + 1, j) * Bernoulli(j) * n^(p + 1 - j)) / (p + 1); |
||
Line 856: | Line 856: | ||
1/8*n^8+1/2*n^7+7/12*n^6-7/24*n^4+1/12*n^2 |
1/8*n^8+1/2*n^7+7/12*n^6-7/24*n^4+1/12*n^2 |
||
1/9*n^9+1/2*n^8+2/3*n^7-7/15*n^5+2/9*n^3-1/30*n |
1/9*n^9+1/2*n^8+2/3*n^7-7/15*n^5+2/9*n^3-1/30*n |
||
1/10*n^10+1/2*n^9+3/4*n^8-7/10*n^6+1/2*n^4-3/20*n^2</ |
1/10*n^10+1/2*n^9+3/4*n^8-7/10*n^6+1/2*n^4-3/20*n^2</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 918: | Line 918: | ||
fmt.Println() |
fmt.Println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 935: | Line 935: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="groovy">import java.util.stream.IntStream |
||
class FaulhabersFormula { |
class FaulhabersFormula { |
||
Line 1,076: | Line 1,076: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 1,091: | Line 1,091: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
====Bernouilli polynomials==== |
====Bernouilli polynomials==== |
||
< |
<syntaxhighlight lang="haskell">import Data.Ratio ((%), numerator, denominator) |
||
import Data.List (intercalate, transpose) |
import Data.List (intercalate, transpose) |
||
import Data.Bifunctor (bimap) |
import Data.Bifunctor (bimap) |
||
Line 1,193: | Line 1,193: | ||
unsignedLength xs = |
unsignedLength xs = |
||
let l = length xs |
let l = length xs |
||
in bool (bool l (l - 1) ('-' == head xs)) 0 (0 == l)</ |
in bool (bool l (l - 1) ('-' == head xs)) 0 (0 == l)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>0 -> n |
<pre>0 -> n |
||
Line 1,210: | Line 1,210: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">Bsecond=:verb define"0 |
||
+/,(<:*(_1^[)*!*(y^~1+[)%1+])"0/~i.1x+y |
+/,(<:*(_1^[)*!*(y^~1+[)%1+])"0/~i.1x+y |
||
) |
) |
||
Line 1,218: | Line 1,218: | ||
Faul=:adverb define |
Faul=:adverb define |
||
(0,|.(%m+1x) * (_1x&^ * !&(m+1) * Bfirst) i.1+m)&p. |
(0,|.(%m+1x) * (_1x&^ * !&(m+1) * Bfirst) i.1+m)&p. |
||
)</ |
)</syntaxhighlight> |
||
Task example: |
Task example: |
||
< |
<syntaxhighlight lang="j"> 0 Faul |
||
0 1x&p. |
0 1x&p. |
||
1 Faul |
1 Faul |
||
Line 1,241: | Line 1,241: | ||
0 _1r30 0 2r9 0 _7r15 0 2r3 1r2 1r9&p. |
0 _1r30 0 2r9 0 _7r15 0 2r3 1r2 1r9&p. |
||
9 Faul |
9 Faul |
||
0 0 _3r20 0 1r2 0 _7r10 0 3r4 1r2 1r10&p.</ |
0 0 _3r20 0 1r2 0 _7r10 0 3r4 1r2 1r10&p.</syntaxhighlight> |
||
Double checking our work: |
Double checking our work: |
||
< |
<syntaxhighlight lang="j"> Fcheck=: dyad def'+/(1+i.y)^x'"0 |
||
9 Faul i.5 |
9 Faul i.5 |
||
0 1 513 20196 282340 |
0 1 513 20196 282340 |
||
Line 1,253: | Line 1,253: | ||
0 1 5 14 30 |
0 1 5 14 30 |
||
2 Fcheck i.5 |
2 Fcheck i.5 |
||
0 1 5 14 30</ |
0 1 5 14 30</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
import java.util.stream.IntStream; |
import java.util.stream.IntStream; |
||
Line 1,399: | Line 1,399: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 1,416: | Line 1,416: | ||
'''Module''': |
'''Module''': |
||
< |
<syntaxhighlight lang="julia">module Faulhaber |
||
function bernoulli(n::Integer) |
function bernoulli(n::Integer) |
||
Line 1,456: | Line 1,456: | ||
end |
end |
||
end # module Faulhaber</ |
end # module Faulhaber</syntaxhighlight> |
||
'''Main''': |
'''Main''': |
||
<lang |
<syntaxhighlight lang="julia">Faulhaber.formula.(1:10)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,475: | Line 1,475: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
As Kotlin doesn't have support for rational numbers built in, a cut-down version of the Frac class in the Arithmetic/Rational task has been used in order to express the polynomial coefficients as fractions. |
As Kotlin doesn't have support for rational numbers built in, a cut-down version of the Frac class in the Arithmetic/Rational task has been used in order to express the polynomial coefficients as fractions. |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun gcd(a: Long, b: Long): Long = if (b == 0L) a else gcd(b, a % b) |
fun gcd(a: Long, b: Long): Long = if (b == 0L) a else gcd(b, a % b) |
||
Line 1,593: | Line 1,593: | ||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
for (i in 0..9) faulhaber(i) |
for (i in 0..9) faulhaber(i) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,611: | Line 1,611: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="lua">function binomial(n,k) |
||
if n<0 or k<0 or n<k then return -1 end |
if n<0 or k<0 or n<k then return -1 end |
||
if n==0 or k==0 then return 1 end |
if n==0 or k==0 then return 1 end |
||
Line 1,756: | Line 1,756: | ||
for i=0,9 do |
for i=0,9 do |
||
faulhaber(i) |
faulhaber(i) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 1,770: | Line 1,770: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[Faulhaber] |
||
Faulhaber[n_, 0] := n |
Faulhaber[n_, 0] := n |
||
Faulhaber[n_, p_] := n^(p + 1)/(p + 1) + 1/2 n^p + Sum[BernoulliB[k]/k! p!/(p - k + 1)! n^(p - k + 1), {k, 2, p}] |
Faulhaber[n_, p_] := n^(p + 1)/(p + 1) + 1/2 n^p + Sum[BernoulliB[k]/k! p!/(p - k + 1)! n^(p - k + 1), {k, 2, p}] |
||
Table[{p, Faulhaber[n, p]}, {p, 0, 9}] // Grid</ |
Table[{p, Faulhaber[n, p]}, {p, 0, 9}] // Grid</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 n |
<pre>0 n |
||
Line 1,787: | Line 1,787: | ||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">sum1(p):=sum(stirling2(p,k)*pochhammer(n-k+1,k+1)/(k+1),k,0,p)$ |
||
sum2(p):=sum((-1)^j*binomial(p+1,j)*bern(j)*n^(p-j+1),j,0,p)/(p+1)$ |
sum2(p):=sum((-1)^j*binomial(p+1,j)*bern(j)*n^(p-j+1),j,0,p)/(p+1)$ |
||
Line 1,793: | Line 1,793: | ||
[0,0,0,0,0,0,0,0,0,0] |
[0,0,0,0,0,0,0,0,0,0] |
||
for p from 0 thru 9 do print(expand(sum2(p)));</ |
for p from 0 thru 9 do print(expand(sum2(p)));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,810: | Line 1,810: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="modula2">MODULE Faulhaber; |
||
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE; |
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
Line 1,991: | Line 1,991: | ||
END; |
END; |
||
ReadChar |
ReadChar |
||
END Faulhaber.</ |
END Faulhaber.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 2,006: | Line 2,006: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="nim">import math, rationals |
||
type |
type |
||
Line 2,079: | Line 2,079: | ||
for n in 0..9: |
for n in 0..9: |
||
echo n, ": ", faulhaber(n)</ |
echo n, ": ", faulhaber(n)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,101: | Line 2,101: | ||
It's not worth using Bernoulli numbers in PARI/GP, because too much cleaning if you are avoiding "dirty" (but correct) result.<br> |
It's not worth using Bernoulli numbers in PARI/GP, because too much cleaning if you are avoiding "dirty" (but correct) result.<br> |
||
Note: Find ssubstr() function here on RC. |
Note: Find ssubstr() function here on RC. |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Using "Faulhaber's" formula based on Bernoulli numbers. aev 2/7/17 |
\\ Using "Faulhaber's" formula based on Bernoulli numbers. aev 2/7/17 |
||
\\ In str string replace all occurrences of the search string ssrch with the replacement string srepl. aev 3/8/16 |
\\ In str string replace all occurrences of the search string ssrch with the replacement string srepl. aev 3/8/16 |
||
Line 2,133: | Line 2,133: | ||
{\\ Testing: |
{\\ Testing: |
||
for(i=0,9, Faulhaber2(i))} |
for(i=0,9, Faulhaber2(i))} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,151: | Line 2,151: | ||
This version is using the sums of pth powers formula from [[wp:Bernoulli_polynomials| Bernoulli polynomials]]. |
This version is using the sums of pth powers formula from [[wp:Bernoulli_polynomials| Bernoulli polynomials]]. |
||
It has small, simple and clear code, and produces instant result. |
It has small, simple and clear code, and produces instant result. |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Using a formula based on Bernoulli polynomials. aev 2/5/17 |
\\ Using a formula based on Bernoulli polynomials. aev 2/5/17 |
||
Faulhaber1(m)={ |
Faulhaber1(m)={ |
||
Line 2,162: | Line 2,162: | ||
{\\ Testing: |
{\\ Testing: |
||
for(i=0,9, Faulhaber1(i))} |
for(i=0,9, Faulhaber1(i))} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,180: | Line 2,180: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use 5.014; |
||
use Math::Algebra::Symbols; |
use Math::Algebra::Symbols; |
||
Line 2,222: | Line 2,222: | ||
foreach my $i (0 .. 9) { |
foreach my $i (0 .. 9) { |
||
say "$i: ", faulhaber_s_formula($i); |
say "$i: ", faulhaber_s_formula($i); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,239: | Line 2,239: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
<!--< |
<!--<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;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">pfrac</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- (0.8.0+)</span> |
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">pfrac</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- (0.8.0+)</span> |
||
Line 2,301: | Line 2,301: | ||
<span style="color: #000000;">faulhaber</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">faulhaber</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,328: | Line 2,328: | ||
< |
<syntaxhighlight lang="python">from fractions import Fraction |
||
def nextu(a): |
def nextu(a): |
||
Line 2,390: | Line 2,390: | ||
for i, p in enumerate(sumpol(10)): |
for i, p in enumerate(sumpol(10)): |
||
print(i, ":", polstr(p))</ |
print(i, ":", polstr(p))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,408: | Line 2,408: | ||
Racket will simplify rational numbers; if this code simplifies the expressions too much for your tastes (e.g. you like <code>1/1 * (n)</code>) then tweak the simplify... clauses to taste. |
Racket will simplify rational numbers; if this code simplifies the expressions too much for your tastes (e.g. you like <code>1/1 * (n)</code>) then tweak the simplify... clauses to taste. |
||
< |
<syntaxhighlight lang="racket">#lang racket/base |
||
(require racket/match |
(require racket/match |
||
Line 2,464: | Line 2,464: | ||
(for ((p (in-range 0 (add1 9)))) |
(for ((p (in-range 0 (add1 9)))) |
||
(printf "f(~a) = ~a~%" p (expression->infix-string (faulhaber p)))) |
(printf "f(~a) = ~a~%" p (expression->infix-string (faulhaber p)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,483: | Line 2,483: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|2018.04.01}} |
{{works with|Rakudo|2018.04.01}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub bernoulli_number($n) { |
||
return 1/2 if $n == 1; |
return 1/2 if $n == 1; |
||
Line 2,521: | Line 2,521: | ||
for 0..9 -> $p { |
for 0..9 -> $p { |
||
say "f($p) = ", faulhaber_s_formula($p); |
say "f($p) = ", faulhaber_s_formula($p); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,538: | Line 2,538: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="ruby">def binomial(n,k) |
||
if n < 0 or k < 0 or n < k then |
if n < 0 or k < 0 or n < k then |
||
return -1 |
return -1 |
||
Line 2,625: | Line 2,625: | ||
end |
end |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 2,640: | Line 2,640: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">import scala.math.Ordering.Implicits.infixOrderingOps |
||
abstract class Frac extends Comparable[Frac] { |
abstract class Frac extends Comparable[Frac] { |
||
Line 2,810: | Line 2,810: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 2,824: | Line 2,824: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">const AnyNum = require('Math::AnyNum') |
||
const Poly = require('Math::Polynomial') |
const Poly = require('Math::Polynomial') |
||
Line 2,846: | Line 2,846: | ||
for p in (^10) { |
for p in (^10) { |
||
printf("%2d: %s\n", p, faulhaber_formula(p)) |
printf("%2d: %s\n", p, faulhaber_formula(p)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,863: | Line 2,863: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Function Gcd(a As Long, b As Long) |
Function Gcd(a As Long, b As Long) |
||
If b = 0 Then |
If b = 0 Then |
||
Line 3,025: | Line 3,025: | ||
Next |
Next |
||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : n |
<pre>0 : n |
||
Line 3,042: | Line 3,042: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-rat}} |
{{libheader|Wren-rat}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/rat" for Rat |
import "/rat" for Rat |
||
Line 3,094: | Line 3,094: | ||
} |
} |
||
for (i in 0..9) faulhaber.call(i)</ |
for (i in 0..9) faulhaber.call(i)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,113: | Line 3,113: | ||
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library |
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library |
||
Uses code from the Bernoulli numbers task (copied here). |
Uses code from the Bernoulli numbers task (copied here). |
||
< |
<syntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"); // libGMP (GNU MP Bignum Library) |
||
fcn faulhaberFormula(p){ //-->(Rational,Rational...) |
fcn faulhaberFormula(p){ //-->(Rational,Rational...) |
||
[p..0,-1].pump(List(),'wrap(k){ B(k)*BN(p+1).binomial(k) }) |
[p..0,-1].pump(List(),'wrap(k){ B(k)*BN(p+1).binomial(k) }) |
||
.apply('*(Rational(1,p+1))) |
.apply('*(Rational(1,p+1))) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">foreach p in (10){ |
||
println("F(%d) --> %s".fmt(p,polyRatString(faulhaberFormula(p)))) |
println("F(%d) --> %s".fmt(p,polyRatString(faulhaberFormula(p)))) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">class Rational{ // Weenie Rational class, can handle BigInts |
||
fcn init(_a,_b){ var a=_a, b=_b; normalize(); } |
fcn init(_a,_b){ var a=_a, b=_b; normalize(); } |
||
fcn toString{ |
fcn toString{ |
||
Line 3,145: | Line 3,145: | ||
} |
} |
||
fcn __opDiv(n){ self(a*n.b,b*n.a) } // Rat / Rat |
fcn __opDiv(n){ self(a*n.b,b*n.a) } // Rat / Rat |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">fcn B(N){ // calculate Bernoulli(n) --> Rational |
||
var A=List.createLong(100,0); // aka static aka not thread safe |
var A=List.createLong(100,0); // aka static aka not thread safe |
||
foreach m in (N+1){ |
foreach m in (N+1){ |
||
Line 3,162: | Line 3,162: | ||
if(str[0]=="+") str[1,*]; // leave leading space |
if(str[0]=="+") str[1,*]; // leave leading space |
||
else String("-",str[2,*]); |
else String("-",str[2,*]); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |