RosettaCodeData/Task/Arithmetic-derivative/FreeBASIC/arithmetic-derivative.basic

47 lines
955 B
Plaintext

Function aDerivative(Byval n As Longint) As Longint
If n < 0 Then Return -aDerivative(-n)
If n = 0 Or n = 1 Then Return 0
If n = 2 Then Return 1
Dim As Longint q, d = 2
Dim As Longint result = 1
While d * d <= n
If n Mod d = 0 Then
q = n \ d
result = q * aDerivative(d) + d * aDerivative(q)
Exit While
End If
d += 1
Wend
Return result
End Function
'Main program
Print "Arithmetic derivatives for -99 through 100:"
Dim As Integer col, n
col = 0
For n = -99 To 100
col += 1
Print Using "####"; aDerivative(n);
If col = 10 Then
Print
col = 0
Else
Print " ";
End If
Next
'Stretch task
Print !"\n\nPowers of 10 derivatives divided by 7:"
Dim As Double m = 1
For n = 1 To 18 ' LongInt limit in FreeBASIC
m *= 10
Dim As Longint a = aDerivative(Clngint(m))
Print Using "D(10^&) / 7 = &"; n; a \ 7
Next
Sleep