简体   繁体   English

如何使用皮萨诺周期确定计算斐波那契数和的最后一位数字的范围?

[英]How to determine the range to calculate the last digit of the sum of Fibonacci numbers using Pisano Period?

I already solved this problem.我已经解决了这个问题。 If I understood correctly the logic is the following: since we need the last digit of a sum of Fibonacci numbers we can use:如果我理解正确,逻辑如下:因为我们需要斐波那契数和的最后一位数字,我们可以使用:

𝐹n mod 10 == (𝐹n % Pisano 10) mod 10

My issue is with the range of the sum.我的问题是总和的范围。 I don't know how to find it.我不知道如何找到它。 Since Pisano 10 = 60, the last digits repeat with a period of 60 so I don't need to calculate the Fibonacci values from 0 to N but I don't know how to determine this range.由于 Pisano 10 = 60,最后一位数字以 60 为周期重复,所以我不需要计算从 0 到 N 的斐波那契值,但我不知道如何确定这个范围。

I found solutions for this problem but I never understood how they found their range.我找到了这个问题的解决方案,但我从来不明白他们是如何找到他们的范围的。 For example:例如:

Original here 原文在这里

def sum_fib_last(n):
    if(n <= 1):
        return n

    previous = 0
    current  = 1

    rem = n % 60 #60 is Pisano period of 10

    for i in range(2, rem + 3): 
        
        previous, current = current, (previous + current) % 60
  
    return(current-1) % 10

I don't know why the range is from (2 to rem+3) and why we return the Fibo value minus 1. Similar solution我不知道为什么范围是从 (2 到 rem+3) 以及为什么我们返回斐波那契值减去 1。类似的解决方案

This solution uses a different range:此解决方案使用不同的范围:

def last_digit(n):
    a, b = 0, 1
    for i in range((n + 2) % 60):
        a, b = b, (a + b) % 10
    return 9 if a == 0 else a - 1

I need help to understand how these ranges where determined, I don't understand the logic behind them.我需要帮助来理解这些范围是如何确定的,我不明白它们背后的逻辑。

They should have been a little bit clearer.他们应该更清楚一点。 The sum of the first n Fibonacci numbers is Fib(n + 2) - 1 .前 n 个斐波那契数的总和是Fib(n + 2) - 1 This is easy to prove by induction.这很容易用归纳法证明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 斐波那契数和的最后一位数 - Last Digit of the Sum of Fibonacci Numbers 找到斐波那契数列部分和的最后一位数字 - Finding the last digit of partial sum of Fibonacci series 从 m 到 n 斐波那契数中查找总和的最后一位。 (0 ≤ ≤ 10^14) - Finding last digit of sum from m to n Fibonacci numbers. (0 ≤ 𝑚 ≤ 𝑛 ≤ 10^14) 在给定序列长度和模数的情况下计算 Pisano 周期 - Calculate Pisano period given length of sequence and modulus 如何获取 4 位数字的所有可能组合的列表,其单个数字总和为 13,最后一位数字为 5 - how get list of all the possible combinations of 4 digit numbers whose individual digit sum is 13 and last digit is 5 in that number 尝试使用python计算斐波纳契数的最后一位时,出现错误“ RuntimeWarning:long_scalars中遇到溢出” - I get the error “RuntimeWarning: overflow encountered in long_scalars” when trying to calculate the last digit of a Fibonacci number using python 如何使用Lambda的范围,映射,过滤和归约计算3和5的整数之和 - How to calculate the sum of numbers divisible by 3 and 5 using lambda's range, map, filter and reduce 如何创建一个递归函数来计算斐波那契数 - How to create a recursive function to calculate Fibonacci numbers 斐波纳契数的总和 - Sum of Fibonacci numbers 斐波那契 N 个数之和 - Sum of N numbers in Fibonacci
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM