aboutsummaryrefslogtreecommitdiff
path: root/Project1/Homework3.cpp
diff options
context:
space:
mode:
authorConnor McDowell <[email protected]>2024-03-10 15:53:34 -0700
committerConnor McDowell <[email protected]>2024-03-10 15:53:34 -0700
commit72ba0e98936d04a24773c1954837054bef8a3e70 (patch)
treeae2ebcdc3d3c67e54baf2ae3c61e6400e42e99ca /Project1/Homework3.cpp
parentmade fibonacci(int n) and powerfunc(int m, int n) recursive (diff)
downloadhomework-3-connormcdowell275-main.tar.xz
homework-3-connormcdowell275-main.zip
size_t comments createdHEADmain
Diffstat (limited to 'Project1/Homework3.cpp')
-rw-r--r--Project1/Homework3.cpp8
1 files changed, 8 insertions, 0 deletions
diff --git a/Project1/Homework3.cpp b/Project1/Homework3.cpp
index 9d0f11e..1555419 100644
--- a/Project1/Homework3.cpp
+++ b/Project1/Homework3.cpp
@@ -40,6 +40,9 @@ int main()
int factorial(int f)
{
+ // factorial equations with numbers as requested by the input cout statements will always be positive, and can get quite large.
+ // in this case, an unsigned number can be used as the sign of the number will always be positive and to prevent the memory from stacking up,
+ // using a size_t (unsigned long long) will prevent that, and in terms of efficiency, it is as fast as the int type.
if (f == 0 || f == 1)
return 1;
long calc = f * factorial(f - 1);
@@ -48,6 +51,9 @@ int factorial(int f)
int fibonacci(int n)
{
+ // for the same as above, though the int type is incredibly fast, the fibonacci sequence is all positive numbers, thus they dont need a sign
+ // and for above as well, the fibonacci sequence gets quite large incredibly quickly, and using a size_t will prevent hitting the number cap that
+ // int types have.
int a = 0, b = 1, c, i;
if (n == 0 || n == 1)
{
@@ -61,6 +67,8 @@ int fibonacci(int n)
int powerfunc(int m, int p)
{
+ // though a sign can be useful in higher level applications for powers, in all actuality, a negative number only needs a sign on odd power values.
+ // for the even powers conversion to a size_t will reduce space and increase the number count.
if(p != 0)
{
return(m * powerfunc(m, p - 1));