斐波那契數列被定義為從1和1開始的整數序列,其中每一個后續的數值都是前面兩個數值之和。
f(0) = 1
f(1) =1
f(n) = f(n-1) f(n-2) where n>=2
我的目標是計算前100個偶數Fibonacci數字的總和。
到目前為止,我已經找到了這段代碼,它可以完美地計算出偶數的總和到400萬,但是我無法找到編輯代碼的方法,使它停止在第100個數值的總和,而不是達到400萬。
public class Improvement {
public static int Fibonacci(int j){
/**。
*
* 遞回花了很長時間,所以繼續用迭代法。
*
* 復雜度是n的平方......嘗試改進為只有n
*
*/
int tmp。
int a = 2;
int b = 1;
int total = 0;
do {
if(isEven(a)) total =a;
tmp = a b;
b = a;
a = tmp;
} while (a < j);
return total;
}
private static boolean isEven(int a) {
return (a & 1) == 0;
}
public static void main(String[] args){
//注意,這里沒有更多的回圈。
System.out.println(Fibonacci(4_000_000) )。
}
}
只是為了顯示@mr1554代碼答案中的控制臺,顯示了前100個偶數值,然后所有值的總和是4850741640,如下所示:
任何幫助都是值得的。
uj5u.com熱心網友回復:
你需要使用BigInteger,因為long很容易溢位,因為Fibonacci的規模相當容易。BigInteger在檢查是奇數還是偶數方面也很棘手,但你可以使用BigInteger::testBit回傳boolean,正如這個答案所解釋的。
下面是一些完整的代碼:
BigInteger fibonacciSum(int count, boolean isOdd) {
int i = 0;
BigInteger sum = BigInteger.ZERO。
BigInteger current = BigInteger.One;
BigInteger next = BigInteger.one;
BigInteger temp;
while (i < count) {
temp = current;
current = current.add(next);
next = temp;
if ((current.testBit(0) && isOdd) || ((!current.testBit(0) && !isOdd)) {
sum = sum.add(current);
i ;
}
}
return sum;
}
或者你可以用Stream API玩一玩:
BigInteger fibonacciSum(int count, boolean isOdd){
final BigInteger[] firstSecond = new BigInteger[] {BigInteger.ONE, BigInteger.ONE};
return Stream.iterate(
firstSecond,
num -> new BigInteger[] { num[1], num[0] 。 add(num[1] })
.過濾(pair ->
(pair[1].testBit(0) & & isOdd) ||
(!pair[1].testBit(0) && !isOdd) )
.limit(count)
.map(pair -> pair[1] )
.reduce(BigInteger.ZERO, BigInteger::add);
}
無論如何,別忘了測驗一下:
void test() {
assertThat(
fibonacciSum(100, false)。
is(new BigInteger("290905784918002003245752779317049533129517076702883498623284700")));
}
uj5u.com熱心網友回復:
正如我所想的,你想要一個程式來對Fibonacci數列的100個前偶數值進行求和。
這里有一個示例代碼,當你運行這個程式時,它會要求你確定偶數值的數量,你想要100個值,例如,在consul中輸入100:
public static void main(String[] args) {
int firstNumber = 0;
int secondNumber = 2;
System.out.print("輸入斐波那契數列的奇數元素之和 : ")。
Scanner scan = new Scanner(System.in)。
int elementCount = scan.nextInt(); //span> 你想要的偶數值的數量。
System.out.print(firstNumber ", "/span>)。
System.out.print(secondNumber ", ")。
long sum = 2;
for (int i = 2; i < elementCount; i ) {
int nextNumber = firstNumber secondNumber;
System.out.print(nextNumber ", ")。
sum = (nextNumber);
firstNumber = secondNumber;
secondNumber = nextNumber;
}
System.out.print("...")。
System.out.println("
" "the sum of " elementCount " values of fibonacci series is: " sum)。
}
uj5u.com熱心網友回復:
你說。
我的目標是計算前100個偶數Fibonacci數字的總和。
這個數字很快就會變得非常大。你需要:
- 使用BigInteger
。
- 使用BigInteger 。
- 使用mod函式來確定是否為偶數 。
為此我可以從(1,1)開始,但它只有一個術語,所以...
BigInteger m = BigInteger.ZERO。
BigInteger n = BigInteger.one;
BigInteger sumOfEven= BigInteger.ZERO;
int count = 0;
BigInteger t;
while( count < 100) {
t = n.add(m)。
//檢查是否為偶數。
if (t.mod(BigInteger.TWO).eals(BigInteger.ZERO)) {
sumOfEven = sumOfEven.add(t);
count ;
}
n = m;
m = t;
}
System.out.println(sumOfEven)。
印刷品
290905784918002003245752779317049533129517076702883498623284700
另一方面,如果從你的評論來看。
我的目的是要計算前100個偶數的總和
那么你可以這樣做
sumFirstNeven = (((2N 2)N)/2 = (N 1)N 所以 (101)100 = 10100,復雜度為O(1)。轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/309418.html
標籤:

