Finding the closest fibonacci numbers

前端 未结 11 2146

I am trying to solve a bigger problem, and I think that an important part of the program is spent on inefficient computations.

I need to compute for a given number N, th

11条回答
  •  执笔经年
    2021-02-02 14:21

    A number is Fibonacci if and only if one or both of (5*n^2 + 4) or (5*n^2 – 4) is a perfect square. I am using this premise to verify if the input number belongs to a fibonacci series or not.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    typedef struct node{
    
        int64_t value;
        struct node *next;
    
    }Node;
    
    Node *head ;
    
    void readElements(int);
    int isPerfectSquare(int64_t sqrValue);
    
    int main(){
    
        int input_count , flag=0;
        Node *temp_node = NULL;
        int64_t sqrValue = 0;
    
        scanf("%d" , &input_count);
    
        if((input_count < 1 )||(input_count > 100000)){
            printf("Total number of Inputs out of Range ..!!\n");
            return 1;
        }
    
        readElements(input_count);
    
        /*Reading the elements from the list*/
    
        temp_node = head;
    
        while(temp_node != NULL){
    
            sqrValue = 5*pow(temp_node->value , 2);
            flag = (isPerfectSquare(sqrValue+4) || isPerfectSquare(sqrValue-4));
    
            if(flag == 1){
                printf("IsFibo\n");
            }
            else{
                printf("IsNotFibo\n");
            }
    
            temp_node = temp_node->next;
    
        }   
    
    
    
        return 0;
    
    }
    
    
    void readElements(int input_count){
    
        int temp = 0;
        int64_t val = 0;
        Node *temp_node =NULL , *cur = NULL;
        char b[20];
    
    
        while (temp < input_count) {
    
            scanf("%s" , b);
            val = atol(b);
    
            if(val < 0 || val >10000000000)
                continue;
    
            temp_node = (Node*) malloc(sizeof(Node));
    
            temp_node->value = val;
            temp_node->next = NULL;
    
            if(head == NULL){
                head = cur = temp_node;
            }
            else{
                cur->next = temp_node;
                cur = temp_node;
            }
    
            temp++;
    
        }
    
    }
    
    int isPerfectSquare(int64_t sqrValue){
    
        int64_t s = 0;
    
        s = sqrt(sqrValue);
    
        return(s*s == sqrValue);
    
    }
    

提交回复
热议问题