简体   繁体   中英

Ada95: Recursive function for Bernoulli numbers

I'm trying to code a function that calculates the N'th value of the positive Bernouille numbers. I want to do it with this recursive formula:

What I've tried so far:

   function Get_B(N : in Integer) return Float is

     X,Bn,Bk:Float;
   begin
     if N = 0 then
       return 1.0;
     else
       Bn:=0.0;
       for K in  0..(N-1) loop
        Bk:=Get_B(K);
        X:=1.0-Float(F(N))/(Float(F(K))*Float(F(N-K))) * 
           Bk/(Float(N)-Float(K)+1.0);
        Bn:=Bn+X;
     end loop;
     return Bn;
      end if;

   end Get_B;

where F is a factorial function (that is, F(N) means N!). I think there's something wrong with the loop, but I don't know what it is.

I can't post images but here's a link to the equation (the bottom one): https://wikimedia.org/api/rest_v1/media/math/render/svg/2571d0a7024741c0a0ad0eb32cc9333f6024c528

The 1.0- should be outside the loop:

function Get_B(N : in Integer) 
               return Float is

 X,Bn,Bk:Float;
 begin
   if N = 0 then
     return 1.0;
   else
     Bn:=0.0;
     for K in  0..(N-1) loop
      Bk:=Get_B(K);
      X:=Float(F(N))/(Float(F(K))*Float(F(N-K))) * 
         Bk/(Float(N)-Float(K)+1.0);
      Bn:=Bn+X;
    end loop;
    return 1.0 - Bn;
  end if;

 end Get_B;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM