- 4
How do I find the cube and cube root of a number without importing any module in PYTHON?.
cubes and cube roots
12 Respuestas
+ 7
The creators of Python have made all the modules and functions for the user's convenience. What is the need to find the cube and cube root without them (Functions and Modules), if they
are making it easier for you?
+ 4
x = int(input("Enter number: "))
cube = x**3
cuberoot = x**(1/3)
print("Cube: " + cube)
print("Cube root: " + cuberoot
+ 1
cube:
val = 8
### If value is positive ###
cube = val**3
cuberoot = val ** 1/3
print("Cube of 8 is: " + str(cube) + ". Cube root of 8 is: " + str(cuberoot) + ".")
### If value is negative ###
val = -8
cube = (val)**3 #still thesame
cuberoot = ((-1 * val) ** 1/3) * (-1)
#Remark the -1s. If you try without, value will be incorrect.
print("Cube of -8 is: " + str(cube) + ". Cube root of -8 is: " + str(cuberoot) + ".")
0
if you read the code for those modules it will help you get an understanding of how it is done.
0
I think you can find the square and square root of a number without importing modules. so, is there a way to find the cube roots without importing?
0
a=1
n=int(input())
while True:
b=n/(a*a)
# print (a,b)
if abs(b-a)<0.001: # increase the zeros if you want more than 2 digit
print("cube root of {} is :{}".format(n,round(b,2)))
break
a=(2*a+b)/3
0
def cuberoot():
ans=int(input("Enter a Number : "))
for i in range(0,ans):
ans2=i*i*i
ans3=i*i
if ans2==ans:
ans4=ans/ans3
print()
print("Cube root of",ans,"=",ans4)
cuberoot()
101% working
0
2. Perform & execute a python program that prints absolute value, square root and cube of a number by importin9 appropriate module.
- 1
print(9**(1/2))
- 1
9 is the variable number
- 1
If you had no modules that would be tough. However nothing is impossible with the primitive types, someone had to do it for the modules that you are using.
I remember seeing it in a Calculus class but I don’t recall all the details. But if I remember correctly the quickest way to narrow down your answer is to use the definition of square and cube.
Let’s say you had 1537:
You know x must be > 10 since 10^2=100 but < 40 since 40^2 = 1600
30^2=900 so it is 30 something. You can do binary to narrow it down. e.g. 35^2= 1225<1537 then try 37^2= 1419 hence it is around 38 and 39
38^2= 1444 and 39^2= 1521. Now you know it is 39.x
Similar for cube:
1537 >10^3 = 1000 and < 20^3=8000
15^3 = 3375 > 1537 then 13^3 because it is between 10 and 15 = 2197 > hence you now know it is around 11 and 12
11^3= 1331 < 1537 > 1728. Hence it is 11.x
Another way to aproach num = abcdef
Is to figure out how many 0’s
Assume “a” is 1 and rest are 0’s
Then we have 100000 = 5 0’s
We divide the number of 0’s by the root
Sq is 5/2=2 hence 100^2 =10000. Dividing the number by 100^2 we get 10 or ab then we can figure x^2 is around ab
Cube is 5/3=1 hence we get 10^3 = 1000. If we do something similar to above we get 100 or abc then similar to above we need x^3 is around abc
5root is 5/5=1 hence we know 10^5 is 100000 but a > 1 means x^2 <= a
6root is 5/6=0, 7^6 > 100000>6^6
Hence you need a function that does what the above does.
Func int findtenxsq (num){
x = 10;
x2=x*x;
While x2<num{
x=x*10;
x2=x*x;
}
return x;
}
Func int narrowdwnsq (num){
Sm=0;
Big= findtenxsq(num);
Mid=(Sm+Big)/2;
M2=Mid*Mid
Do{
If M2 < num{
Sm=Mid;
}else{
Big=Mid;
}
Mid=(Sm+Big)/2;
M2=Mid*Mid
}while ((M2< num)&& (Sm!=Big))
Return Mid
}
Similar for cube root
- 2
here is the program to find square root without importing. it works for every number