Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Combination is not working correctly -- fortran 90

  1. May 29, 2015 #1
    Code (Text):

    !subprogram for combination
    !Author:: mannaf

    function fact(n)
        implicit none
       integer ::fact,n,i
         do i=1,n
        end do

    program comb
        implicit none
        integer ::fact,n,r,combination
        print*,'enter the value of nCr'
    if ( n>=0 .and. r>=0 .and. n>=r) then
    print*,'combination is =',combination
        print*,'combination is not possible  '
    end if

    end program
    when we input n=13,r=2 then result is not correct? how to solve?
  2. jcsd
  3. May 29, 2015 #2


    User Avatar

    Staff: Mentor

    What is the problem statement? What is the code supposed to do?

    BTW, I've enclosed your program in "code" tags -- it helps the readability a lot. Just use [ code ] and [ /code ] (without the spaces) around your program code when posting here. :smile:
  4. May 29, 2015 #3


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    13! = 6,227,020,800. If the default largest signed integer allowed is Int32, that is 2,147,483,647. So Int32 is not large enough to hold the number 13! You need to specify that fact is int64. That will allow signed integers up to 9,223,372,036,854,775,807. That will be large enough for 20!, but not for 21!. You can still calculate larger combinatorial expressions, but you will have to cancel common factors of numerator and denominator to avoid such large factorials.
    Last edited: May 29, 2015
  5. May 29, 2015 #4


    Staff: Mentor

    What did you get for a result? The correct answer is 78.

    Your factorial function looks OK, although it doesn't give the correct answer for 0!, which is 1, by definition.

    Edit: As FactChecker notes, 13! will overflow the capacity of a 4-byte signed integer.
  6. Jun 5, 2015 #5
    If you need large numbers try to define nfactorial as real.
  7. Jun 5, 2015 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    If you try to calculate combinations by a "brute force" method, like trying to compute nCr = n! / [(n-r)! * r!], as you have found, you will run into issues of precision when evaluating factorials of large numbers. This is a side effect of using only a fixed number of bytes to represent fixed point (integer) or floating point (real) numbers.

    By analyzing the formula for calculating a combination, we can see that computing the factorials in the numerator and denominator and then doing the division leads to a certain number of cancellations. Why not exploit this situation and only calculate the terms in the combination formula which do not cancel?
  8. Jun 6, 2015 #7
    Here it is, purposely put together in an unacceptable fashion, so make sure to work your way out of this one
    Code (Text):

    program comb
        integer i, n, r
        read(*,*) n, r
        if ( n >= 0 .and. r >= 0 .and. n>=r ) then
            write(*,*) 'Number of combinations is =', torial()/fact()
            write(*,*) "No can do"
        end if    
    integer function fact()
        fact = 1      
        do i = 1, r
            fact = fact*i    
        end do    
    end function fact
    integer function torial()
        torial = 1      
        do i = n-r+1, n        
            torial = torial*i    
        end do    
    end function torial
    end program comb
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook