0

I am doing Project Euler 29 in Fortran. I am having a problem when the numbers get too big (I believe that is the case as the numbers turn negative when I print them), although I am using int64 from iso_fortran_env. This is my code (ignore the line numbers on the left):

1           PROGRAM MAIN                                                                   
  1             USE ISO_FORTRAN_ENV
  2             IMPLICIT NONE
  3             REAL :: START, FINISH
  4             INTEGER :: COUNTER
  5             INTEGER(KIND=INT64) :: A_LARGEINT(100000) !ARRAY THAT
  6         !CONTAINS THE NUMBERS
  7             INTEGER :: I,K,POS
  8             INTEGER(KIND=INT64) :: NUM_LARGEINT
  9             CALL CPU_TIME(START)
 10                 COUNTER=0
 11                 POS=1
 12                 DO I=2,100
 13                 DO K=2,100
 14                 NUM_LARGEINT = I**K
 15                 !NUM_LARGEINT = HUGE(NUM)
 16                 PRINT *,NUM_LARGEINT
 17                 IF (ANY(A_LARGEINT==NUM_LARGEINT)) THEN
 18                 ELSE
 19                     COUNTER = COUNTER + 1
 20                     A_LARGEINT(POS) = NUM_LARGEINT
 21                     POS = POS + 1
 22                 ENDIF
 23                 ENDDO
 24                 ENDDO
 25                 PRINT*, A_LARGEINT, COUNTER
 26             CALL CPU_TIME(FINISH)
 27             PRINT*, "TIME TAKEN: ", FINISH-START
 28         END PROGRAM MAIN

Any suggestions why this int64 type doesn't work? Or if there is any way to deal with even larger integers?

Many thanks in advance

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
paulanueno
  • 31
  • 2
  • It would really help if you could describe in more detail what you mean by "I am having a problem when the numbers get too big". But almost certainly it because of the many default kind integer calculations you have - just because you are assigning to a int64 doesn't mean long integers are used for the evaluation of an expression. You will need to make the appropriate variables and constants the correct kind for everything to be done consistantly in kind int64 arithmetic. NUM_LARGEINT = I**K looks a prime suspect, there may be others – Ian Bush Jul 31 '20 at 11:24
  • 2
    I'm not a specialist on FORTRAN, but a 64bit integer can hold values up to 2^63 (about 10^19). And I think you are calculating values like 2^100, 3^100, ... which definitely exceed the valid range. – Ronald Jul 31 '20 at 11:27
  • For example, these are the first numbers of the calculation: 4,8,16,32,64,128,256,512,...,1073741824, -2147483648,0,0,0 – paulanueno Jul 31 '20 at 11:29
  • @Ronald - good point, yes int64 won't do this! Also please note the spelling is officially Fortran, and has been for at least 30 years. – Ian Bush Jul 31 '20 at 11:34
  • Probable cause (but comments from Ronald and Ian bush still hol), `NUM_LARGEINT = I**K` as `I` and `K` are "just" `INTEGER` so calculation is done in `INTEGER` and the results in what you see. When using `INTEGER(KIND=INT64) :: I,K, POS` results will be a little better but after a little while the same problem reappears. So you need to resort to a "big number" / "Arbitrary-precision_arithmetic" package (disclaimer no experience with it)> – albert Jul 31 '20 at 11:42

0 Answers0