0

I have a problem to solve. First I have to check if the input n (positive integer) is a degree of three and if it's not, then to find a number k such that |n-2^k| is the smallest possible (if there are more than one k which satisfy the condition to find all of them). I did the first part but I don't know how to do the other. I think I have to use else, but I'm stuck. (I'm sorry if the code is ugly/not well written, I'm a beginner.)

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int n, m, countd = 0, k;
    do {
        cout << "Enter a positive integer: "; cin >> n;
    } while (n < 1);
    m = n;
    while (m % 3 == 0) {
        m /= 3;
        countd++;
    }if (m == 1) {
        cout << "3^" << countd << " = " << n << endl;
    }

    return 0;
}

0 Answers0