# Combination is not working correctly -- fortran 90

• Fortran
• Md. Abde Mannaf
In summary, this program calculates the number of combinations given a value of n and r, taking into account the issue of precision when evaluating factorials of large numbers by only calculating the terms that do not cancel in the combination formula.
Md. Abde Mannaf
Code:
!subprogram for combination
!Author:: mannaf

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

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
combination=fact(n)/(fact(n-r)*fact(r))
print*,'combination is =',combination
else
print*,'combination is not possible  '
end if

end program

when we input n=13,r=2 then result is not correct? how to solve?

Md. Abde Mannaf said:
Code:
!subprogram for combination
!Author:: mannaf

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

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
combination=fact(n)/(fact(n-r)*fact(r))
print*,'combination is =',combination
else
print*,'combination is not possible  '
end if

end program

when we input n=13,r=2 then result is not correct? how to solve?

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.

Md. Abde Mannaf
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:
berkeman
Md. Abde Mannaf said:
Code:
!subprogram for combination
!Author:: mannaf

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

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
combination=fact(n)/(fact(n-r)*fact(r))
print*,'combination is =',combination
else
print*,'combination is not possible  '
end if

end program

when we input n=13,r=2 then result is not correct? how to solve?
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.

Md. Abde Mannaf
If you need large numbers try to define nfactorial as real.

Md. Abde Mannaf said:
Code:
!subprogram for combination
!Author:: mannaf

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

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
combination=fact(n)/(fact(n-r)*fact(r))
print*,'combination is =',combination
else
print*,'combination is not possible  '
end if

end program

when we input n=13,r=2 then result is not correct? how to solve?

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?

Md. Abde Mannaf and theodoros.mihos
Here it is, purposely put together in an unacceptable fashion, so make sure to work your way out of this one
Code:
program comb
integer i, n, r
if ( n >= 0 .and. r >= 0 .and. n>=r ) then
write(*,*) 'Number of combinations is =', torial()/fact()
else
write(*,*) "No can do"
end if
contains
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

Md. Abde Mannaf

## 1. Why is my code giving an error message "Combination is not working correctly"?

There could be a few reasons for this error message. It could be due to a typo or syntax error in your code, or it could be caused by a logical error in your algorithm. It is also possible that you are trying to combine incompatible data types. Double check your code and make sure your logic is correct.

## 2. How can I fix the "Combination is not working correctly" error in Fortran 90?

First, try to identify the specific line of code where the error is occurring. This can give you a clue as to what might be causing the issue. Then, carefully review your code and check for any typos or syntax errors. If you are still unable to fix the error, consider seeking help from a more experienced Fortran programmer or consulting the Fortran 90 documentation.

## 3. Can incompatible data types cause the "Combination is not working correctly" error in Fortran 90?

Yes, trying to combine data types that are not compatible can result in this error message. For example, trying to add a string and an integer together will cause an error. Make sure that you are combining data types that can be operated on together.

## 4. How do I know if my algorithm is causing the "Combination is not working correctly" error in Fortran 90?

If you have ruled out any typos or syntax errors in your code, then it is possible that there is a logical error in your algorithm. One way to check this is to use debugging tools or print statements to trace the flow of your code and see where it might be going wrong. You can also consult with other Fortran programmers or refer to the documentation for help in troubleshooting your algorithm.

## 5. Is there a specific way to combine variables in Fortran 90?

Yes, Fortran has specific syntax for combining variables depending on the data types being used. For example, to add two integers together, you would use the "+" operator, while for strings you would use the "concatenate" function. It is important to use the correct syntax for combining variables to avoid errors like "Combination is not working correctly."

• Programming and Computer Science
Replies
5
Views
3K
• Programming and Computer Science
Replies
4
Views
1K
• Programming and Computer Science
Replies
4
Views
958
• Programming and Computer Science
Replies
5
Views
2K
• Programming and Computer Science
Replies
8
Views
1K
• Programming and Computer Science
Replies
5
Views
22K
• Programming and Computer Science
Replies
12
Views
2K
• Programming and Computer Science
Replies
8
Views
1K
• Programming and Computer Science
Replies
18
Views
1K
• Programming and Computer Science
Replies
10
Views
766