Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factorial Function Implementation #652

Closed
wants to merge 7 commits into from
Closed

Factorial Function Implementation #652

wants to merge 7 commits into from

Conversation

octavianarmasu
Copy link
Contributor

Description

This Rust code provides implementations of the factorial calculation for positive integers, both iteratively and recursively. The code includes unit tests to ensure the correctness of the functions.

Iterative Factorial Calculation (factorial function)

The factorial function uses a straightforward iterative approach. It checks for base cases (0! and 1!) and, for other cases, calculates the factorial using the product of the range from 2 to the given number (inclusive). The product method is used to efficiently calculate the product of the range.

Recursive Factorial Calculation (factorial_recursive function)

The factorial_recursive function employs a recursive approach. It also checks for base cases and recursively calculates the factorial by multiplying the current number with the factorial of (n - 1). This recursive method provides a clear representation of the factorial definition.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant