Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int fib( int n );
- int fib_sum( int Start, int End );
- int is_fib_sum( int n );
- int main( void )
- {
- printf("podaj liczbe\n");
- int n;
- scanf("%d",&n);
- if(is_fib_sum(n) == 1)
- {
- printf("%d jest suma conajwyzej 4 liczb fib", n);
- }
- else
- {
- printf("%d nie jest suma conajwyzej 4 liczb fibonacciego.",n);
- }
- return 0;
- }
- int fib( int n )
- {
- if(n == 0)
- {
- return 0;
- }
- else if(n == 1)
- {
- return 1;
- }
- else
- {
- int fprim = 0;
- int f = 1;
- int m;
- for(int i = 2; i <= n; i++)
- {
- m = f + fprim;
- fprim = f;
- f = m;
- }
- return f;
- }
- }
- int fib_sum( int iStart, int iEnd )
- {
- int sum = 0;
- for(int i = iStart; i <= iEnd; i++)
- {
- sum += fib(i);
- }
- return sum;
- }
- int is_fib_sum( int n )
- {
- if(n == 0) return 1;
- int currentIndex = 0;
- while(fib(currentIndex) < n)
- {
- int currentSum = 0;
- for(int i = currentIndex; i <= currentIndex + 3; i++)
- {
- currentSum = fib_sum(currentIndex,currentIndex + i);
- // printf("Porownanie: sumy=%d \t oraz liczby n=%d\n",currentSum,n);
- if(n == currentSum)
- {
- return 1;
- }
- }
- currentIndex++;
- }
- currentIndex = 0;
- while(fib(currentIndex) < n)
- {
- int currentSum = 0;
- for(int i = currentIndex + 3; i > currentIndex; i--)
- {
- currentSum = fib_sum(i,currentIndex + 3);
- //printf("Porownanie: sumy=%d \t oraz liczby n=%d\n",currentSum,n);
- if(n == currentSum)
- {
- return 1;
- }
- }
- currentIndex++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement