How to calculate the series 1 2 3 6 9 18 27 54 81 162 243 ... N in C++

2 Answers

0 votes
// 1 2 3 6 9 18 27 54 81 162 243 ... N 
  
// (1) (2) 1 * 3 (3) 2 * 3 (6) : 3 * 3 (9) 6 * 3 (18) :
// 9 * 3 (27) 18 * 3 (54) : 27 * 3 (81) 54 * 3 (162) :
// 81 * 3 (243)
  
#include <iostream>
  
int main()
{
    int a = 1, b = 2, total = 11;
      
    std::cout << a << " " << b << " ";
      
    for (int i = 3; i <= total; i++) {
        if (i % 2 == 1) {
            a = a * 3;
            std::cout << a << " ";
        }
        else {
            b = b * 3;
            std::cout << b << " ";
        }
    }
      
    return 0;
}
  
  
 
  
/*
run:
  
1 2 3 6 9 18 27 54 81 162 243 
  
*/

 



answered Mar 30, 2022 by avibootz
0 votes
// 1 2 3 6 9 18 27 54 81 162 243 ... N 
 
// (1) 1 + 1 (2) : 1 + 2 (3) : 3 + 3 (6) : 3 + 6 (9) :
// 9 + 9 (18) : 9 + 18 (27) : 27 + 27 (54) : 27 + 54 (81) :
// 81 + 81 (162) : 81 + 162 (243)
 
#include <iostream>
 
int main()
{
    int a = 1, b = 0, total = 11;
 
    for (int i = 1; i <= total; i++) {
        if (i % 2 == 1) {
            b = a + a;
            std::cout << a << " ";
        }
        else {
            a = a + b;
             std::cout << b << " ";
        }
    }
 
    return 0;
}
 
 
 
 
/*
run:
 
1 2 3 6 9 18 27 54 81 162 243  
 
*/

 



answered Mar 30, 2022 by avibootz

Related questions

2 answers 289 views
1 answer 215 views
2 answers 248 views
2 answers 233 views
2 answers 237 views
1 answer 159 views
...