通过用户输入找到所有偶数斐波那契数的总和
Find the sum of all even Fibonacci numbers, with user input
目前我正在编写一个程序,通过用户输入在 Windows 表单应用程序 (WPA) 中查找 even
斐波纳契数。
当我执行我的程序时,我得到了与我拥有的测试数据不同的数据。
例如,当我输入 100,000
时,我得到的输出是 5500034
但它应该是 60696.
我的程序代码如下:
int val1 = 1;
int val2 = 2;
Int64 evenTerms = 2;
val2 = int.Parse(textBox3.Text);
while (val2 < 5000000)
{
int temp = val1;
val1 = val2;
val2 = temp + val2;
if (val2 % 2 == 0)
{
evenTerms += val2;
}
}
MessageBox.Show("" + val2);
谁能帮我解决这个问题?
谢谢。
据我了解你的问题(问题不清楚),希望这个解决方案有效:)
int val1 = 0;
int val2 = 1;
Int64 evenTerms = 0;
int val3 = int.Parse(textBox3.Text), val4 = 0, temp;
if (val3 < 5000000)
{
while (val4 < val3){
temp = val1 + val2;
val1 = val2;
val2 = temp;
if (temp % 2 == 0)
{
evenTerms += 1;
}
val4++;
}
}
MessageBox.Show("" + evenTerms);
好吧,第一个斐波那契以 1,1,2,3,.... 开头,这意味着您比列表领先一步。您应该从 val1=1 和 val2=1;
开始
https://en.wikipedia.org/wiki/Fibonacci_number
那你为什么要使用你的输入参数作为你计算的一部分?!!
我建议使用 generator 枚举 all 斐波那契数列:
public static IEnumerable<long> FiboGen() {
long left = 0;
long right = 1;
yield return left;
yield return right;
while (true) {
long result = left + right;
yield return result;
left = right;
right = result;
}
}
然后 Linq 仅总结 所需的值:
int limit = int.Parse(textBox3.Text);
// 60696 for the 1000000 limit
// 4613732 for the 5000000 limit
var result = FiboGen() // take Fibonacci numbers
.Where(val => val % 2 == 0) // but only even ones
.TakeWhile(val => val < limit) // and less than limit
.Sum(); // finally sum them up.
MessageBox.Show(result.ToString());
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
typedef unsigned long ulong;
ulong fib(ulong a, ulong b, ulong * odd_z, ulong n) {
ulong c = a + b;
if((c+b) >= n) { return 0; }
if(a%2 == 0) { *odd_z+=(b+c); }
return fib(b,c,odd_z, n);
}
int T;
scanf("%d",&T);
ulong odd_z = 0;
ulong *sum = &odd_z;
while(T--) {
ulong N;
scanf("%lu",&N);
fib(0,1,&odd_z, N);
printf("%lu\n",*sum);
*sum=0;
}
return 0;
}
这个算法也更省时 space 高效
目前我正在编写一个程序,通过用户输入在 Windows 表单应用程序 (WPA) 中查找 even
斐波纳契数。
当我执行我的程序时,我得到了与我拥有的测试数据不同的数据。
例如,当我输入 100,000
时,我得到的输出是 5500034
但它应该是 60696.
我的程序代码如下:
int val1 = 1;
int val2 = 2;
Int64 evenTerms = 2;
val2 = int.Parse(textBox3.Text);
while (val2 < 5000000)
{
int temp = val1;
val1 = val2;
val2 = temp + val2;
if (val2 % 2 == 0)
{
evenTerms += val2;
}
}
MessageBox.Show("" + val2);
谁能帮我解决这个问题?
谢谢。
据我了解你的问题(问题不清楚),希望这个解决方案有效:)
int val1 = 0;
int val2 = 1;
Int64 evenTerms = 0;
int val3 = int.Parse(textBox3.Text), val4 = 0, temp;
if (val3 < 5000000)
{
while (val4 < val3){
temp = val1 + val2;
val1 = val2;
val2 = temp;
if (temp % 2 == 0)
{
evenTerms += 1;
}
val4++;
}
}
MessageBox.Show("" + evenTerms);
好吧,第一个斐波那契以 1,1,2,3,.... 开头,这意味着您比列表领先一步。您应该从 val1=1 和 val2=1;
开始https://en.wikipedia.org/wiki/Fibonacci_number
那你为什么要使用你的输入参数作为你计算的一部分?!!
我建议使用 generator 枚举 all 斐波那契数列:
public static IEnumerable<long> FiboGen() {
long left = 0;
long right = 1;
yield return left;
yield return right;
while (true) {
long result = left + right;
yield return result;
left = right;
right = result;
}
}
然后 Linq 仅总结 所需的值:
int limit = int.Parse(textBox3.Text);
// 60696 for the 1000000 limit
// 4613732 for the 5000000 limit
var result = FiboGen() // take Fibonacci numbers
.Where(val => val % 2 == 0) // but only even ones
.TakeWhile(val => val < limit) // and less than limit
.Sum(); // finally sum them up.
MessageBox.Show(result.ToString());
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
typedef unsigned long ulong;
ulong fib(ulong a, ulong b, ulong * odd_z, ulong n) {
ulong c = a + b;
if((c+b) >= n) { return 0; }
if(a%2 == 0) { *odd_z+=(b+c); }
return fib(b,c,odd_z, n);
}
int T;
scanf("%d",&T);
ulong odd_z = 0;
ulong *sum = &odd_z;
while(T--) {
ulong N;
scanf("%lu",&N);
fib(0,1,&odd_z, N);
printf("%lu\n",*sum);
*sum=0;
}
return 0;
}
这个算法也更省时 space 高效