0

Help me solving this code

We have a Boolean function which give true as an output when there exists any x^i= n. Where i and n are given and false otherwise! Write a program without help of math library except .pow

19th Nov 2021, 9:37 PM
sam
4 odpowiedzi
+ 2
Do you want to be helped or to solve the problem for you?
19th Nov 2021, 9:54 PM
Solo
Solo - avatar
+ 2
#include<iostream> #include<cmath> using namespace std; bool isPow(int x, int i, int n){ return pow(x,i) == n; } int main() { if (isPow(3, 2, 9)) cout<<"true"; else cout<<"false"; return 0; } https://code.sololearn.com/cp0yUnjBL9EQ
19th Nov 2021, 11:50 PM
SoloProg
SoloProg - avatar
+ 1
you don't say anything about variable x so I wrote code consider that x, i and n are given #include<iostream> #include<cmath> using namespace std; bool f(int x,int i,int n){ return pow(x,i) == n; } int main(){ return 0; }
19th Nov 2021, 10:19 PM
Zein Khalil
0
The isPow method aims to investigate whether there is a strictly positive basis for the parameters nand ia strictly positive basisx∈N+x \in \mathbb{N}^{+}x∈N+ so that: xi=n x^{i} = nxi=n. Does such a thing existxxx, we give it trueback, otherwise false. The method only handles non-negative ones i, ie if ithis criterion is not met, the default value is falsereturned. A (naive) iterative implementation is sufficient for this task. You can find the function in the template so that you can implement this without having to import the power function from Java’s own library pow. Use this method to calculate the power:pOw(a,b)=ab pow(a, b) = a^{b}pow(a,b)=ab Here is an example: Be i=3i = 3i=3 and n=8n = 8n=8. isPow(3, 8)are trueback, asxi=23=8=n x^{i} = 2^{3} = 8 = nxi=23=8=n. Thus there is a strictly positive onex=2x=2x=2. Entries withn≤109n \ leq 10 ^ 9n≤109 and i≤10i \ leq 10i≤10 must be treated correctly and efficiently.
19th Nov 2021, 10:27 PM
sam