[Gambas-user] I asked chat gpt to write a gambas script, here is what it did
Brian G
brian at westwoodsvcs.com
Tue Feb 14 08:37:10 CET 2023
I asked it for a faster algorithm it gave me this
using the Sieve of Eratosthenes, along with the count:
Public Sub Main()
Dim limit As Integer = 3000
Dim isPrime As Boolean[] = New Boolean[limit + 1]
Dim primes As New Integer[]
For i As Integer = 2 To limit
isPrime[i] = True
Next
For i As Integer = 2 To limit
If isPrime[i] Then
primes.Add(i)
For j As Integer = i * i To limit Step i
isPrime[j] = False
Next
Endif
Next
Print "Prime numbers between 2 and 300:"
For Each prime As Integer In primes
Print prime
Next
Print "Total number of primes found: " & primes.Count
End
"Failure is the key to success;
each mistake teaches us something" .. Morihei Ueshiba
Brian G
----- On Feb 13, 2023, at 11:24 PM, Brian G <brian at westwoodsvcs.com> wrote:
> I asked
> write a gambas script to print primes from 2 to 300
> then added
> please also print the number of primes found
> here is the script it produced
> Public Sub Main() Dim primes As New Integer[] For i As Integer = 2 To 300 Dim
> isPrime As Boolean = True For j As Integer = 2 To i - 1 If i Mod j = 0 Then
> isPrime = False Exit Endif Next If isPrime Then primes.Add(i) Endif Next Print
> "Prime numbers between 2 and 300:" For Each prime As Integer In primes Print
> prime Next Print "Total number of primes found: " & primes.Count End
> Not elegant but it worked just fine. Ok do we really have a future here!
> spoiler, anything more complex was junk, but still....
> "Failure is the key to success;
> each mistake teaches us something" .. Morihei Ueshiba
> Brian G
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230213/441477b6/attachment.htm>
More information about the User
mailing list