Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prime Number Generator
#1
I'm sure many of you have already made this before. But I don't believe I ever have, and I've programmed since the mid-80's. I also got zero help from anyone or any A.I. this time. 
It asks you how many numbers you wish to calculate in sequence from 1 to your number. The limit is 100000. While it is doing it, it places the number on PrimeNumbers.txt file in the same directory as this program. So it's best to make a new directory before you start it. When it is finished it automatically opens your Notepad to display all the numbers. At the end of the list of numbers it will tell you how many numbers are on the list. Therefore, this will only work on a Windows computer I believe since it uses SHELL and Notepad.  

If you decide to change the limit of the amount of prime numbers it makes, just be aware that computers can heat up and break if over-used. I set the limit at 100000 numbers to calculate and the primes placed on the PrimeNumbers.txt file.  I hold no responsibility of any over-use in any way, as usual.  

Code: (Select All)

'Prime Number Calculator by SierraKen
'December 26, 2024
'I got no help from anyone or any A.I. 
'
'If you change any of this code, please do not change the limit to more than 100000.
'I'm sure it can do much more but I hold no responsibility if this program overworks your computer in any way.

_Title "Prime Number Calculator"
Screen _NewImage(800, 600, 32)
start:
Cls
Print: Print: Print
Print "  Prime Number Calculator by SierraKen"
Print: Print: Print
Print "  This will calculate all prime numbers from 1 up to your given number (1-100000)."
Print "  A prime number is a number that can only be divided by itself and 1."
Print "  Then it will save the numbers to PrimeNumbers.txt and will"
Print "  open it in Notepad when it is finished."
Print "  The total amount of prime numbers will be shown at the bottom of the text file."
Print "  Make sure and close this program when it is finished."
Print: Print: Print
Input "  How many numbers do you wish to calculate in sequence between 1-100000: "; n
If n > 100000 Or n < 1 Or n <> Int(n) Then GoTo start:
Print
Open "PrimeNumbers.txt" For Output As #1
Do
    t = t + 1
    If t = 1 Or t = 2 Or t = 3 Then
        Print Str$(t) + ", ";
        Print #1, t
        a = a + 1
        GoTo nex:
    End If
    For tt = 2 To t - 1
        If t / tt = Int(t / tt) Then GoTo nex:
    Next tt
    Print Str$(t) + ", ";
    Print #1, t
    a = a + 1
    nex:
Loop Until t = n
Print #1, ""
Print #1, "Total Prime Numbers Between 1 and " + Str$(t) + ": " + Str$(a)
Close #1
Shell "CMD /C START /MAX notepad " + "PrimeNumbers.txt"
Reply
#2
Here is a prime generator for you:

Code: (Select All)
Rem The Prime Number Generator v2.0a PD 2024.
Rem  First release (06/06/2024)
Rem  Release v2.0a (06/10/2024)
Rem    Adds IsPrime to check test value.
DECLARE FUNCTION Keyboard$ ()
DECLARE FUNCTION FormatString$ (s#)
DECLARE SUB Dot.Display (VarA#, VarB#, VarC!)
DECLARE SUB Back.Space ()
$Checking:Off
Rem $Dynamic
DefDbl A-Z
On Error GoTo Error.Routine
On Timer(1) GoSub StatusLine
Timer On
Rem _Title "Prime Generstor"
Rem _ScreenMove _Middle
GoSub StatusLine
StartLoop:
Width 80, 25
Cls
Color 14
Print "Prime number generator v2.0a."
Color 15
Do
  Print "Enter (T)est/(P)rint/(C)ount?";
  Do
      A$ = InKey$
      If Len(A$) Then Exit Do
  Loop
  A$ = UCase$(A$)
  If A$ = "T" Then
      Print: PrintPrimes = -2
      Do
        Print "Enter test prime?";
        T$ = Keyboard$
        If CDbl(Val(T$)) < 32768 Then
            T = CInt(Val(T$))
        Else
            T = CDbl(Val(T$))
        End If
        Select Case T
            Case Is < 0, 0, 1 ' anything less than 2 is not a prime.
              Print T; "is not prime."
            Case 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 ' short list of first 12 primes.
              Print T; "is prime."
            Case Else
              S = 1024
              Exit Do
        End Select
      Loop
      Exit Do
  End If
  If A$ = "P" Then Print: PrintPrimes = -1: Exit Do
  If A$ = "C" Then Print: PrintPrimes = 0: Exit Do
  Print
Loop
Do
  Print "Enter fast display(Y/N)?";
  Do
      A$ = InKey$
      If Len(A$) Then Exit Do
  Loop
  A$ = UCase$(A$)
  If A$ = "Y" Then Print: FastDisplay = -1: Exit Do
  If A$ = "N" Then Print: FastDisplay = 0: Exit Do
  Print
Loop
If PrintPrimes = -2 Then GoTo ContinueLoop
Do
  Print "Upper prime limit(10-1000000)?";
  U$ = Keyboard$
  If CDbl(Val(U$)) < 32768 Then
      U = CInt(Val(U$))
  Else
      U = CDbl(Val(U$))
  End If
  If U = 0 Then U = 1000000: Exit Do
  If U >= 10 Then Exit Do
Loop
ContinueLoop:
Do
  Print "Upper sieve limit(1024-32767)?";
  S$ = Keyboard$
  If CDbl(Val(S$)) < 32768 Then
      S = CInt(Val(S$))
  Else
      S = CDbl(Val(S$))
  End If
  If S = 0 Then S = 1024: Exit Do
  If S >= 1024 Then Exit Do
Loop
ReDim p(S) As Double
T! = Timer
Color 14
Print "Storing sieve array of "; FormatString$(S); " elements."
VarD = 0
VarE = 0
VarF! = Timer
StartTimer! = Timer
q = 0
Z = 1
Do
  If FastDisplay = 0 Then
      Elapsed! = Timer - StartTimer!
      If Elapsed! < 0! Then Elapsed! = Elapsed! + 86400!
      If Elapsed! >= 1! Then
        Call Dot.Display(VarD, VarE, VarF!)
        StartTimer! = Timer
      End If
  End If
  Z = Z + 1
  F = 0
  For l = 2 To Int(Sqr(Z))
      If Z / l = Int(Z / l) Then
        F = -1
        Exit For
      End If
  Next
  If F = 0 Then
      q = q + 1
      p(q) = Z
  End If
  If q = S Then
      Exit Do
  End If
Loop
Do
  If Pos(0) > 1 Then
      Locate CsrLin, Pos(0) - 1, 0
      Print " ";
      Locate CsrLin, Pos(0) - 1, 1
  Else
      Exit Do
  End If
Loop
' display calculated runtime.
Z! = Timer - T!
If Z! < 0! Then Z! = Z! + 86400!
If Z! > 0! Then
  Z$ = Str$(Z!)
  If InStr(Z$, ".") Then Z$ = Left$(Z$, InStr(Z$, ".") + 1)
  Print " Runtime:"; Z$; " seconds."
End If
Print "Calculating primes.."
Print "Maximum prime: ";
x# = (p(S) * p(S))
Print FormatString$(x#)
Color 15
Print "Press <escape> to quit:"
If PrintPrimes = -2 Then
  ' check sieve arrays.
  Z = T
  GoSub IsPrime
  Select Case F
      Case 0
        Print "Value "; FormatString$(T); " is prime."
      Case -1
        Print "Value "; FormatString$(T); " is not prime."
      Case -2
        Print "Array limit exceeded."
  End Select
  GoTo EndLoop
End If
If PrintPrimes Then
  Print 2 ' first prime is always 2.
End If
VarD = 0
VarE = 0
VarF! = Timer
StartTimer! = Timer
c = 0
Z = 1
x = 0
Do
  Z = Z + 2
  q = 0
  F = 0
  Do
      If FastDisplay = 0 Then
        Elapsed! = Timer - StartTimer!
        If Elapsed! < 0! Then Elapsed! = Elapsed! + 86400!
        If Elapsed! >= 1! Then
            Call Dot.Display(VarD, VarE, VarF!)
            StartTimer! = Timer
        End If
      End If
      q = q + 1
      If q > S Then
        Exit Do
      End If
      If p(q) > Int(Sqr(Z)) Then
        Exit Do
      End If
      If Z / p(q) = Int(Z / p(q)) Then
        F = -1
        Exit Do
      End If
  Loop
  If q > S Then
      Exit Do
  End If
  If F = 0 Then
      c = c + 1
      If PrintPrimes Then
        Print Z
      End If
      If InKey$ = Chr$(27) Then
        Exit Do
      End If
      If Z > U Then
        Exit Do
      End If
      If Z > (p(S) * p(S)) Then
        Exit Do
      End If
  End If
Loop

' clear dots.
Do
  If Pos(0) > 1 Then
      Locate CsrLin, Pos(0) - 1, 0
      Print " ";
      Locate CsrLin, Pos(0) - 1, 1
  Else
      Exit Do
  End If
Loop

' display totals.
Print "Last prime: ";
Print FormatString$(Z)
Print "Primes: ";
Print FormatString$(c + 1)

' display calculated runtime.
Z! = Timer - T!
If Z! < 0! Then Z! = Z! + 86400!
Z$ = Str$(Z!)
If InStr(Z$, ".") Then Z$ = Left$(Z$, InStr(Z$, ".") + 1)
Print " Runtime:"; Z$; " seconds."

' prompt to run again.
EndLoop:
Do
  Color 15
  Print "Run again(Y/N)";
  Do
      A$ = InKey$
      If Len(A$) Then Exit Do
  Loop
  A$ = UCase$(A$)
  If A$ = "Y" Then GoTo StartLoop
  If A$ = "N" Then Exit Do
  Print
Loop
GoSub ClearLine
Color 7
End

' standard error trap
Error.Routine:
Color 12
Print "Error"; Err;
If Err = 6 Then Print " Overflow";
Print
Color 7
Resume EndLoop
End

' display statusline in timer loop.
StatusLine:
X1 = CsrLin
Y1 = Pos(0)
Locate 25, 1
Print "Prime generator " + Date$ + " " + Time$;
If q > 0 Then
  Print " Test:"; q; "    ";
Else
  Print Space$(15);
End If
Locate X1, Y1
Return

' clear statusline in timer loop.
ClearLine:
X1 = CsrLin
Y1 = Pos(0)
Locate 25, 1
Print Space$(79);
Locate X1, Y1
Return

' see if Z is prime. returns F as false if prime.
IsPrime:
q = 0
F = 0
' check in sieve array first.
For x = 1 To S
  If Z = p(x) Then Return
Next
' calculate prime next.
Do
  If FastDisplay = 0 Then
      elapsed = Timer - starttimer
      If elapsed < 0 Then elapsed = elapsed + 86400
      If elapsed >= 1 Then
        Call Dot.Display(VarD, VarE, VarF!)
        starttimer = Timer
      End If
  End If
  q = q + 1
  If q > S Then
      F = -2
      Exit Do
  End If
  If p(q) > Int(Sqr(Z)) Then
      Exit Do
  End If
  If Z / p(q) = Int(Z / p(q)) Then
      F = -1
      Exit Do
  End If
Loop
' clear dots.
Do
  If Pos(0) > 1 Then
      Locate CsrLin, Pos(0) - 1, 0
      Print " ";
      Locate CsrLin, Pos(0) - 1, 1
  Else
      Exit Do
  End If
Loop
Return

Sub Back.Space
  If Pos(0) > 1 Then
      Locate CsrLin, Pos(0) - 1, 0
      Print " ";
      Locate CsrLin, Pos(0) - 1, 1
  End If
End Sub

Sub Dot.Display (VarA, VarB, VarC!)
  VarG! = Timer - VarC!
  If VarG! < 0! Then VarG! = VarG! + 86400!
  If VarG! >= 1! Then
      VarC! = Timer
      If VarA = 0 Then
        VarB = VarB + 1
        Print ".";
        If VarB = 5 Then
            VarA = -1
            VarB = 0
        End If
      Else
        VarB = VarB + 1
        Call Back.Space
        Print " ";
        Call Back.Space
        If VarB = 5 Then
            VarA = 0
            VarB = 0
        End If
      End If
  End If
End Sub

' formats a numeric string.
Function FormatString$ (s)
  x$ = ""
  s$ = Str$(s)
  s$ = LTrim$(s$)
  If InStr(s$, ".") Then s$ = Left$(s$, InStr(s$, ".") - 1)
  For l = Len(s$) To 3 Step -3
      x$ = Mid$(s$, l - 2, 3) + "," + x$
  Next
  If l > 0 Then
      x$ = Mid$(s$, 1, l) + "," + x$
  End If
  If Len(s$) < 3 Then
      x$ = s$
  End If
  If Right$(x$, 1) = "," Then
      x$ = Left$(x$, Len(x$) - 1)
  End If
  FormatString$ = x$
End Function

' get inkey during timer loop.
Function Keyboard$
  V$ = ""
  Do
      A$ = ""
      Do
        A$ = InKey$
        If Len(A$) Then Exit Do
      Loop
      If A$ >= "0" And A$ <= "9" Then
        V$ = V$ + A$
        Print A$;
      End If
      If A$ = Chr$(8) Then '  backspace
        If Len(V$) Then
            V$ = Left$(V$, Len(V$) - 1)
            X1 = CsrLin
            Y1 = Pos(0)
            Locate X1, Y1 - 1
            Print " ";
            Locate X1, Y1 - 1
        End If
      End If
      If A$ = Chr$(27) Then
        For V = 1 To Len(V$)
            X1 = CsrLin
            Y1 = Pos(0)
            Locate X1, Y1 - 1
            Print " ";
            Locate X1, Y1 - 1
        Next
        V$ = ""
      End If
      If A$ = Chr$(13) Then Exit Do
  Loop
  Keyboard$ = V$
  Print
End Function
Reply
#3
Here's one of many of mine:
Code: (Select All)
_Title " A Simple Prime Sieve and Factor of 4 Digit Numbers" ' mod  2024-12-27 bplus
' sieving marks composites ONLY, primes are what is left when composites are crossed off
DefLng A-Z
topN = 9999 ' any 4 digit number
Dim ff(topN) ' need array to hold numbers crossed off, ff stands for first (prime) factor
For pcandidate = 2 To topN ' absolutely no optimation here!!!!
If ff(pcandidate) = 0 Then ' this is next prime discovered so cross off all composites
For i = pcandidate * pcandidate To topN Step pcandidate ' check all multiples of pcand
If ff(i) = 0 Then ff(i) = pcandidate ' going to use ff to factor numbers
Next
End If
Next
For i = 2 To topN
If ff(i) = 0 Then c = c + 1
Next
Print c; " prime numbers 2 to 9999."
Do
Input "Enter a 4 digit number to factor or see if prime ", n
m = n
While ff(m)
Print ff(m);
m = m / ff(m)
Wend
If m = n Then Print n; "is prime." Else Print m
Loop Until n < 2

EDITED to do something useful with our calculations!
b = b + ...
Reply
#4
i think i have shown this off before, but i have to find the topic.  i computed primes beginning with 18 trilliard.

it was somewhat slow on my laptop computer with 2-core cpu and 4gb ram.

i used a routine like that to try to come up with a 19-digit palindromic prime.  so far i have been unsuccessful finding one.

could do that ancient greek guy's sieve with a _bit array that represents only the odd numbers beginning with 3.  sadly it cannot reach anywhere near the positive signed _integer64 limit, would need 64gb ram or much more but it did speed up calculations a bit of really large integers.
Reply
#5
Amazing guys! Very interesting how we all make them differently.
Reply
#6
@hsiangch_ong

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

I can't remember that name either; your avatar name the same Wink
b = b + ...
Reply
#7
(10 hours ago)SierraKen Wrote: Amazing guys! Very interesting how we all make them differently.

yes

This:
Code: (Select All)
If t / tt = Int(t / tt) Then GoTo nex:

Is same as this:
Code: (Select All)
If t Mod tt = 0 then GoTo nex    '  Mod is very handy in coding, BTW colon not needed
b = b + ...
Reply
#8
Woops! LOL I've been adding a colon to them since the 1990's in QBasic, I'll try to remember to keep it off from now on. Thanks!
Reply
#9
This is about as simple as it gets:

A sieve generator:

Code: (Select All)
Print "Enter array size";: Input m
d = m ^ 2
ReDim p(d) As _Byte
For x = 2 To m
  For l = x + x To d Step x
      If l <= d Then
        p(l) = -1
      End If
  Next
Next
For x = 2 To d
  If p(x) = 0 Then Print x;
Next
End
A brute force generator:

Code: (Select All)
Print "Enter upper range";: Input d
For l = 2 To d
  f = 0
  For m = 2 To Int(Sqr(l))
      If l Mod m = 0 Then f = -1: Exit For
  Next
  If f = 0 Then Print l;
Next
End
Reply




Users browsing this thread: 1 Guest(s)