查找数字是否在斐波那契范围内

时间:2015-08-11 10:21:31

标签: c# numbers position range fibonacci

我已经打印了一些Fibonacci数字。现在我想查看一个数字/我输入/是否在该范围内,如果是,则显示它的位置。 这就是我到目前为止所做的:

using System;

namespace SomeFibonacciPrimes
{
    class SomeFibonacciPrimes
    {
        static void Main()
        {
            int first = 0;
            int second = 1;
            int third = 1;
            for (int i = 0; i < 50; i++)
            {
                third = second;
                second = first + second;
                first = third;
                Console.WriteLine(second);                      
            }
            Console.WriteLine("Enter a number to find if it's in Fibonacci      range:");
            int number = int.Parse(Console.ReadLine());
            if (number == second)
            {
                Console.WriteLine("Your number is within the Fibonacci range.");
            }
            else
            {
                Console.WriteLine("Your number is NOT within the Fibonacci range");
            }
       }
   }
}

如果输入我在范围内看到的数字,我无法理解为什么If语句会打印else结果?! 我认为在我设法使If语句起作用之后,这个位置就是&#34; i&#34;在for语句中?

2 个答案:

答案 0 :(得分:2)

我建议您使用array of integerList of integer来解决此问题:如下所示:

            int first = 0;
            int second = 1;
            int third = 1;
            List<int> fibnoList= new List<int>();                  
            for (int i = 0; i < 50; i++)
            {
                fibnoList.Add(second);  
                Console.WriteLine(second); //To print the series  
                third = second;
                second = first + second;
                first = third;                                    
            }
            Console.WriteLine("Enter a number to find if it's in Fibonacci      range:");
            int number = int.Parse(Console.ReadLine());
            if (fibnoList.Contains(number))
            {
                Console.WriteLine("Your number is within the Fibonacci range.");
            }
            else
            {
                Console.WriteLine("Your number is NOT within the Fibonacci range");
            }

答案 1 :(得分:0)

除了使用循环,也许你可以考虑这个

using System;

namespace SomeFibonacciPrimes
{
    class SomeFibonacciPrimes
    {
        static void Main()
        {
            Console.WriteLine("Enter a number to find if it's in Fibonacci      range:");
            int number = int.Parse(Console.ReadLine());
            if (IsFibonacci(number))
            {
                Console.WriteLine("Your number is within the Fibonacci range.");
            }
            else
            {
                Console.WriteLine("Your number is NOT within the Fibonacci range");
            }
       }




        static bool IsFibonacci(int number)
        {
            //Uses a closed form solution for the fibonacci number calculation.
            //http://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression

            double fi = (1 + Math.Sqrt(5)) / 2.0; //Golden ratio
            int n = (int) Math.Floor(Math.Log(number * Math.Sqrt(5) + 0.5, fi)); //Find's the index (n) of the given number in the fibonacci sequence

            int actualFibonacciNumber = (int)Math.Floor(Math.Pow(fi, n) / Math.Sqrt(5) + 0.5); //Finds the actual number corresponding to given index (n)

            return actualFibonacciNumber == number;
        }
    }
}