English, asked by hema438, 2 months ago

find the program given an integer n, return true if it is a power of nine. otherwise,return false.an integer n is power of nine,if there exists an integer X such that n==9^x​

Answers

Answered by tripathiakshita48
0

Answer:

Here is the C++ code on return true if it is a power of nine. otherwise,return false.an integer n is power of nine

Explanation:

#include <iostream>

using namespace std;

#include<math.h>

int main()

{

int b = 81;

int a = 3;

// computing power

double p = log10(b) / log10(a);

// checking to see if power is an integer or not

if (p - (int)p == 0) {

 cout<<"YES";

}

else{

 cout<<"NO";

}

return 0;

}

Given a positive integer, write a function to find if it is a power of three or not.

Examples:

Input : 3

Output :Yes

Input :6

Output :No

Time Complexity: O(1)

Space Complexity: O(1)

Recursive approach :

Check if the number is divisible by 3, if yes then keep checking the same for number/3 recursively. If the number can be reduced to 1, then the number is divisible by 3 else not.

Time Complexity: O(log3n), where n represents the given integer.

Auxiliary Space: O(log3n).

Approach:

The logic is very simple. Any integer number other than power of 3 which divides highest power of 3 value that integer can hold 3^19 = 1162261467 (Assuming that integers are stored using 32 bits) will give reminder non-zero

Step 1: If the given number, n, is not ending with 3,9,7 or 1, it means that the number is not a power of three, therefore return FALSE.

Step 2 : If not, we create a Map with 4 entries in it in order to maintain the mapping between the powers to three(1,2,3,4) and the number’s last digits(3,9,7,1).

Step 3 : Extract the last digit from a given number and look up it’s corresponding power in the map.

Step 4 :  If this power when raised to three equals  the number, n, return TRUE.

Step 5 : If this power raised to three is less than the number, n, increment the power straight by 4 and loop step 4 until the power raised to three becomes more than n.  

Step 6 : If the power raised to three becomes more than the given number, return FALSE.

See more:

https://brainly.in/question/24087060

#SPJ1

Similar questions