Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to add fibonacci numbers to a set in C++

1 Answer

0 votes
#include <iostream>
#include <bits/stdc++.h> 
  
using namespace std;
  
void print(set<int> const &st) {
    copy(st.begin(), st.end(), ostream_iterator<int>(cout, " "));
}
  
set<int> fibonacci() { 
    int max = 300;
    
    int a = 0, b = 1; 
    set<int> fib, arr_fib; 
    fib.insert(a); 
 
    do {
        fib.insert(b); 
        int c = a + b; 
        a = b; 
        b = c; 
    } while (b <= max);
    
    return fib;
} 
    
int main() 
{ 
    // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
    
    set<int> fib = fibonacci(); 
      
    print(fib);
      
    return 0; 
} 
  
  
  
/*
run:
  
0 1 2 3 5 8 13 21 34 55 89 144 233 
  
*/
  

 



answered May 23, 2019 by avibootz
edited May 23, 2019 by avibootz

Related questions

1 answer 182 views
1 answer 185 views
185 views asked May 24, 2019 by avibootz
1 answer 172 views
1 answer 132 views
1 answer 176 views
1 answer 103 views
103 views asked Sep 18, 2022 by avibootz
...