Anonymous user
Bernoulli numbers: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, simplified some statements, optimized some functions.
m (Phix/mpfr) |
m (→{{header|REXX}}: added/changed comments and whitespace, simplified some statements, optimized some functions.) |
||
Line 3,284:
<lang rexx>/*REXX program calculates N number of Bernoulli numbers expressed as fractions. */
parse arg N .; if N=='' | N=="," then N= 60 /*Not specified? Then use the default.*/
say 'B(n)' center("Bernoulli number expressed as a fraction", max(78-w, Nw)) /*title.*/
say copies('─',w) copies("─", max(78-w,Nw+2*w)) /*display 2nd line of title, separators*/
!.=
b= bern(#); if b==0 then iterate
indent= max(0, nW - pos('/', b) )
say right(#, w) left('', indent) b
end /*#*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 3,298:
if x==1 then return '-1/2' /* " " " " " one. */
if x//2 then return 0 /* " " " " " odds > 1.*/
do j=2 to x by 2; jp= j+1
sn= 1 - j /*define the numerator. */
sd= 2 /* " " denominator. */
Line 3,304:
parse var @.k bn '/' ad /*get a previously calculated fraction.*/
an= comb(jp, k) * bn /*use COMBination for the next term. */
$
sn= $
an= $
sn= sn + an /* " " current " */
end /*k*/ /* [↑] calculate the SN/SD sequence.*/
sn= -sn /*
sd= sd * jp /*calculate
if sn\==1 then do; _= GCD(sn, sd) /*get the Greatest Common Denominator.*/
sn= sn%_; sd= sd%_
end /* [↑] done with the reduction(s). */
@.j= sn'/'sd /*save the result for the next round. */
end
return sn'/'sd
/*──────────────────────────────────────────────────────────────────────────────────────*/
comb: procedure expose !.; parse arg x,y;
if !.c.x.y
if x-y < y
z= perm(x, y); do j=2 for y-1; z= z % j
end /*j*/
!.c.x.y= z; return z /*assign memoization & return.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
GCD: procedure; parse arg x,y; x= abs(x)
do until y==0; parse value x//y y with y x; end;
/*──────────────────────────────────────────────────────────────────────────────────────*/
LCM: procedure; parse arg x,y /*
do until y==0; parse value x//y y
end /*until*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
perm: procedure expose !.; parse arg x,y; if !.p.x.y \== 0 then return !.p.x.y
z= 1; do j=x-y+1 to x; z= z*j; end; !.p.x.y= z; return z
</lang>
{{out|output|text= when using the default input:}}
<pre>
|