Advertisement
Guest User

Fibonacci

a guest
May 26th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int rozmiar = 1000;
  4. int tab [2][rozmiar];
  5.  
  6. int main(){
  7.  
  8. cout<<"Witaj w programie przenaczonym do liczenia kolejnych liczb Fibonacciego.\nWpisz, do ktorego elementu ciagu chcialbys generowac kolejne elementy ciagu.\nUwaga:Nie da sie wygenerowac wiecej niz 10^18 liczb!\nDominik Matuszek 2019.\n";
  9. long long a;
  10. start:
  11. cin>>a;
  12. cout<<"\n";
  13. if(a>=0){
  14. cout<<"f(0)="<<0<<"\n";
  15. }
  16. else{
  17.     cout<<"Numery ciagu oznaczamy od liczby 0! Podaj inna liczbe:\n";
  18.     goto start;
  19. }
  20. if(a>0){
  21. cout<<"f(1)="<<1<<"\n";
  22. }
  23. if(a>1){
  24. cout<<"f(2)="<<1<<"\n";
  25. }
  26.  
  27. int maksz=0;
  28. tab[0][rozmiar]=1;
  29. tab[1][rozmiar]=1;
  30.  
  31.  
  32. for(unsigned long long i=3;i<a+1;i++){
  33.    for(int j=rozmiar-maksz;j<=rozmiar;j++){
  34.     tab[i%2][j]=tab[(i-1)%2][j]+tab[i%2][j];
  35.    }
  36.  
  37.     for(int j=rozmiar;j>0;j--){
  38.         if(tab[i%2][j]>9){
  39.             //cout<<"rozmiar"<<rozmiar-j+1<<endl;
  40.             maksz=max(maksz,rozmiar-j+1);
  41.             tab[i%2][j-1]++;
  42.             tab[i%2][j]=tab[i%2][j]-10;
  43.         }
  44.  
  45.     }
  46.     cout<<"f("<<i<<")=";
  47.      for(int j=rozmiar-maksz;j<=rozmiar;j++){
  48.         if(tab[i%2][j]>9){
  49.             cout<<"Shit";
  50.             return 0;
  51.         }
  52.         cout<<tab[i%2][j];
  53.     }
  54.     cout<<"\n";
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement