75 lines
2 KiB
Text
75 lines
2 KiB
Text
'METHOD -- Use the Pascal triangle to retrieve the coefficients
|
|
'UPPER LIMIT OF FREEBASIC ULONGINT GETS PRIMES UP TO 70
|
|
Sub string_split(s_in As String,char As String,result() As String)
|
|
Dim As String s=s_in,var1,var2
|
|
Dim As Integer n,pst
|
|
#macro split(stri,char,var1,var2)
|
|
pst=Instr(stri,char)
|
|
var1="":var2=""
|
|
If pst<>0 Then
|
|
var1=Mid(stri,1,pst-1)
|
|
var2=Mid(stri,pst+1)
|
|
Else
|
|
var1=stri
|
|
End If
|
|
Redim Preserve result(1 To 1+n-((Len(var1)>0)+(Len(var2)>0)))
|
|
result(n+1)=var1
|
|
#endmacro
|
|
Do
|
|
split(s,char,var1,var2):n=n+1:s=var2
|
|
Loop Until var2=""
|
|
Redim Preserve result(1 To Ubound(result)-1)
|
|
End Sub
|
|
|
|
'Get Pascal triangle components
|
|
Function pasc(n As Integer,flag As Integer=0) As String
|
|
n+=1
|
|
Dim As Ulongint V(n):V(1)=1ul
|
|
Dim As String s,sign
|
|
For r As Integer= 2 To n
|
|
s=""
|
|
For i As Integer = r To 1 Step -1
|
|
V(i) += V(i-1)
|
|
If i Mod 2=1 Then sign="" Else sign="-"
|
|
s+=sign+Str(V(i))+","
|
|
Next i
|
|
Next r
|
|
If flag Then 'formatted output
|
|
Dim As String i,i2,i3,g
|
|
Redim As String a(0)
|
|
string_split(s,",",a())
|
|
For n1 As Integer=1 To Ubound(a)
|
|
If Left(a(n1),1)="-" Then sign="" Else sign="+"
|
|
If n1=Ubound(a) Then i2="" Else i2=a(n1)
|
|
If n1=2 Then i3="x" Else i3="x^"+Str(n1-1)
|
|
If n1=1 Then i="":sign=" " Else i=i3
|
|
g+=sign+i2+i+" "
|
|
Next n1
|
|
g="(x-1)^"+Str(n-1)+" = "+g
|
|
Return g
|
|
End If
|
|
Return s
|
|
End Function
|
|
|
|
Function isprime(num As Integer) As Integer
|
|
Redim As String a(0)
|
|
string_split(pasc(num),",",a())
|
|
For n As Integer=Lbound(a)+1 To Ubound(a)-1
|
|
If (Valulng(Ltrim(a(n),"-"))) Mod num<>0 Then Return 0
|
|
Next n
|
|
Return -1
|
|
End Function
|
|
'====================================
|
|
'Formatted output
|
|
For n As Integer=1 To 9
|
|
Print pasc(n,1)
|
|
Next n
|
|
|
|
Print
|
|
'Limit of Freebasic Ulongint sets about 70 max
|
|
Print "Primes up to 70:"
|
|
For n As Integer=2 To 70
|
|
If isprime(n) Then Print n;
|
|
Next n
|
|
|
|
Sleep
|