簡體   English   中英

是否有可能在恆定時間內計算isFibonacci()?

[英]Is it possible to calculate isFibonacci() in constant time?

這是可能的,描述的創建一個快速“給第n個斐波那契數”的功能在這里 有沒有辦法編寫一個在O(1)中執行的isFibonacci(int i)函數?

我可以預先估算價值觀。 但計算最后O(n),我不能為大數字做。

當且僅當(5 * n 2 + 4)或(5 * n 2 - 4)中的一個或兩個是完美正方形時,數字是斐波那契。

bool isFibonacci(int n) 
{ 
    // n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both 
    // is a perferct square 
    return isPerfectSquare(5*n*n + 4) || 
           isPerfectSquare(5*n*n - 4); 
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM