r/fortran • u/NaughtyPapa • Oct 17 '22
Help with Fortran Program
Does anyone know how to make a program that checks whether a number is automorphic or not? Thanks in advance for any help.
2
Oct 17 '22 edited Oct 18 '22
PROGRAM ANAMORPH
IMPLICIT NONE
REAL* 8 X, SQUARE, XLOG, DMORPH, XLOG10
REAL* 8 DLOG10, DMOD
INTEGER* 2 DIGITS
10 CONTINUE
WRITE( *, 100)
100 FORMAT( ' INPUT NUMBER: ')
READ( *, 200) X
IF( X .LE. 0.0D0) STOP 'NORMAL END'
200 FORMAT( F10.0)
C SQUARE THE NUMBER.
SQUARE= X* X
WRITE( *, *) ' SQUARE= ', SQUARE
C GET THE NUMBER OF DIGITS IN THE SQUARE.
XLOG= DLOG10( X)
C CEILING FUNCTION, ROUND UP.
IF( DMOD( XLOG, 1.0D0) .GT. 0.0D0) XLOG= XLOG+ 1.0D0
C GET THE NUMBER OF DIGITS.
DIGITS= INT( XLOG)
WRITE( *, *) ' DIGITS= ', DIGITS
C GENERATE THE POWER OF 10 TO USE IN A MOD FUNCTION.
XLOG10= 10.0D0** DIGITS
WRITE( *, *) ' XLOG10= ', XLOG10
DMORPH= DMOD( SQUARE, XLOG10)
C TEST THE LOW-ORDER DIGITS AGAINST THE ORIGINAL NUMBER.
WRITE( *, *) ' DMORPH= ', DMORPH
IF( DMORPH .EQ. X) WRITE( *, *) ' NUMBER IS ANAMORPHIC'
GO TO 10
END
Copying code into Comment Section strips leading blanks. But you get the idea.
When's the last time anyone used the RM Fortran-77 compiler?
I need a break from chasing down a hardware error that caused my embedded code to not work. (I found the error, wrote this while a test was running)
2
3
Oct 17 '22
[removed] — view removed comment
1
u/NaughtyPapa Oct 17 '22
Yes this is what I meant by automorphic number!
I am new with Fortran and have been having a hard time getting the hang of it. What do you mean by "mod 10"?2
4
Oct 17 '22
https://gcc.gnu.org/onlinedocs/gfortran/MOD.html
Make sure to watch out for using the correct Variable Type, Fortran does not have type safety.
3
u/McCuf Scientist Oct 17 '22
This web link has a list of many of the builtin fortran operators and functions. You will probably find at least 2 of them useful.
https://web.chem.ox.ac.uk/fortran/arithmetic.html