Test if a number is fibonacci

后端 未结 20 879
深忆病人
深忆病人 2020-11-30 17:58

I know how to make the list of the Fibonacci numbers, but i don\'t know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is genera

相关标签:
20条回答
  • 2020-11-30 18:38

    Java solution can be done as below. But still it can be optimized

    The following solution works for

    1. 1≤T≤10 ^5
    2. 1≤N≤10 ^10

    T is no.of test cases, N is range of number

        import java.util.Scanner;
        import java.math.BigDecimal;
        import java.math.RoundingMode;
    
        public class FibonacciTester {
            private static BigDecimal zero = BigDecimal.valueOf(0);
            private static BigDecimal one = BigDecimal.valueOf(1);
            private static BigDecimal two = BigDecimal.valueOf(2);
            private static BigDecimal four = BigDecimal.valueOf(4);
            private static BigDecimal five = BigDecimal.valueOf(5);
    
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int n = sc.nextInt();
                BigDecimal[] inputs = new BigDecimal[n];
                for (int i = 0; i < n; i++) {
                    inputs[i] = sc.nextBigDecimal();
                }
    
                for (int i = 0; i < inputs.length; i++) {
                    if (isFibonacci(inputs[i]))
                        System.out.println("IsFibo");
                    else
                        System.out.println("IsNotFibo");
                }
    
    
            }
    
            public static boolean isFibonacci(BigDecimal num) {
                if (num.compareTo(zero) <= 0) {
                    return false;
                }
    
                BigDecimal base = num.multiply(num).multiply(five);
                BigDecimal possibility1 = base.add(four);
                BigDecimal possibility2 = base.subtract(four);
    
    
                return (isPerfectSquare(possibility1) || isPerfectSquare(possibility2));
            }
    
            public static boolean isPerfectSquare(BigDecimal num) {
                BigDecimal squareRoot = one;
                BigDecimal square = one;
                BigDecimal i = one;
                BigDecimal newSquareRoot;
                int comparison = -1;
    
                while (comparison != 0) {
                    if (comparison < 0) {
                        i = i.multiply(two);
                        newSquareRoot = squareRoot.add(i).setScale(0, RoundingMode.HALF_UP);
                    } else {
                        i = i.divide(two);
                        newSquareRoot = squareRoot.subtract(i).setScale(0, RoundingMode.HALF_UP);
                    }
    
                    if (newSquareRoot.compareTo(squareRoot) == 0) {
                        return false;
                    }
    
                    squareRoot = newSquareRoot;
                    square = squareRoot.multiply(squareRoot);
                    comparison = square.compareTo(num);
                }
    
                return true;
            }
        }
    
    0 讨论(0)
  • 2020-11-30 18:38
    int isfib(int n /* number */, int &pos /* position */)
    {
       if (n == 1)
       {
          pos=2;  // 1 1
          return 1;
       }
       else if (n == 2)
       {
          pos=3;  // 1 1 2
          return 1;
       }
       else
       {
          int m = n /2;
          int p, q, x, y;
          int t1=0, t2 =0;
          for (int i = m; i < n; i++)
          {
            p = i;
            q = n -p;    // p + q = n
            t1 = isfib(p, x);
            if (t1) t2 = isfib(q, y);
            if (t1 && t2 && x == y +1)
            {
               pos = x+1;
               return 1; //true
            }
          }
          pos = -1;
          return 0; //false
       }
    }
    

    How about this?

    0 讨论(0)
提交回复
热议问题