Results 1 to 3 of 3

Thread: Counting Primes and the 6k+/-1

  1. #1

    Counting Primes and the 6k+/-1

    can you believe that every prime number is a multiple of 6 + or -1. as an example 17=6*3 - 1
    31=6*5 + 1
    and so on forever
    i have asked ChatGPT to give me a JavaScript code to count the number of primes from 1 to 1000000 using the formula of 6*k+ or -1
    he said sure here it is: attached the Chat with his description.
    realy the robots will make the people lazy and their brains will be smaller over time.
    converting the JS to thinbasic is almost straightforward
    the time is about 12 seconds
    but if we use the thinbasic IsPrime function it is zero seconds since it is inside thinbasic such as in this code
    Uses "Console"
    Long i,a,tot
    For i=1 To 1000000 Step 2
      If IsPrime(i) Then
      tot=tot+1
      End If
    Next
    PrintL Str$(tot+1)  'we add 1 to consider number 2 as a prime
    PrintL "Press a key to end program"
    '---Wait for a key press
    WaitKey
    

    thinbasic code converted from the robot javascript:
    'author: ChatGPT
    '---Load Console Module
    Uses "Console"
    Dim a, w, c, tot As Long
    Dim T1, T2 As Quad
    
    HiResTimer_Init
    T1 = HiResTimer_Delta
    
    Function IsPrimeChatGPT(n)
      ' Check If n is less than 2 Or divisible by 2 
      Long i
      If n < 2 Or Mod(n, 2) = 0 Then
        Return FALSE
      End If
    
      ' Check If n is divisible by Any odd Number up To the square root of n
      'For (let i = 3; i <= Math.sqrt(n); i += 2) {
      For i = 3 To Sqr(n) Step 2
        If Mod(n,i)= 0 Then
          Return FALSE
        End If
      Next
    
      ' n is prime
      Return TRUE
    End Function
    
    Function countPrimes()
      Long count, i
      count = 0
    
      ' Check All numbers Between 2 And 1000000 Using the 6k+1 And 6k-1 formula
      For i = 5 To 1000000 Step 6 
        If (IsPrimeChatGPT(i)) = TRUE Then
          count+=1
        End If
    
        If (IsPrimeChatGPT(i + 2))= TRUE Then 
          count+=1
        End If
      Next
    
      ' Add 3 And 2 To the count since they are prime numbers but Not checked Using the formula
      count += 2
    
      Return count
    End Function
    
    PrintL Str$(countPrimes()) 
    T2 = HiResTimer_Delta
    T2 = T2/1000000
    PrintL "Elapsed time is " & T2 & " (seconds)"
    
    PrintL "Press a key to end program"
    
    '---Wait for a key press
    WaitKey
    
    regarding the javascript code save the attached "IsPrimeJS.txt" as html file
    i have included also the chat to see the robot description
    Attached Files Attached Files

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732
    Hi Primo,

    thank you for sharing your thoughts and the algorithm I never observed the relationship with number 6!


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,782
    Rep Power
    10
    Quote Originally Posted by primo View Post
    but if we use the thinbasic IsPrime function it is zero seconds since it is inside thinbasic such as in this code
    Uses "Console"
    Long i,a,tot
    For i=1 To 1000000 Step 2
      If IsPrime(i) Then
      tot=tot+1
      End If
    Next
    PrintL Str$(tot+1)  'we add 1 to consider number 2 as a prime
    PrintL "Press a key to end program"
    '---Wait for a key press
    WaitKey
    
    Super fast! Isn't it?
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. Caussian Primes fun - WinTimer question
    By RobbeK in forum thinBasic General
    Replies: 4
    Last Post: 28-02-2014, 00:01
  2. Haskell: factoring into primes
    By danbaron in forum Science
    Replies: 0
    Last Post: 12-06-2011, 08:47
  3. primes spiral with labels (canvas example)
    By zak in forum UI (User Interface)
    Replies: 2
    Last Post: 30-07-2010, 21:07

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •