In mathematics,   Faulhaber's formula,   named after Johann Faulhaber,   expresses the sum of the p-th powers of the first n positive integers as a (p + 1)th-degree polynomial function of n,   the coefficients involving Bernoulli numbers.



Task

Generate the first 10 closed-form expressions, starting with p = 0.



Related tasks

   Bernoulli numbers.
   evaluate binomial coefficients.


See also

   The Wikipedia entry:   Faulhaber's formula.
   The Wikipedia entry:   Bernoulli numbers.
   The Wikipedia entry:   binomial coefficients.


 
\\ 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
sreplace(str,ssrch,srepl)={
  my(sn=#str,ssn=#ssrch,srn=#srepl,sres="",Vi,Vs,Vz,vin,vin1,vi,L=List(),tok,zi,js=1);
  if(sn==0,return("")); if(ssn==0||ssn>sn,return(str));
  \\ Vi - found ssrch indexes
  Vi=sfindalls(str,ssrch); vin=#Vi;
  if(vin==0,return(str));
  vin1=vin+1; Vi=Vec(Vi,vin1); Vi[vin1]=sn+1;
  for(i=1,vin1, vi=Vi[i];
  for(j=js,sn, \\print("ij:",i,"/",j,": ",sres);
    if(j!=vi, sres=concat(sres,ssubstr(str,j,1)),
              sres=concat(sres,srepl); js=j+ssn; break) 
  ); \\fend j
  ); \\fend i
  return(sres);
}
B(n)=(bernfrac(n));
Comb(n,k)={my(r=0); if(k<=n, r=n!/(n-k)!/k!); return(r)};
Faulhaber2(p)={
  my(s="",s1="",s2="",c1=0,c2=0);
  for(j=0,p, c1=(-1)^j*Comb(p+1,j)*B(j); c2=(p+1-j);
    s2="*n";
    if(c1==0, next);
    if(c2==1, s1="", s1=Str("^",c2));
    s=Str(s,c1,s2,s1,"+") );
  s=ssubstr(s,1,#s-1); s=sreplace(s,"1*n","n"); s=sreplace(s,"+-","-");
  if(p==0, s="n", s=Str("(",s,")/",p+1)); print(p,": ",s);
}
{\\ Testing:
for(i=0,9, Faulhaber2(i))}
 