summaryrefslogtreecommitdiff
path: root/Homework/cs3460/nbonacci
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
commit6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch)
treeed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs3460/nbonacci
downloadmisc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.tar.gz
misc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.zip
Diffstat (limited to 'Homework/cs3460/nbonacci')
-rw-r--r--Homework/cs3460/nbonacci/Assignment1.cpp74
-rw-r--r--Homework/cs3460/nbonacci/a.outbin0 -> 56434 bytes
2 files changed, 74 insertions, 0 deletions
diff --git a/Homework/cs3460/nbonacci/Assignment1.cpp b/Homework/cs3460/nbonacci/Assignment1.cpp
new file mode 100644
index 0000000..20f7769
--- /dev/null
+++ b/Homework/cs3460/nbonacci/Assignment1.cpp
@@ -0,0 +1,74 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+const double RATIO_THRESHOLD = 0.000001;
+const unsigned int DISPLAY_ITERATIONS = 20;
+
+/*
+ @param series: unsigned int, the n-bonacci series "degree" (2 -> fib, 3 -> trib)
+ @param n: unsigned int, the term to find
+ @return long: the nth term of the series-degree n-bonacci series
+*/
+long nbonacci(unsigned int series, unsigned int n)
+{
+ if (n <= series)
+ return 1;
+
+ long sum = 0;
+ for (unsigned int i = 1; i <= series; ++i)
+ sum += nbonacci(series, n - i);
+
+ return sum;
+}
+
+/*
+ @param title: std::string, the name of the series
+ @param series: unsigned int, the degree of the n-bonacci series
+ @return void: print the ratio of the series to stdout
+*/
+void computeNbonacciRatio(std::string title, unsigned int series)
+{
+ unsigned int i = series+1;
+ long prev = 1;
+ long curr = nbonacci(series, i); // The first series numbers are one's
+ double currRatio = static_cast<double>(curr) / static_cast<double>(prev);
+ double prevRatio;
+ do
+ {
+ prevRatio = currRatio;
+ prev = curr;
+ curr = nbonacci(series, ++i);
+ currRatio = static_cast<double>(curr) / static_cast<double>(prev);
+ } while (std::abs(currRatio - prevRatio) > RATIO_THRESHOLD);
+
+ // i-1 because the nth number in the sequence corresponds with the (n-1)'th iteration
+ std::cout << title << " ratio approches " << currRatio << " after " << i-1 << " iterations." << std::endl;
+}
+
+int main()
+{
+ std::vector<std::string> names;
+ names.push_back("Fibonacci");
+ names.push_back("Tribonacci");
+ names.push_back("Fourbonacci");
+ names.push_back("Fivebonacci");
+
+ unsigned int i;
+ for (i = 0; i < names.size(); i++)
+ {
+ std::cout << "--- " << names.at(i) << " ---" << std::endl;
+ for (unsigned int n = 1; n < i+2; n++)
+ std::cout << "1 ";
+ for (unsigned int n = i+2; n <= DISPLAY_ITERATIONS; n++)
+ std::cout << nbonacci(i+2, n) << " ";
+ std::cout << std::endl;
+ }
+
+ std::cout << std::endl;
+
+ for (i = 0; i < names.size(); i++)
+ computeNbonacciRatio(names.at(i), i+2);
+
+ return 0;
+}
diff --git a/Homework/cs3460/nbonacci/a.out b/Homework/cs3460/nbonacci/a.out
new file mode 100644
index 0000000..3bd8426
--- /dev/null
+++ b/Homework/cs3460/nbonacci/a.out
Binary files differ