RosettaCodeData/Task/Bernoulli-numbers/FreeBASIC/bernoulli-numbers.basic

55 lines
1.1 KiB
Plaintext

' version 08-10-2016
' compile with: fbc -s console
' uses gmp
#Include Once "gmp.bi"
#Define max 60
Dim As Long n
Dim As ZString Ptr gmp_str :gmp_str = Allocate(1000) ' 1000 char
Dim Shared As Mpq_ptr tmp, big_j
tmp = Allocate(Len(__mpq_struct)) :Mpq_init(tmp)
big_j = Allocate(Len(__mpq_struct)) :Mpq_init(big_j)
Dim Shared As Mpq_ptr a(max), b(max)
For n = 0 To max
A(n) = Allocate(Len(__mpq_struct)) :Mpq_init(A(n))
B(n) = Allocate(Len(__mpq_struct)) :Mpq_init(B(n))
Next
Function Bernoulli(n As Integer) As Mpq_ptr
Dim As Long m, j
For m = 0 To n
Mpq_set_ui(A(m), 1, m + 1)
For j = m To 1 Step - 1
Mpq_sub(tmp, A(j - 1), A(j))
Mpq_set_ui(big_j, j, 1) 'big_j = j
Mpq_mul(A(j - 1), big_j, tmp)
Next
Next
Return A(0)
End Function
' ------=< MAIN >=------
For n = 0 To max
Mpq_set(B(n), Bernoulli(n))
Mpq_get_str(gmp_str, 10, B(n))
If *gmp_str <> "0" Then
If *gmp_str = "1" Then *gmp_str = "1/1"
Print Using "B(##) = "; n;
Print Space(45 - InStr(*gmp_str, "/")); *gmp_str
End If
Next
' empty keyboard buffer
While Inkey <> "" :Wend
Print :Print "hit any key to end program"
Sleep
End