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, Combinations & Decimal_To_Fraction Functions #654

Merged
merged 16 commits into from
Jan 10, 2024

Conversation

octavianarmasu
Copy link
Contributor

@octavianarmasu octavianarmasu commented Jan 9, 2024

Factorial

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.

Combinations

Implemented a Rust version of the combinations function, which calculates the number of combinations of k elements from a set of n elements. The function now panics when provided with negative values for n or k, ensuring that only positive values are accepted.

  • The combinations function follows the algorithm described in the Wikipedia article on Combinations.
  • Three unit tests are included to verify the correctness of the function for specific input values.

Decimal to fraction

The decimal_to_fraction function in Rust converts decimal numbers to their simplest fraction form. It takes a floating-point number as input and returns a tuple representing the numerator and denominator of the simplified fraction.

  • The function handles both integer and decimal input, ensuring that the output is in its simplest fraction form.
  • If the input is already an integer, the function returns the integer and 1 as the denominator.
  • For decimal input, it calculates the fractional part, determines the number of decimal places, and converts the decimal to an integer.
  • Euclid's algorithm is then applied to find the greatest common divisor (GCD) of the numerator and denominator.
  • The function reduces the fraction by dividing both the numerator and denominator by the GCD, providing the simplest form of the fraction.

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.

@codecov-commenter
Copy link

codecov-commenter commented Jan 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (cb3b21d) 94.57% compared to head (a8ce62f) 94.64%.
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #654      +/-   ##
==========================================
+ Coverage   94.57%   94.64%   +0.06%     
==========================================
  Files         285      288       +3     
  Lines       22906    23062     +156     
==========================================
+ Hits        21664    21826     +162     
+ Misses       1242     1236       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@octavianarmasu octavianarmasu marked this pull request as ready for review January 9, 2024 14:14
@octavianarmasu octavianarmasu changed the title Factorial Function Implementation Factorial and Combinations Functions Jan 9, 2024
@octavianarmasu octavianarmasu changed the title Factorial and Combinations Functions Factorial, Combinations & Decimal_To_Fraction Functions Jan 9, 2024
Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The factorial function is already implemented in this repository, could you merge it with your implementation?

@octavianarmasu
Copy link
Contributor Author

Do you mean the fast_factorial function in the big_integer module?
I found it, it was in hello_bigmath.rs, I am going to merge it with my code.

@siriak
Copy link
Member

siriak commented Jan 10, 2024

Could you remove the factorial in hello_bigmath? It's duplicated there now

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit f3dda8d into TheAlgorithms:master Jan 10, 2024
4 checks passed
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.

3 participants