Jump to content

Looking for Prime Numbers?

Recommended Posts

Thanks to a CyberSlug idea and a my idea we have a much faster version!

Dim $PRIMI[2], $N = 1, $C, $T = 0, $L, $LIMIT, $PATH
$LIMIT = 5000
$PATH = 'c:\primi.txt'

If $LIMIT < 2 Then Exit
$PRIMI[0] = 1
$PRIMI[1] = 2
$FILE = FileOpen($PATH, 2)
If $FILE = -1 Then Exit

FileWrite($FILE, '2' & @CRLF);It adds 2 to the file since it is already in the array as begin of all.
HotKeySet('!x', '_Terminate')

While 1
   $N = $N + 2;It skips all divisible by 2 numbers. The 2 is already added as begin.
              ;since they can't be prime.
   $T = 0
   If $N >= $LIMIT Then Exit
   For $C = 1 To $PRIMI[0]
      If $PRIMI[$C] > Sqrt($N) Then ExitLoop;It avoids useless controls
      If Mod($N, $PRIMI[$C]) = 0 Then
         $T = 1
   If $T = 0 Then
      $L = UBound($PRIMI)
      ReDim $PRIMI[$L + 1]
      $PRIMI[0] = $L
      $PRIMI[$L] = $N
      FileWrite($FILE, $PRIMI[$PRIMI[0]] & @CRLF)

Func _Terminate()
EndFunc  ;==>_Terminate
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...