Faulhaber's triangle: Difference between revisions
Content added Content deleted
imported>Maxima enthusiast |
(Added Algol 68) |
||
Line 36: | Line 36: | ||
* [http://www.ww.ingeniousmathstat.org/sites/default/files/Torabi-Dashti-CMJ-2011.pdf Faulhaber's triangle (PDF)] |
* [http://www.ww.ingeniousmathstat.org/sites/default/files/Torabi-Dashti-CMJ-2011.pdf Faulhaber's triangle (PDF)] |
||
<br> |
<br> |
||
=={{header|ALGOL 68}}== |
|||
Usingcode from the Algol 68 samples for the [[Arithmetic/Rational]] and [[Bernoulli numbers]] tasks and the Algol W sample for the [[Evaluate binomial coefficients]] task. The code to calculate the coefficients for a row of the triangle is based on the C sample.<br> |
|||
Note that in the Bernoulli numbers task, the Algol 68 sample returns -1/2 for B(1) - this is modified here so B(1) is 1/2. |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # show some rows of Faulhaber's triangle # |
|||
# utility operators # |
|||
OP LENGTH = ( STRING a )INT: ( UPB a - LWB a ) + 1; |
|||
PRIO PAD = 9; |
|||
OP PAD = ( INT width, STRING v )STRING: # left blank pad v to width # |
|||
IF LENGTH v >= width THEN v ELSE ( " " * ( width - LENGTH v ) ) + v FI; |
|||
# Code from the Arithmetic/Rational task # |
|||
MODE FRAC = STRUCT( INT num #erator#, den #ominator#); |
|||
PROC gcd = (INT a, b) INT: # greatest common divisor # |
|||
(a = 0 | b |: b = 0 | a |: ABS a > ABS b | gcd(b, a MOD b) | gcd(a, b MOD a)); |
|||
PROC lcm = (INT a, b)INT: # least common multiple # |
|||
a OVER gcd(a, b) * b; |
|||
PRIO // = 9; # higher then the ** operator # |
|||
OP // = (INT num, den)FRAC: ( # initialise and normalise # |
|||
INT common = gcd(num, den); |
|||
IF den < 0 THEN |
|||
( -num OVER common, -den OVER common) |
|||
ELSE |
|||
( num OVER common, den OVER common) |
|||
FI |
|||
); |
|||
OP + = (FRAC a, b)FRAC: ( |
|||
INT common = lcm(den OF a, den OF b); |
|||
FRAC result := ( common OVER den OF a * num OF a + common OVER den OF b * num OF b, common ); |
|||
num OF result//den OF result |
|||
); |
|||
OP - = (FRAC a, b)FRAC: a + -b, |
|||
* = (FRAC a, b)FRAC: ( |
|||
INT num = num OF a * num OF b, |
|||
den = den OF a * den OF b; |
|||
INT common = gcd(num, den); |
|||
(num OVER common) // (den OVER common) |
|||
); |
|||
OP - = (FRAC frac)FRAC: (-num OF frac, den OF frac); |
|||
# end code from the Arithmetic/Rational task # |
|||
# returns a + b # |
|||
OP * = ( INT a, FRAC b )FRAC: ( num OF b * a ) // den OF b; |
|||
# returns the nth Bernoulli number, n must be >= 0 # |
|||
OP BERNOULLI = ( INT n )FRAC: |
|||
IF n < 0 |
|||
THEN # n is out of range # 0 // 1 |
|||
ELSE # n is valid # |
|||
[ 0 : n ]FRAC a; |
|||
FOR m FROM 0 TO n DO |
|||
a[ m ] := 1 // ( m + 1 ); |
|||
FOR j FROM m BY -1 TO 1 DO |
|||
a[ j - 1 ] := j * ( a[ j - 1 ] - a[ j ] ) |
|||
OD |
|||
OD; |
|||
IF n = 1 THEN - a[ 0 ] ELSE a[ 0 ] FI |
|||
FI # BERNOULLI # ; |
|||
# returns n! / k! # |
|||
PROC factorial over factorial = ( INT n, k )INT: |
|||
IF k > n THEN 0 |
|||
ELIF k = n THEN 1 |
|||
ELSE # k < n # |
|||
INT f := 1; |
|||
FOR i FROM k + 1 TO n DO f *:= i OD; |
|||
f |
|||
FI # factorial over Factorial # ; |
|||
# returns n! # |
|||
PROC factorial = ( INT n )INT: |
|||
BEGIN |
|||
INT f := 1; |
|||
FOR i FROM 2 TO n DO f *:= i OD; |
|||
f |
|||
END # factorial # ; |
|||
# returns the binomial coefficient of (n k) # |
|||
PROC binomial coefficient = ( INT n, k )INT: |
|||
IF n - k > k |
|||
THEN factorial over factorial( n, n - k ) OVER factorial( k ) |
|||
ELSE factorial over factorial( n, k ) OVER factorial( n - k ) |
|||
FI # binomial coefficient # ; |
|||
# returns a string representation of a # |
|||
OP TOSTRING = ( FRAC a )STRING: |
|||
whole( num OF a, 0 ) + IF den OF a = 1 THEN "" ELSE "/" + whole( den OF a, 0 ) FI; |
|||
# returns the pth row of Faulhaber's triangle - based on the C sample # |
|||
OP FAULHABER = ( INT p )[]FRAC: |
|||
BEGIN |
|||
FRAC q = 1 // ( p + 1 ); |
|||
INT sign := -1; |
|||
[ 0 : p ]FRAC coeffs; |
|||
FOR j FROM 0 TO p DO |
|||
sign := 0 - sign; |
|||
coeffs[ p - j ] := q |
|||
* ( sign // 1 ) |
|||
* ( binomial coefficient( p + 1, j ) // 1 ) |
|||
* BERNOULLI j |
|||
OD; |
|||
coeffs |
|||
END # faulhaber # ; |
|||
FOR i FROM 0 TO 9 DO |
|||
[]FRAC frow = FAULHABER i; |
|||
FOR j FROM LWB frow TO UPB frow DO |
|||
print( ( " ", 6 PAD TOSTRING frow[ j ] ) ) |
|||
OD; |
|||
print( ( newline ) ) |
|||
OD |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 |
|||
1/2 1/2 |
|||
1/6 1/2 1/3 |
|||
0 1/4 1/2 1/4 |
|||
-1/30 0 1/3 1/2 1/5 |
|||
0 -1/12 0 5/12 1/2 1/6 |
|||
1/42 0 -1/6 0 1/2 1/2 1/7 |
|||
0 1/12 0 -7/24 0 7/12 1/2 1/8 |
|||
-1/30 0 2/9 0 -7/15 0 2/3 1/2 1/9 |
|||
0 -3/20 0 1/2 0 -7/10 0 3/4 1/2 1/10 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |