# Thread: Counting Primes and the 6k+/-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
Uses "Console"
Dim a, w, c, tot As Long

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  Reply With Quote

2. Hi Primo,

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

Petr  Reply With Quote

3. Originally Posted by primo 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?   Reply With Quote

#### Posting Permissions

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