I would like to solve a given equation of the following kind with Fortran:
1 ? 2 ? 3 = 7
In this equation only the arithmetic operators are missing and the solution would be '+' for the first question mark and '*' for the second one. I would like to write a short script that finds the correct operators by brute force. So in this case four times four cases would have to be checked. In order to do this I would like to store the operators in an array and use them in a nested do loop:
value1=1
value2=2
value3=3
result=7
op(1)=+
op(2)=-
op(3)=/
op(4)=*
do i=1,4
do j=1,4
if(value1 op(i) value2 op(j) value3 .eq. result) then
write(*,*)'Found solution: ' ,op(i), op(j)
else
j=j+1
endif
enddo
i=i+1
enddo
Apparently this doesn't work because of the wrong interpretation of the if-statement. Any ideas how to make this work?