| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | 250224T075010Z | roblogic | |
| 077 | Arithmetic if Fortran | 250224T210800Z | avid |
| nan | 250224T090342Z | roblogic | |
| nan | 250224T084318Z | roblogic | |
| nan | 250224T082804Z | roblogic | |
| nan | 250224T081149Z | roblogic | |
| nan | 250224T073601Z | roblogic | |
| nan | 250224T014354Z | Ahamad |
Use uninitialised variables for a random effect
If you want non-deterministic behaviour, but it doesn't have to be uniformly random, just do something like print*,i (where i hasn't been previously set) and Fortran (on TIO) will give you an undetermined integer. Or you can do print*,mod(i,5) to get a random integer from 0 to 4. Example
Arithmetic if (Fortran 77)
Rather than
if (e < 0) then
A()
else if (e == 0) then
B()
else
C()
end if
D()
you can use
if(e)1,2,3
1 A()
goto 4
2 B()
goto 4
3 C()
4 D()
Often, one or more of the gotos can be omitted. For example:
if(e)1,1,2
1 A()
2 B()
is equivalent to
if(e<=0)then
A()
endif
B()
This saves 7 characters: the statement labels themselves are 'free', as the first 6 characters on each line must be present and cannot be used for anything else.
Computed goto
A similar feature is the computed goto, which takes the form
goto(6,7,8,9)i
A()
6 B()
7 C()
8 D()
9 E()
which can be expanded into something like
if(i==4)then goto 9
if(i==3)then goto 8
if(i==2)then goto 7
if(i==1)then goto 6
A()
6 B()
7 C()
8 D()
9 E()
This can be used to implement something like a switch statement.
Some handy print formats:
print"(*(g0))" formats integers to minimum width, instead of their usual fixed 20-odd character width
print'(16Z3.2)' repeat 16 times, convert to hex, 3 characters wide, 0-padded to width 2
Use a function to minimise I/O boilerplate
Setting up variables and reading data from stdin uses up bytes! This is usually allowed to be handled elsewhere, as long as your submission is a working function or subroutine. Example
Use the short form of if when possible
If it's a simple condition, don't do this: if(a.gt.b)then;print*,'yay';endif
Do this instead: if(a>b)print*,'yay'. Example
Use implicit do loops inside print statements
Instead of: do i=1,n;print*,i;enddo
Try this: print*,(i,i=1,n)
You can nest these loops too, using more parentheses (): (reference)
print*,((i*j,i=1,10),j=1,10)
Use macros to #define repeated stuff
Just add the -cpp flag to the gfortran compiler. Easier than subroutines or loops. Example
Leverage Default Behavior:
Don’t declare IMPLICIT NONE unless the problem demands it. Rely on Fortran’s default typing to save characters.
Use Fixed-Form Tricks:
If allowed, use fixed-form Fortran (.f) where columns 1-5 are for labels and 7-72 for code. You can omit spaces and pack statements tightly. For example:
10I=I+1
is shorter than free-form equivalents.
Minimize I/O:
Input/output statements like READ and WRITE can be verbose. Use the shortest form possible:
READ*,Ninstead ofREAD(*,*) N.PRINT*,Xinstead ofWRITE(*,*) X.
Exploit Loops:
Fortran’s DO loop can be concise. For example:
DO1I=1,N
in fixed-form is shorter than DO I=1,N with an END DO. If the loop body is one line, skip the END DO and use a label.
Program Structure:
Skip PROGRAM and END PROGRAM if the problem allows an implicit program. A bare subroutine or just executable lines can work:
N=5;PRINT*,N
instead of:
PROGRAM P N=5 PRINT*,N END PROGRAM P
String Handling:
Fortran’s string output can be golfed with concatenation (//) or by avoiding unnecessary quotes. For example, PRINT*,'A',I can sometimes be PRINT*,A,I if A is a character variable.
Know Your Compiler:
Some tricks depend on the compiler (e.g., gfortran vs. ifort). Test minimal syntax like omitting optional keywords or semicolons.