What is the issue with this Fortran recursion problem?

In summary: REAL. This will let you use larger integers.In summary, the problem you are having is that you are not using recursion in your program, and you need to use a stack pointer in order to keep track of all the values. The expert you talked to said you need to implement a stack pointer, but you don't understand him. Can somebody point out your mistakes or make suggestions about your program?
  • #1
hassamu
1
0
Hi everybody.
I am learning Fortran on my own, and I started doing simple examples like factorials, iterations, and stuff like that.
The problem comes in recursion, I'm trying to run this simple factorial program using recursion, but the outcome is completely wrong.
I asked to an expert in Fortran, but he said that I need to implement a stack pointer in order to keep track of all the values of my function, but I didn't understand him , and he is no more available this time.
Can somebody point out my mistakes or make suggestions about my program?
Thank you
Code:
recursive function factorial(myNumber) result (temp)
integer temp, myNumber
if(myNumber<=0) then
        temp = 1
        return
else
        temp = myNumber*factorial(myNumber-1)
        return
end if
end function factorial

program recursiveFactorial
      integer elNumero, resultado
      resultado = 1
      elNumero = 0
      write(*,*) 'Enter a positive integer:'
      read(*,*) elNumero
      resultado=factorial(elNumero)
      write(*,*) 'X!=',resultado
end program recursiveFactorial
 
Technology news on Phys.org
  • #2
hassamu said:
Hi everybody.
I am learning Fortran on my own, and I started doing simple examples like factorials, iterations, and stuff like that.
The problem comes in recursion, I'm trying to run this simple factorial program using recursion, but the outcome is completely wrong.
I asked to an expert in Fortran, but he said that I need to implement a stack pointer in order to keep track of all the values of my function, but I didn't understand him , and he is no more available this time.
Can somebody point out my mistakes or make suggestions about my program?
Thank you
Code:
recursive function factorial(myNumber) result (temp)
integer temp, myNumber
if(myNumber<=0) then
        temp = 1
        return
else
        temp = myNumber*factorial(myNumber-1)
        return
end if
end function factorial

program recursiveFactorial
      integer elNumero, resultado
      resultado = 1
      elNumero = 0
      write(*,*) 'Enter a positive integer:'
      read(*,*) elNumero
      resultado=factorial(elNumero)
      write(*,*) 'X!=',resultado
end program recursiveFactorial

Caveat: I haven't done much Fortran for about a dozen years. Since you are getting some results, your compiler must adhere to the Fortran 90 (or later) standard. Recursion isn't supported in Fortran 77.

Here's a link to some recursion examples in Fortran 90: http://math.scu.edu/~dsmolars/ma60/notesforrec.html.

Your code looks OK to me except for one line in your recursive function. I would change this:
Code:
if(myNumber <= 0) then

to this:
Code:
if(myNumber == 1) then
 
  • #3
hassamu said:
Hi everybody.
I am learning Fortran on my own, and I started doing simple examples like factorials, iterations, and stuff like that.
The problem comes in recursion, I'm trying to run this simple factorial program using recursion, but the outcome is completely wrong.
I asked to an expert in Fortran, but he said that I need to implement a stack pointer in order to keep track of all the values of my function, but I didn't understand him , and he is no more available this time.
Can somebody point out my mistakes or make suggestions about my program?
Thank you
Code:
recursive function factorial(myNumber) result (temp)
integer temp, myNumber
if(myNumber<=0) then
        temp = 1
        return
else
        temp = myNumber*factorial(myNumber-1)
        return
end if
end function factorial

program recursiveFactorial
      integer elNumero, resultado
      resultado = 1
      elNumero = 0
      write(*,*) 'Enter a positive integer:'
      read(*,*) elNumero
      resultado=factorial(elNumero)
      write(*,*) 'X!=',resultado
end program recursiveFactorial

A problem here is that you have not used an IMPLICIT NONE statement in your program and function to turn off implicit typing. Here "factorial" in your main program is a real quantity, but it is an integer in your function. You need to have:

Code:
integer factorial
or

Code:
integer,external::factorial
in your main program.

But you should always have IMPLICT NONE in both your main program, and all subroutines.


Also, you don't mention what kind of problems you are having, but by using the default integer you are restricting yourself to only small values for elNumero (for example, up to 12 for my compiler). If you go past this limit you will not get the answer, you'll just get garbage. You can choose a different KIND value for the integer to extend the range of the integers; for example, for my compiler you could use:

Code:
integer(kind=8):: elNumero, resultado
and similarly for all the other integer declarations. (Your compiler may use a different number than 8. )


To see what the largest value is that your variable can hold, you could add as a temporary line:

Code:
 print*,'The largest number here is ',huge(resultado)
right before the END PROGRAM line. At some point, for very large numbers, you might want to consider changing your program variables from integers to reals.
 
  • #4
Mark44 said:
Caveat: I haven't done much Fortran for about a dozen years. Since you are getting some results, your compiler must adhere to the Fortran 90 (or later) standard. Recursion isn't supported in Fortran 77.

Here's a link to some recursion examples in Fortran 90: http://math.scu.edu/~dsmolars/ma60/notesforrec.html.

Your code looks OK to me except for one line in your recursive function. I would change this:
Code:
if(myNumber <= 0) then

to this:
Code:
if(myNumber == 1) then

No, that is wrong, because factorial 0 (written 0!) is 1.
Your modification would cause an infinite loop for an argument of 0
and for any negative value.
The original test is correct.
 
  • #5
hassamu said:
Hi everybody.
I am learning Fortran on my own, and I started doing simple examples like factorials, iterations, and stuff like that.
The problem comes in recursion, I'm trying to run this simple factorial program using recursion, but the outcome is completely wrong.
I asked to an expert in Fortran, but he said that I need to implement a stack pointer in order to keep track of all the values of my function, but I didn't understand him , and he is no more available this time.
Can somebody point out my mistakes or make suggestions about my program?
Thank you
Code:
recursive function factorial(myNumber) result (temp)
integer temp, myNumber
if(myNumber<=0) then
        temp = 1
        return
else
        temp = myNumber*factorial(myNumber-1)
        return
end if
end function factorial

program recursiveFactorial
      integer elNumero, resultado
      resultado = 1
      elNumero = 0
      write(*,*) 'Enter a positive integer:'
      read(*,*) elNumero
      resultado=factorial(elNumero)
      write(*,*) 'X!=',resultado
end program recursiveFactorial

You should reorganize your code thus:
Code:
program recursiveFactorial
      IMPLICIT NONE
      integer elNumero, resultado
      resultado = 1
      elNumero = 0
      write(*,*) 'Enter a positive integer:'
      read(*,*) elNumero
      resultado=factorial(elNumero)
      write(*,*) 'X!=',resultado

CONTAINS

recursive function factorial(myNumber) result (temp)
integer temp, myNumber
if(myNumber<=0) then
        temp = 1
        return
else
        temp = myNumber*factorial(myNumber-1)
        return
end if
end function factorial

end program recursiveFactorial
 
  • #6
Louisa said:
No, that is wrong, because factorial 0 (written 0!) is 1.
Your modification would cause an infinite loop for an argument of 0
and for any negative value.
The original test is correct.
My point was that it was silly to check for negative numbers. If the input value was 0 or 1, you could return 1 as a special case, but for values greater than 1, you could use recursion.
 
  • #7
Louisa said:
You should reorganize your code thus:
Code:
program recursiveFactorial
      IMPLICIT NONE
      integer elNumero, resultado
      resultado = 1
      elNumero = 0
      write(*,*) 'Enter a positive integer:'
      read(*,*) elNumero
      resultado=factorial(elNumero)
      write(*,*) 'X!=',resultado

CONTAINS

recursive function factorial(myNumber) result (temp)
integer temp, myNumber
if(myNumber<=0) then
        temp = 1
        return
else
        temp = myNumber*factorial(myNumber-1)
        return
end if
end function factorial

end program recursiveFactorial

There is a problem with this code as well. If you pass a negative number to the factorial function, it will return 1, and that is incorrect.
 
  • #8
Mark44 said:
My point was that it was silly to check for negative numbers. If the input value was 0 or 1, you could return 1 as a special case, but for values greater than 1, you could use recursion.

It is necessary to test for negative numbers.
If the recursive routine is presented with a negative argument,
it will loop forever.
 
  • #9
Mark44 said:
There is a problem with this code as well. If you pass a negative number to the factorial function, it will return 1, and that is incorrect.

It is essential to have a test for negative.
Either there needs to be an error trap, or the routine returns a value, say 1.
If there is no test for zero, an infinite loop occurs when the
function is presented with a negative argument.
 

Related to What is the issue with this Fortran recursion problem?

What is a Fortran recursion problem?

A Fortran recursion problem is a type of programming problem that involves using the recursion technique in the Fortran programming language. Recursion is a programming technique where a function calls itself repeatedly until a specific condition is met.

Why is it important to understand Fortran recursion?

Understanding Fortran recursion is important because it allows programmers to solve complex problems more efficiently. Recursion can help break down a problem into smaller, more manageable parts and can also lead to more elegant and concise code.

What are some common challenges when working with Fortran recursion?

Some common challenges when working with Fortran recursion include understanding how the recursive function will be executed, properly defining the base case, and avoiding infinite loops.

How can I debug a Fortran recursion problem?

Debugging a Fortran recursion problem can be done by using print statements to track the flow of the recursive function and identify any errors. It can also be helpful to use a debugger tool or step through the code line by line to see how the function is being executed.

What are some best practices for using Fortran recursion?

Some best practices for using Fortran recursion include understanding the problem thoroughly before attempting to use recursion, defining a clear base case, and making sure the recursive function has a clear and efficient termination condition. It is also important to avoid using recursion for problems that can be solved more efficiently using other techniques.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
62
Views
10K
  • Programming and Computer Science
Replies
4
Views
748
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top