RosettaCodeData/Task/Sisyphus-sequence/FreeBASIC/sisyphus-sequence.basic

84 lines
2.0 KiB
Plaintext

'#include "isprime.bas"
Function getNthPrime(n As Integer) As Longint
If n <= 0 Then Return 0
Dim As Integer cnt = 0
Dim As Longint num = 1
While cnt < n
num += 1
If isPrime(num) Then cnt += 1
Wend
Return num
End Function
Function findMax(arr() As Integer) As Integer
Dim As Integer maxVal = arr(0)
For i As Integer = 1 To Ubound(arr)
If arr(i) > maxVal Then maxVal = arr(i)
Next
Return maxVal
End Function
' Main program
Const As Longint limit = 1e6
Dim As Double sisyphus(100)
Dim As Integer under250(250)
Dim As Integer i, m, np = 0
Dim As Longint specific = 1000
Dim As Longint cnt = 1
Dim As Double nextVal = 1
sisyphus(0) = 1
under250(1) = 1
Do
If (nextVal Mod 2) = 0 Then
nextVal /= 2
Else
np += 1
nextVal += getNthPrime(np)
End If
If nextVal <= 250 Then under250(nextVal) += 1
cnt += 1
If cnt <= 100 Then
sisyphus(cnt-1) = nextVal
If cnt = 100 Then
Print "The first 100 members of the Sisyphus sequence are:"
For i = 0 To 99
Print Using "####"; sisyphus(i);
If (i + 1) Mod 10 = 0 Then Print
Next
Print
End If
Elseif cnt = specific Then
Print Using "###,###,###,###"; cnt;
Print "th member is: ";
Print Using "###,###,###,###"; nextVal;
Print " and highest prime needed: ";
Print Using "###,###,###"; getNthPrime(np)
If cnt = limit Then
m = findMax(under250())
Print !"\nNumbers under 250 that do not occur in first "; cnt; " terms:"
For i = 1 To 250
If under250(i) = 0 Then Print i; " ";
Next
Print
Print !"\nNumbers under 250 that occur the most in first "; cnt; " terms:"
For i = 1 To 250
If under250(i) = m Then Print i; " ";
Next
Print " all occur "; m; " times."
Exit Do
End If
specific *= 10
End If
Loop
Sleep