# What ChatGPT says about Fortran

Books on algorithms and numerical analysis often have pseudocode. A Wikipedia article presents a few styles of pseudocode and notes that pseudocode has not been standardized. If you write in a pseudocode that resembles Fortran, Python, R, or some mix of them, ChatGPT-4 can usually generate valid Fortran from it. I wonder if the Fortran community could agree on a syntax for a pseudocode that resembles Fortran but is shorter and simpler that special-purpose tools or LLMs could translate to standard Fortran. Syntax of interactive Fortran is a related thread from 2021.

ChatGPT-4 translates

``````do i = 1 to 16
if mod(i, 3) == 0 and mod(i, 5) == 0
print "fizzbuzz"
else if mod(i, 3) == 0
print "fizz"
else if mod(i, 5) == 0
print "buzz"
else
print i
end if
end do
``````

to valid Fortran

``````program FizzBuzz
implicit none
integer :: i

do i = 1, 16
if (mod(i, 3) == 0 .and. mod(i, 5) == 0) then
print *, 'fizzbuzz'
else if (mod(i, 3) == 0) then
print *, 'fizz'
else if (mod(i, 5) == 0) then
print *, 'buzz'
else
print *, i
end if
end do

end program FizzBuzz
``````

and does the same if `do i=1, 16` is replaced by `for i in 1:16`, terminated by `next` instead of `end do`.

It translates pseudocode to simulate pi

``````nran = 10^6
nin = 0
for i in 1:ran
x = rand()
y = rand()
if (x**2 + y**2) < 1
nin += 1
end if
next
print "pi =",4*nin/nran
``````

to

``````program estimate_pi
implicit none
integer, parameter :: dp = kind(1.d0)
integer :: i, nran, nin
real(dp) :: x, y, pi_estimate
integer :: count, count_rate, count_max, seed_size
integer, allocatable :: seed(:)

call system_clock(count=count, count_rate=count_rate, count_max=count_max)
call random_seed(size=seed_size)
allocate(seed(seed_size))
seed = count * count_max / count_rate
call random_seed(put=seed)

nran = 10**6
nin = 0

do i = 1, nran
call random_number(x)
call random_number(y)
if ((x**2 + y**2) < 1) then
nin = nin + 1
end if
end do

pi_estimate = 4.d0 * nin / nran
print*, "pi =", pi_estimate
end program estimate_pi
``````

admittedly after a few iterations of being fed gfortran error messages regarding its misuse of `random_seed`