Power of 3 | Practice | GeeksforGeeks

Given a number N, the task is to count the numbers in the range [1, N] which can be represented as power of 3.

Note: 1 can be represented as the power of 3. 
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains single integer N.

Output: For each test case, print the answer

Constraints:
1. 1 <= T <= 104
2. 1 <= N <= 109

Example:
Input:
2
6
1
Output:
2
1
+2
chechipresh3 months ago
C++ solution:
#include <iostream>using namespace std;
class Solution{   public:  long long int count(long long int n) {      long long int i,ans=0;      for (i=1;i<=n;i=i*3){          ans++;      }      return ans;          }};
int main() {   int t;   cin>>t;   long long int n,ans;   while (t--) {       cin>>n;       Solution ob;       ans=ob.count(n);       cout<<ans<<endl;   }   return 0;}    
+1
ankitparashxr3 months ago
java
Scanner sc = new Scanner(System.in);  int t =sc.nextInt();  while(t!=0)  {      int n = sc.nextInt();      int count = 0;      for(int i = 0;i<n;i++)      {          int mul = (int)Math.pow(3,i);          if(mul<=n)          {              count++;          }          else if(mul>n)          {              break;          }      }      System.out.println(count);      t--;
0
chessnoobdj4 months ago
C++ O(1)
Size of pref is 20 only, so lower_bound and binary search both these operations cost O(1) time only
#include<iostream>
#include<algorithm>

const long MAX = 1e10;
int pref[20]; // 10^9 = 3^k (k~20)

void precompute(){
    long int num = 1, i = 0;
    while(num < MAX){
        pref[i] = num;
        i += 1;
        num *= 3;
    }
}

int main(){
    int t;
    std::cin >> t;
    while(t--){
        precompute();
        int n;
        std::cin >> n;
        int ans = std::lower_bound(pref, pref+20, n)-pref;
        if(std::binary_search(pref, pref+20, n))
            ans += 1;
        std::cout << ans << std::endl;
    }
}
0
himanshujain4574 months ago
CORRECTED:
#include <iostream>using namespace std;#include<bits/stdc++.h>void solve(){   int n;   cin>>n;   int i=0;   int temp=0;   vector<int>v;   while(temp<=n)   {       temp=pow(3,i);       v.push_back(temp);       i++;   }   cout<<v.size()-1<<endl;}int main() {int t;cin>>t;while(t--)solve();return 0;}
-1
shubhanshud96 months ago
#include <iostream>using namespace std;
int main() {    int t;    cin>>t;    int count=1;    while(t--)    {        int n;        cin>>n;        int count=1;        for(int i=3;i<=n;i=i*3)        {            count++;        }        cout<<count<<endl;    }return 0;}
0
e20040807 months ago
runtime: 0.1/7.6
import math
for i in range(int(input())):
    print(int(math.log(int(input()), 3))+1)
0
msaravanansara4747 months ago
for _ in range(int(input())):
    N=int(input())
    co=0
    total=0
    while 3**co<=N:
        total+=1
        co+=1
    print(total)
-1
pawan16087 months ago
Here the logic is n ≤3 to the power of x....Here x means number of digit who 3 to the power up to n.
#include<bits/stdc++.h>
using namespace std;

int main() {
	//code
	int t; cin>>t;
	while(t--){
	    int n; cin>>n;
	    n = log(n)/log(3);
	    cout<<n+1<<endl;
	}
	return 0;
}
-2
ankitsinha1112008 months ago
0
Manish Patil10 months ago
Manish Patil
#include <bits stdc++.h="">using namespace std;
int main() {
int T;cin>>T;
while(T--){       int N;    cin>>N;
    cout<<int(log(n) log(3)="" +="" 1)<<endl;="" }="" return="" 0;="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.