【问题标题】:How to compute partial sum of Fibonacci series?如何计算斐波那契数列的部分和?
【发布时间】:2018-09-14 01:07:26
【问题描述】:

我正在 Coursera 上学习算法工具箱课程,但我被困在作业的第 7 题上。 需要得到两个给定斐波那契数之间的部分和,实际上问题陈述需要该和的最后一位。

我知道从 0 到 Fn 的和是 Fn+2 - 1

所以 Fx 和 Fy 的部分和是

Fx+2 - Fy+1,对吗?

我将使用 Pisano 数列来获取任何斐波那契数的最后一位,通过获取它与序列长度的模数,到目前为止一切顺利。

但是,当输入为:

9999999999999999 99999999999999999

我的程序输出 4,答案实际上是 6。

我已经检查了表示每个数字需要多少位,并且两者都在 64 位范围内,并且我使用的是无符号 64 位整数。

我不确定这里有什么问题。

#include <iostream>
#include <vector>
using namespace std;

vector<uint64_t> fibList; // Fibonacci numbers List
vector<uint64_t> pisanoSequence; // Pisano Sequence list
void generatePisanoSequence(int mod)
{
    fibList.push_back((*(fibList.end()-1) + *(fibList.end()-2)) % mod); // Get the last digits of the next Fibonacci number depending on the modulp. 
    pisanoSequence.push_back(*(fibList.end()-1)); //Put the last digit of the last Fibonacci number in the Pisano Sequence
    if (*(pisanoSequence.end() - 1) == 1 && *(pisanoSequence.end() - 2) == 0) // If we return to having 0 then 1 as inputs to the Pisano sequence that mean we have reached the end of the period of the sequence
    {
        return; // Stop Generating entries
    }
    else
    {
        generatePisanoSequence(mod); // Calculate the next entry.
    }
}
int main()
{
    fibList.push_back(0); // Put 0 to the Fibonacci sequence
    fibList.push_back(1); // Put 1 to the Fibonacci sequence
    pisanoSequence.push_back(0); // Put 0 to the Pisano Sequence
    pisanoSequence.push_back(1); // Put 1 to the Pisano sequence
    generatePisanoSequence(1000); // An Examplry Modulos of 1000
    uint64_t n, m; // Input Fibonacci numbers
    cin >> n >> m;
    if (m == n) //If the same number entered for both, simply get the last digit of that number/
    {
        m  = m % pisanoSequence.size(); //Find its place in the Pisano sequence
        cout << pisanoSequence[m] % 10; // Get the number and print and its units digits
        return 0;
    }
    if (m > n) swap(m,n); //If m is bigger than n, i.e the second Fibonacci is bigger than the first, swap them.
    n = n + 2; //Get Fn+2
    m = m + 1; //Get Fm+1
    n = n % (pisanoSequence.size()); // Get its position in Pisano Sequence
    m = m % (pisanoSequence.size()); // Get its position in Pisano Sequence
    uint64_t n2 = pisanoSequence[n]; //Get the number
    uint64_t m2 = pisanoSequence[m]; //Get the number
    int64_t z = n2 - m2; //Subtract the numbers to find the partial sum
    z = abs(z); //If negative make it positive because the sum is +ve and the subtraction might yield negative.
    cout << z % 10; // Print the units of the sum

return 0;
}

【问题讨论】:

    标签: c++ sum fibonacci


    【解决方案1】:

    z = abs(z) 错误

    如果你得到-4,那么答案是6,所以应该是z = abs(z)而不是z = (z % 10 + 10) % 10;

    【讨论】:

    • 但是我会检查你的添加。我希望它有效。谢谢!
    • 它吓坏了!你能告诉我推理的路线吗?
    • 你是个天才,先生!
    • @RaafatAbualazm 假设您已经计算了 n2 和 m2 而没有进行取模,例如 m2 = 996n2 = 1023。那么你真正想要的是(n2 - m2) % 10 = 7,但是你已经通过模数做了所有事情,所以你得到m2 = 996n2 = 23(如果mod = 1000)。让我们看看:n2 - m2 = -973z = (n2 - m2) % 10 = -3。但实际上-3 = 7 (mod 10)。我们只需要让它变得积极。我们知道-mod &lt; z &lt; mod。我们也知道(z + mod) % mod = z % mod + mod % mod = z % mod + 0 = z % mod
    • @RaafatAbualazm 其实这似乎是无用的操作,但是0 &lt; z + mod &lt; 2 * mod,所以(z + mod) % mod会一直是正数,会在计算中给我们7
    猜你喜欢
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2017-07-22
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多