summaryrefslogtreecommitdiff
path: root/PlaygroundCpp
diff options
context:
space:
mode:
authorHaidong Ji2018-08-29 21:06:31 -0500
committerHaidong Ji2018-08-29 21:06:31 -0500
commit0144b21da618acf0737d3ee35b2aaa41e2064a27 (patch)
tree472f1b531de8261bcc94ca56b0f66d027d6c72e4 /PlaygroundCpp
parent0c624da482f7941f0b4f6285e2fb48484f862738 (diff)
Partial Fib sum last digit done for C++
This concludes the second module of Algorithmic Design and Techniques from edX. I had a lot of fun doing it. It's challenging and I learned a lot!
Diffstat (limited to 'PlaygroundCpp')
-rw-r--r--PlaygroundCpp/Sources/Playground.cpp57
1 files changed, 25 insertions, 32 deletions
diff --git a/PlaygroundCpp/Sources/Playground.cpp b/PlaygroundCpp/Sources/Playground.cpp
index 4445c37..c737a3c 100644
--- a/PlaygroundCpp/Sources/Playground.cpp
+++ b/PlaygroundCpp/Sources/Playground.cpp
@@ -26,46 +26,39 @@ static int getLastDigit(long n) {
else
return result - 1;
}
+static int getLastDigit(long m, long n) {
+ if (m == n)
+ return getFibNModM(n);
+ else {
+ int sumFn = getLastDigit(n);
+ int sumFm;
+ if (m <= 1)
+ sumFm = 0;
+ else
+ sumFm = getLastDigit(m - 1);
+ if (sumFn >= sumFm)
+ return sumFn - sumFm;
+ else
+ return sumFn - sumFm + 10;
+ }
+}
-//TEST(FibSumLastDigitTest, Zero) {
-// ASSERT_EQ(getLastDigit(0), 0);
-//}
-//TEST(FibSumLastDigitTest, One) {
-// ASSERT_EQ(getLastDigit(1), 1);
-//}
-//TEST(FibSumLastDigitTest, Six) {
-// ASSERT_EQ(getLastDigit(6), 0);
-//}
-//TEST(FibSumLastDigitTest, Nine) {
-// ASSERT_EQ(getLastDigit(9), 8);
-//}
-//TEST(FibSumLastDigitTest, OneHundred) {
-// ASSERT_EQ(getLastDigit(100), 5);
-//}
-
-//TEST(FibLastDigitTest, One) {
-// ASSERT_EQ(getFibNModM(239, 1000), 161);
-//}
-//
-//TEST(FibLastDigitTest, Three) {
-// ASSERT_EQ(getFibNModM(2816213588, 30524), 10249);
-//}
-
-//TEST(FibLastDigitTest, Forty) {
-// ASSERT_EQ(1344, getGCD(1344, 1344));
+//TEST(FibPartialSumLastDigit, Five_Five) {
+// ASSERT_EQ(getLastDigit(5, 5), 5);
//}
//
-//TEST(FibLastDigitTest, ThreeThreeOne) {
-// ASSERT_EQ(4, getGCD(14159572, 63967072));
+//TEST(FibPartialSumLastDigit, Ten_200) {
+// ASSERT_EQ(getLastDigit(10, 200), 2);
//}
//
-//TEST(FibLastDigitTest, ReverseAB) {
-// ASSERT_EQ(4, getGCD(14159572, 63967072));
+//TEST(FibPartialSumLastDigit, One_2) {
+// ASSERT_EQ(getLastDigit(1, 2), 2);
//}
int main() {
- long n;
+ long m, n;
+ std::cin >> m;
std::cin >> n;
- int c = getLastDigit(n);
+ int c = getLastDigit(m, n);
std::cout << c << '\n';
}