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

Implementing Factorial using recursion #1370

Closed
wants to merge 11 commits into from

Conversation

sricharan200
Copy link

@sricharan200 sricharan200 commented Nov 15, 2023

Description of Change

References

Checklist

  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes: Hey, I have raised a pull request. This is part of my coursework assignment. Kindly, look into it. Thank you.

Copy link
Collaborator

@realstealthninja realstealthninja left a comment

Choose a reason for hiding this comment

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

thank you for submitting next time please ensure you create separate pull requests for each file change ❤️

displayArray(array5, length5);

return 0;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}
}

return factResults[sequenceNumber];
}

int main() {

This comment was marked as resolved.

@@ -0,0 +1,33 @@
#include <stdio.h>

This comment was marked as resolved.


unsigned long long factResults[MAX_LIMIT] = {0};

unsigned long long computeFactorial(int sequenceNumber) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

using lengthy data types is not recommended
use the _t types from stdint.h

#include<stdint.h>

uint64_t computeFactorial(...)
Suggested change
unsigned long long computeFactorial(int sequenceNumber) {
uint64_t computeFactorial(int sequenceNumber) {

@@ -0,0 +1,33 @@
#include <stdio.h>

This comment was marked as resolved.

int array1[] = {121, 432, 564, 23, 1, 45, 788};
int length1 = sizeof(array1) / sizeof(array1[0]);
int array2[] = {95, 90, 85, 80, 75, 70};
int length2 = sizeof(array2) / sizeof(array2[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
int length2 = sizeof(array2) / sizeof(array2[0]);
int length2 = sizeof(array2) / sizeof(int);

int array2[] = {95, 90, 85, 80, 75, 70};
int length2 = sizeof(array2) / sizeof(array2[0]);
int array3[] = {333, 287, 911, 462, 786};
int length3 = sizeof(array3) / sizeof(array3[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
int length3 = sizeof(array3) / sizeof(array3[0]);
int length3 = sizeof(array3) / sizeof(int);

int array3[] = {333, 287, 911, 462, 786};
int length3 = sizeof(array3) / sizeof(array3[0]);
int array4[] = {5, 3, 8, 1, 2, 9, 4, 7, 6};
int length4 = sizeof(array4) / sizeof(array4[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
int length4 = sizeof(array4) / sizeof(array4[0]);
int length4 = sizeof(array4) / sizeof(int);

int array4[] = {5, 3, 8, 1, 2, 9, 4, 7, 6};
int length4 = sizeof(array4) / sizeof(array4[0]);
int array5[] = {12, 34, 12, 45, 67, 45, 34};
int length5 = sizeof(array5) / sizeof(array5[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
int length5 = sizeof(array5) / sizeof(array5[0]);
int length5 = sizeof(array5) / sizeof(int);

int length4 = sizeof(array4) / sizeof(array4[0]);
int array5[] = {12, 34, 12, 45, 67, 45, 34};
int length5 = sizeof(array5) / sizeof(array5[0]);

Copy link
Collaborator

Choose a reason for hiding this comment

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

if you are going for speed with the test of this algorithm you could just do

float size_of_int = 1/sizeof(int);
// then multiply this value instead of divding over and over again
// remember multiplication is always faster than division
int length1 = sizeof(array1) * size_of_int;
int length2 = sizeof(array2) * size_of_int;
int length3 = sizeof(array3) * size_of_int;
int length4 = sizeof(array4) * size_of_int;
int length5 = sizeof(array5) * size_of_int;

@sricharan200
Copy link
Author

I have made the required changes for factorial recursion.
As I have raised pull request for factorial using recursion. Kindly look into it. Thank you

Copy link
Collaborator

@realstealthninja realstealthninja left a comment

Choose a reason for hiding this comment

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

please mark things youve completed via the resolved button thats below each suggestion

Copy link
Collaborator

Choose a reason for hiding this comment

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

remove this file from this pr

assert(computeFactorial(10) == 3628800);

}

Copy link
Collaborator

Choose a reason for hiding this comment

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

all functions must be documented this is because when doxygen generates documentation
it pulls from your inline documentation

refer to the format below as well as the suggestion:

/*
 * @brief <explanation of function in a sentence>
 * @param <parameter name> <explanation of parameter>
 * @returns <return type>
 */
 type identifier(type param_name) {...}
Suggested change
/*
* @brief main function
* @returns 0 on successful exit
*/

Copy link
Author

Choose a reason for hiding this comment

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

Made all the required changes. Thank you.

- Memoization: https://www.geeksforgeeks.org/memoization-1d-2d-and-3d/
*/

#include <stdio.h>
Copy link
Collaborator

Choose a reason for hiding this comment

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

it would be preferable if you document the headers as well

#include<header.h> /// <reason for using header>

Copy link
Author

Choose a reason for hiding this comment

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

I have made all the required changes. Kindly look into it. Thank you.

Copy link
Author

Choose a reason for hiding this comment

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

@realstealthninja, can you please review the pull request?

Copy link
Collaborator

@realstealthninja realstealthninja left a comment

Choose a reason for hiding this comment

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

very good

@sricharan200
Copy link
Author

@realstealthninja Thanks. Is the pull request merged?

@realstealthninja
Copy link
Collaborator

Sadly not I'm afraid. You see the maintainers are all busy with their lives so we're pretty much stuck until they're able to review pull requests. Please be patient

@sricharan200
Copy link
Author

Okay. As, Github contributions are a part of my coursework assignment. I am supposed to finish it by the first week of December.

@sricharan200
Copy link
Author

Hi, @realstealthninja , Can you please let the other reviewers know about the pull request review?

PS: This is a part of my coursework assignment that counts towards the final grade. This assignment has to be completed by first week of December.

@realstealthninja
Copy link
Collaborator

Will try to get their attention no promises though. Thank you for your contributions

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

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

I do not have the power to merge it, but there are some issues with this code.

#include <stdint.h> /// Standardized integer data types for improved portability.
#include <assert.h> /// Assertions for testing purposes.

#define MAX_LIMIT 200
Copy link
Member

Choose a reason for hiding this comment

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

Why 200? What is the highest input, for which the factorial will fit into uint64_t?

* @param sequenceNumber The non-negative integer for which to compute the factorial.
* @return The factorial of the input integer.
*/
uint64_t computeFactorial(int sequenceNumber) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
uint64_t computeFactorial(int sequenceNumber) {
uint64_t computeFactorial(unsinged sequenceNumber) {

It does not make sense to run computeFactorial(-1), right? Why not to use unisigned as input type?

You can also go for:

uint64_t computeFactorial(const unsinged sequenceNumber) {

Comment on lines +38 to +40
if (sequenceNumber == 0) {
return 1;
}
Copy link
Member

Choose a reason for hiding this comment

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

You are wasting one slot in the factResults.

if (sequenceNumber == 0) {
return 1;
}
if (factResults[sequenceNumber] != 0) {
Copy link
Member

Choose a reason for hiding this comment

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

What will happen if you call computeFactorial(1000) or computeFactorial(-1000)?

if (factResults[sequenceNumber] != 0) {
return factResults[sequenceNumber];
}
factResults[sequenceNumber] = sequenceNumber * computeFactorial(sequenceNumber - 1);
Copy link
Member

Choose a reason for hiding this comment

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

You have a conversion to 'uint64_t' from 'int' may change the sign of the result - this might be much more serious than it looks at first. Either make sequenceNumber unsinged or change this line to:

factResults[sequenceNumber] = (uint64_t)(sequenceNumber) * computeFactorial(sequenceNumber - 1);

/**
* @brief Tests the 'computeFactorial' function by comparing its results with known factorial values.
*/
void testFactorial() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
void testFactorial() {
void testFactorial(void) {

cf. -Wstrict-prototypes.

* @brief Main function to test the factorial calculation.
* @return 0 on success.
*/
int main() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
int main() {
int main(void) {


int testNumber;
testNumber = 5;
printf("The factorial of %d is %llu\n", testNumber, computeFactorial(testNumber));
Copy link
Member

Choose a reason for hiding this comment

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

If you want to be super correct, then change this line to:

Suggested change
printf("The factorial of %d is %llu\n", testNumber, computeFactorial(testNumber));
printf("The factorial of %d is %" PRIu64 "\n", testNumber, computeFactorial(testNumber));

Make sure to:

#include <inttypes.h>


#define MAX_LIMIT 200

uint64_t factResults[MAX_LIMIT] = {0};
Copy link
Member

Choose a reason for hiding this comment

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

Why not to make this a static variable inside computeFactorial?

Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why are you deleting this file?

Copy link
Contributor

This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Dec 29, 2023
Copy link
Contributor

github-actions bot commented Jan 5, 2024

Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel or our Discord server. Thank you for your contributions!

@github-actions github-actions bot closed this Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants