Understanding C# Method for Calculating Factorials

  • Context: C# 
  • Thread starter Thread starter Chestermiller
  • Start date Start date
  • Tags Tags
    Method
Click For Summary

Discussion Overview

The discussion revolves around a C# method for calculating factorials, focusing on the concept of recursion and its implementation in programming. Participants explore the logic behind recursive calls, compare it with iterative approaches, and share insights on handling edge cases in factorial calculations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses confusion about the recursive nature of the C# factorial method and questions the logical sequence of operations involved.
  • Another participant confirms that the method uses a "recursive" call, noting that recursive algorithms are favored for their elegance.
  • Some participants highlight the importance of handling the base case (n=0) in recursive functions, with one noting that the provided implementation does not account for this and may lead to overflow.
  • There is a discussion about the cost of recursion compared to iterative solutions, with some suggesting that recursion can be more understandable in certain contexts.
  • One participant shares a print statement example to illustrate the flow of recursion in a similar function, emphasizing the importance of tracking entry and exit points in recursive calls.
  • Several participants engage in a light-hearted exchange about the challenges of understanding recursive functions, including a humorous remark about stack overflow.
  • There are suggestions for code modifications to correctly handle the base case of factorial calculations, with some participants discussing formatting issues related to code tags in the forum.

Areas of Agreement / Disagreement

Participants generally agree on the significance of handling the base case in recursive functions, but there are differing views on the efficiency of recursion versus iteration. The discussion remains unresolved regarding the optimal implementation of the factorial function.

Contextual Notes

Some participants mention limitations in the original implementation, such as not defining 0! = 1, which could lead to incorrect results. There is also a discussion about the implications of using recursion in programming languages that may not support it as effectively as others.

Who May Find This Useful

This discussion may be useful for individuals learning C# programming, particularly those interested in understanding recursion and its applications in algorithms, as well as those transitioning from languages like Fortran.

  • #61
So, monadic constructs are things I really didn't get when I was looking at Haskell. The "types" of problems I was playing with at the time were computer algebra type things, vector spaces, groups and such. Just trying to learn. I came away feeling okay, cool, what do I get from this effort?
 
  • Like
Likes   Reactions: sysprog
Technology news on Phys.org
  • #62
Paul Colby said:
So, monadic constructs are things I really didn't get when I was looking at Haskell. The "types" of problems I was playing with at the time were computer algebra type things, vector spaces, groups and such. Just trying to learn. I came away feeling okay, cool, what do I get from this effort?
I would just use MATLAB (or Mathematica/Wolfram) for most of that. For one-offs, my TI-83 is usually more than adequate for my purposes. If I need to do, e.g., a computationally intensive set of Fourier transforms, I'll use routines out of the Fortran library.

Most of my programming work is in IBM mainframe OS interfacing, and most of that is done in assembly language. Some of it is fixing other people's code, in whichever language they wrote it in.

On the PC, I prefer to use assembly language over using high-level languages, but I'll work with whatever best gets the job done. Subject to the condition that I prefer to code in languages I already know, in general, if I have a choice, I try to use whichever language I think is best for the problem I'm dealing with.
 
Last edited:
  • #63
I just reinstalled Haskell, I'm holding you directly responsible.
 
  • Haha
Likes   Reactions: sysprog
  • #64
The plus side of using functional languages (real functional languages, without side-effects, not languages that just look functional) is referential transparency. If you convince yourself that an expression always evaluates to some particular value, you can replace it by that value, and your program will work the same way. That's a powerful way to reason about programs.
 
  • Like
Likes   Reactions: Nugatory and sysprog
  • #65
For @Chestermiller or anybody else, there's a nice book by RB Whittaker I forgot its name on C# so far looks like a great book, and also a website with solutions to the problems in it.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
22
Views
5K