Fortran 90 Program Factorial: Fixing Incorrect Output

Click For Summary

Discussion Overview

The discussion revolves around a Fortran 90 program designed to compute the factorial of a number. Participants explore issues related to incorrect outputs when large numbers are input, potential solutions, and alternative approaches to handle large factorial calculations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants note that the original code fails for large values of n due to exceeding the maximum allowable integer size, leading to incorrect outputs.
  • One participant suggests showing outputs for small values of n (1 through 6) to illustrate the problem.
  • Another participant mentions that implementations exist for calculating factorials beyond 20!, although these are more complex to write.
  • A different approach is proposed involving Fortran 90 intrinsic functions like "selected_int_kind" to allow for larger integers, demonstrating a recursive function for factorial calculation.
  • One participant suggests using double or extended real types to compute factorials for very large numbers, which could handle values up to 4930!.

Areas of Agreement / Disagreement

Participants generally agree that the original program has limitations with large inputs, but multiple competing views exist on how to address these limitations, with no consensus on the best approach.

Contextual Notes

Limitations include the dependence on integer size and the complexity of implementing alternative methods for large factorial calculations. The discussion does not resolve the best method for handling large factorials.

Who May Find This Useful

This discussion may be useful for programmers working with Fortran 90, particularly those interested in numerical methods, factorial calculations, and handling large integers in programming.

Md. Abde Mannaf
Messages
20
Reaction score
1
Code:
program factorial
  implicit none
  integer::fact,i,n
  print*,'enter the value of n'
  read*,n
  fact=1
  do i=1,n
  fact=fact*i
  end do

  print*,'factorial is ',fact
end program

when input n largest number then answer is incorrect. how to solve
 
Technology news on Phys.org
Md. Abde Mannaf said:
Code:
program factorial
  implicit none
  integer::fact,i,n
  print*,'enter the value of n'
  read*,n
  fact=1
  do i=1,n
  fact=fact*i
  end do

  print*,'factorial is ',fact
end program

when input n largest number then answer is incorrect. how to solve

Show us what you get for the first few possibilities for n:

1
2
3
4
5
6

etc.
 
Your code looks good to me. When you say the answer is incorrect for "the largest number", don't forget that fact will be larger than the largest allowable integer in that case. The program will either abort or give an incorrect answer. You can only input values of n that will not make n! too large for the integer fact.
 
  • Like
Likes   Reactions: berkeman
There are implementations of the factorial function that allow the computation of factorials beyond 20!.

The implementations are much more difficult to write, but if you want to get around the storage limits, you have to be a bit more clever.
 
  • Like
Likes   Reactions: Md. Abde Mannaf
You need to start using Fortran90 intrinsic functions that allow for requesting more storage in a platform compatible manner, I am talking about functions like "selected_int_kind"; in my laptop, I was able to go as high as 18. Needless to say, the factorial function is a nice textbook exercise to show another Fortran90 feature: recursive attribute.

Code:
program torial
    implicit none
   
    integer, parameter :: IK = selected_int_kind(18)
    integer(IK) :: i
     
    write(*,'(A,$)') 'Number? : '
    read(*,*) i
    write(*,*) 'Factorial is ',factorial(i)

contains

    recursive function factorial( n ) result (f)
        integer(IK) :: f, n
       
        if( n == 1 ) then
          f = 1
        else
          f = n * factorial( n - 1_IK )
        end if
       
    end function factorial
   
end program torial

> a.exe
Number? : 20
Factorial is 2432902008176640000
 
  • Like
Likes   Reactions: Md. Abde Mannaf
For large numbers may you don't need exact an integer. Try the same code with result as double or extended real (16 byte float number gives answer up to 4930!).
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K