-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Conversation
There was a problem hiding this 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 ❤️
sorting/radix_sort.c
Outdated
displayArray(array5, length5); | ||
|
||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | |
} | |
return factResults[sequenceNumber]; | ||
} | ||
|
||
int main() { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
math/factorial_recursion.c
Outdated
@@ -0,0 +1,33 @@ | |||
#include <stdio.h> |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
math/factorial_recursion.c
Outdated
|
||
unsigned long long factResults[MAX_LIMIT] = {0}; | ||
|
||
unsigned long long computeFactorial(int sequenceNumber) { |
There was a problem hiding this comment.
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(...)
unsigned long long computeFactorial(int sequenceNumber) { | |
uint64_t computeFactorial(int sequenceNumber) { | |
math/factorial_recursion.c
Outdated
@@ -0,0 +1,33 @@ | |||
#include <stdio.h> |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
sorting/radix_sort.c
Outdated
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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int length2 = sizeof(array2) / sizeof(array2[0]); | |
int length2 = sizeof(array2) / sizeof(int); | |
sorting/radix_sort.c
Outdated
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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int length3 = sizeof(array3) / sizeof(array3[0]); | |
int length3 = sizeof(array3) / sizeof(int); | |
sorting/radix_sort.c
Outdated
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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int length4 = sizeof(array4) / sizeof(array4[0]); | |
int length4 = sizeof(array4) / sizeof(int); | |
sorting/radix_sort.c
Outdated
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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int length5 = sizeof(array5) / sizeof(array5[0]); | |
int length5 = sizeof(array5) / sizeof(int); | |
sorting/radix_sort.c
Outdated
int length4 = sizeof(array4) / sizeof(array4[0]); | ||
int array5[] = {12, 34, 12, 45, 67, 45, 34}; | ||
int length5 = sizeof(array5) / sizeof(array5[0]); | ||
|
There was a problem hiding this comment.
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;
I have made the required changes for factorial recursion. |
There was a problem hiding this 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
sorting/radix_sort.c
Outdated
There was a problem hiding this comment.
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); | ||
|
||
} | ||
|
There was a problem hiding this comment.
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) {...}
/* | |
* @brief main function | |
* @returns 0 on successful exit | |
*/ |
There was a problem hiding this comment.
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.
math/factorial_recursion.c
Outdated
- Memoization: https://www.geeksforgeeks.org/memoization-1d-2d-and-3d/ | ||
*/ | ||
|
||
#include <stdio.h> |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very good
@realstealthninja Thanks. Is the pull request merged? |
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 |
Okay. As, Github contributions are a part of my coursework assignment. I am supposed to finish it by the first week of December. |
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. |
Will try to get their attention no promises though. Thank you for your contributions |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) {
if (sequenceNumber == 0) { | ||
return 1; | ||
} |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void testFactorial() { | |
void testFactorial(void) { |
cf. -Wstrict-prototypes.
* @brief Main function to test the factorial calculation. | ||
* @return 0 on success. | ||
*/ | ||
int main() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int main() { | |
int main(void) { |
|
||
int testNumber; | ||
testNumber = 5; | ||
printf("The factorial of %d is %llu\n", testNumber, computeFactorial(testNumber)); |
There was a problem hiding this comment.
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:
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}; |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
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. |
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! |
Description of Change
References
Checklist
Notes: Hey, I have raised a pull request. This is part of my coursework assignment. Kindly, look into it. Thank you.