TSE: Out of scope: How to let ChatGPT generate working TSE programs by letting it rewrite missing features?

12 views
Skip to first unread message

knud van eeden

unread,
Mar 15, 2023, 12:31:48 AM3/15/23
to SemWare TSE Pro Text Editor
Hello,

FYIO:

ChatGPT remembers what you query, use e.g. rewrite
to replace missing information.

ChatGPT: "Can you write a Semware SAL program to calculate the first 1000 primes?"

Certainly! Here is a Semware SAL program that uses the Sieve of Eratosthenes algorithm to calculate the first 1000 primes:

less
Copy code
// library: math <description></description>
// <version control></version control> (filenamemacro=primes.s) [<Program>] [<Research>] [kn, ri, su, 12-03-2023 23:06:28]

PROC Main()
    INTEGER maxN = 7919 // The 1000th prime number
    INTEGER sieve[maxN + 1] // Create an array to hold the sieve
    INTEGER primes[1000] // Create an array to hold the primes
    INTEGER count = 0 // Counter for the number of primes found
    INTEGER i, j // Loop variables

    // Initialize the sieve
    FOR i = 2 TO maxN
        sieve[i] = TRUE
    ENDFOR

    // Perform the Sieve of Eratosthenes
    FOR i = 2 TO SQRT(maxN)
        IF sieve[i] THEN
            FOR j = i*i TO maxN STEP i
                sieve[j] = FALSE
            ENDFOR
        ENDIF
    ENDFOR

    // Collect the primes into an array
    FOR i = 2 TO maxN
        IF sieve[i] THEN
            primes[count] = i
            count = count + 1
            IF count = 1000 THEN BREAK ENDIF // We have found the first 1000 primes
        ENDIF
    ENDFOR

    // Display the first 1000 primes
    FOR i = 0 TO 999
        Message( Format( primes[i], "d" ) )
    ENDFOR
END

This program initializes a sieve array to hold Boolean values
indicating whether each number from 2 to the 1000th prime is prime or
not. It then performs the Sieve of Eratosthenes algorithm by marking
all multiples of each prime as composite. Finally, it collects the
primes into an array and displays the first 1000 primes.

Note that this program is optimized for readability and simplicity,
rather than efficiency. There are more efficient algorithms for finding
primes, such as the Sieve of Atkin or the Miller-Rabin primality test,
which may be more appropriate for larger prime ranges.

===

Rrewrite this program because Semware SAL does not know about a STEP

Sure! Here is a rewritten version of the program that does not use the STEP keyword:

// library: math <description></description>
// <version control></version control> (filenamemacro=primes.s) [<Program>] [<Research>] [kn, ri, su, 12-03-2023 23:06:28]

PROC Main()
    INTEGER maxN = 7919 // The 1000th prime number
    INTEGER sieve[maxN + 1] // Create an array to hold the sieve
    INTEGER primes[1000] // Create an array to hold the primes
    INTEGER count = 0 // Counter for the number of primes found
    INTEGER i, j // Loop variables

    // Initialize the sieve
    FOR i = 2 TO maxN
        sieve[i] = TRUE
    ENDFOR

    // Perform the Sieve of Eratosthenes
    FOR i = 2 TO SQRT(maxN)
        IF sieve[i] THEN
            j = i*i
            WHILE j <= maxN
                sieve[j] = FALSE
                j = j + i
            ENDWHILE
        ENDIF
    ENDFOR

    // Collect the primes into an array
    FOR i = 2 TO maxN
        IF sieve[i] THEN
            primes[count] = i
            count = count + 1
            IF count = 1000 THEN BREAK ENDIF // We have found the first 1000 primes
        ENDIF
    ENDFOR

    // Display the first 1000 primes
    FOR i = 0 TO 999
        Message( Format( primes[i], "d" ) )
    ENDFOR
END
In this version, the STEP keyword in the inner FOR loop has been replaced with a WHILE loop that increments j by i in each iteration. This achieves the same effect as the original STEP i construct.

===

Rewrite this program because Semware SAL does not know about a SQRT

Sure! Here is a rewritten version of the program that does not use the SQRT function:

less
Copy code
// library: math <description></description>
// <version control></version control> (filenamemacro=primes.s) [<Program>] [<Research>] [kn, ri, su, 12-03-2023 23:06:28]

PROC Main()
    INTEGER maxN = 7919 // The 1000th prime number
    INTEGER sieve[maxN + 1] // Create an array to hold the sieve
    INTEGER primes[1000] // Create an array to hold the primes
    INTEGER count = 0 // Counter for the number of primes found
    INTEGER i, j, limit // Loop variables

    // Calculate the integer square root of maxN
    limit = 0
    WHILE limit*limit <= maxN
        limit = limit + 1
    ENDWHILE
    limit = limit - 1

    // Initialize the sieve
    FOR i = 2 TO maxN
        sieve[i] = TRUE
    ENDFOR

    // Perform the Sieve of Eratosthenes
    FOR i = 2 TO limit
        IF sieve[i] THEN
            j = i*i
            WHILE j <= maxN
                sieve[j] = FALSE
                j = j + i
            ENDWHILE
        ENDIF
    ENDFOR

    // Collect the primes into an array
    FOR i = 2 TO maxN
        IF sieve[i] THEN
            primes[count] = i
            count = count + 1
            IF count = 1000 THEN BREAK ENDIF // We have found the first 1000 primes
        ENDIF
    ENDFOR

    // Display the first 1000 primes
    FOR i = 0 TO 999
        Message( Format( primes[i], "d" ) )
    ENDFOR
END

In this version, the SQRT function has been replaced with a loop that
calculates the integer square root of maxN. The loop increments limit
until limit*limit exceeds maxN, then decrements limit by 1 to get the
correct value of the square root. The rest of the program is similar to
the original version.

===

rewrite the program because Semware SAL arrays start at 1

Sure! Here is a rewritten version of the program that takes into account that Semware SAL arrays start at 1:

// library: math <description></description>
// <version control></version control> (filenamemacro=primes.s) [<Program>] [<Research>] [kn, ri, su, 12-03-2023 23:06:28]

PROC Main()
    INTEGER maxN = 7919 // The 1000th prime number
    INTEGER sieve[maxN] // Create an array to hold the sieve
    INTEGER primes[1000] // Create an array to hold the primes
    INTEGER count = 0 // Counter for the number of primes found
    INTEGER i, j, limit // Loop variables

    // Calculate the integer square root of maxN
    limit = 0
    WHILE limit*limit < maxN
        limit = limit + 1
    ENDWHILE
    limit = limit - 1

    // Initialize the sieve
    FOR i = 1 TO maxN
        sieve[i] = TRUE
    ENDFOR

    // Perform the Sieve of Eratosthenes
    FOR i = 2 TO limit
        IF sieve[i] THEN
            j = i*i
            WHILE j <= maxN
                sieve[j] = FALSE
                j = j + i
            ENDWHILE
        ENDIF
    ENDFOR

    // Collect the primes into an array
    FOR i = 2 TO maxN
        IF sieve[i] THEN
            primes[count+1] = i
            count = count + 1
            IF count = 1000 THEN BREAK ENDIF // We have found the first 1000 primes
        ENDIF
    ENDFOR

    // Display the first 1000 primes
    FOR i = 1 TO 1000
        Message( Format( primes[i], "d" ) )
    ENDFOR
END

In this version, the arrays are declared to have a maximum size of maxN
and 1000, respectively, and the loop indices start at 1 instead of 2.
The limit variable is calculated in the same way as before, and the
rest of the program is similar to the previous version, except that the
count variable is incremented before storing the current prime in the
primes array to account for the fact that array indices start at 1 in
Semware SAL. The FOR loop that displays the primes is also adjusted to
start at 1 and end at 1000.

===

After some very smale changes like
moving the array declarations to be global variables
and saving the program with a .si extension,
removing the THEN
and using ==
this was the final working program

--- cut here: begin --------------------------------------------------

// library: math <description></description>
// <version control></version control> (filenamemacro=primes.s) [<Program>] [<Research>] [kn, ri, su, 12-03-2023 23:06:28]

    INTEGER maxN = 7919 // The 1000th prime number
    INTEGER sieve[ 7919 ] // Create an array to hold the sieve
    INTEGER primes[1000] // Create an array to hold the primes

PROC Main()
    INTEGER count = 0 // Counter for the number of primes found
    INTEGER i, j, limit // Loop variables

    // Calculate the integer square root of maxN
    limit = 0
    WHILE limit*limit < maxN
        limit = limit + 1
    ENDWHILE
    limit = limit - 1

    // Initialize the sieve
    FOR i = 1 TO maxN
        sieve[i] = TRUE
    ENDFOR

    // Perform the Sieve of Eratosthenes
    FOR i = 2 TO limit
        IF sieve[i]
            j = i*i
            WHILE j <= maxN
                sieve[j] = FALSE
                j = j + i
            ENDWHILE
        ENDIF
    ENDFOR

    // Collect the primes into an array
    FOR i = 2 TO maxN
        IF sieve[i]
            primes[count+1] = i
            count = count + 1
            IF count == 1000 BREAK ENDIF // We've found the first 1000 primes
        ENDIF
    ENDFOR

    // Display the first 1000 primes
    FOR i = 1 TO 1000
        Warn( Format( primes[i] ) )
    ENDFOR
END

--- cut here: end ----------------------------------------------------

with friendly greetings
Knud van Eeden

knud van eeden

unread,
Mar 15, 2023, 10:07:50 PM3/15/23
to SemWare TSE Pro Text Editor
Hello,

FYIO:

ChatGPT has now the possibility to use GPT-4.

But that does not really know TSE SAL language anymore, e.g. I could not reproduce the previous 1000 primes example anymore and it generates all kind of other computer languages output.

So I used 'Model: Legacy (GPT-3.5)' (and if necessary told the program explicitly to use Semware SAL language, instead of e.g. Python).

That seems to work for now and I could again automatically generate interesting TSE Semware SAL language programs.
Reply all
Reply to author
Forward
0 new messages