Advertisement
Wojtekd

Fibonacci

Feb 24th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int fib( int n );
  4. int fib_sum( int Start, int End );
  5. int is_fib_sum( int n );
  6.  
  7. int main( void )
  8. {
  9.         printf("podaj liczbe\n");
  10.         int n;
  11.         scanf("%d",&n);
  12.        
  13.         if(is_fib_sum(n) == 1)
  14.         {
  15.             printf("%d jest suma conajwyzej 4 liczb fib", n);
  16.         }
  17.         else
  18.         {
  19.             printf("%d nie jest suma conajwyzej 4 liczb fibonacciego.",n);
  20.         }      
  21.  
  22.         return 0;
  23. }
  24. int fib( int n )
  25. {
  26.         if(n == 0)
  27.         {
  28.                 return 0;
  29.         }
  30.         else if(n == 1)
  31.         {
  32.                 return 1;
  33.         }
  34.         else
  35.         {
  36.                 int fprim = 0;
  37.                 int f = 1;
  38.                
  39.                 int m;
  40.                
  41.                 for(int i = 2; i <= n; i++)
  42.                 {
  43.                         m = f + fprim;
  44.                         fprim = f;
  45.                         f = m;
  46.                 }
  47.                 return f;
  48.         }      
  49. }
  50. int fib_sum( int iStart, int iEnd )
  51. {
  52.         int sum = 0;
  53.         for(int i = iStart; i <= iEnd; i++)
  54.         {
  55.                 sum += fib(i);
  56.         }      
  57.         return sum;
  58. }
  59. int is_fib_sum( int n )
  60. {  
  61.     if(n == 0) return 1;
  62.  
  63.     int currentIndex = 0;
  64.    
  65.     while(fib(currentIndex) < n)
  66.     {
  67.         int currentSum = 0;
  68.         for(int i = currentIndex; i <= currentIndex + 3; i++)
  69.         {
  70.                 currentSum = fib_sum(currentIndex,currentIndex + i);
  71.                
  72.                // printf("Porownanie: sumy=%d \t oraz liczby n=%d\n",currentSum,n);
  73.                 if(n == currentSum)
  74.                 {
  75.                     return 1;
  76.                 }
  77.         }  
  78.         currentIndex++;
  79.     }  
  80.     currentIndex = 0;
  81.    
  82.     while(fib(currentIndex) < n)
  83.     {
  84.  
  85.         int currentSum = 0;
  86.         for(int i = currentIndex + 3; i > currentIndex; i--)
  87.         {
  88.                 currentSum = fib_sum(i,currentIndex + 3);
  89.                
  90.                 //printf("Porownanie: sumy=%d \t oraz liczby n=%d\n",currentSum,n);
  91.                 if(n == currentSum)
  92.                 {
  93.                     return 1;
  94.                 }
  95.         }  
  96.         currentIndex++;
  97.     }  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement