How do I find the cube and cube root of a number without importing any module in PYTHON?. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 4

How do I find the cube and cube root of a number without importing any module in PYTHON?.

cubes and cube roots

24th Dec 2017, 2:22 AM
Vishwatma Bhat
12 Answers
+ 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?
23rd Jul 2019, 5:28 PM
King Vader
King Vader - avatar
+ 4
x = int(input("Enter number: ")) cube = x**3 cuberoot = x**(1/3) print("Cube: " + cube) print("Cube root: " + cuberoot
24th Dec 2017, 3:04 AM
blackcat1111
blackcat1111 - avatar
+ 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) + ".")
22nd Jul 2021, 2:14 PM
Mendjemo Ngangom Gerard Ledoux
0
if you read the code for those modules it will help you get an understanding of how it is done.
24th Dec 2017, 2:35 AM
emmey
emmey - avatar
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?
24th Dec 2017, 2:49 AM
Vishwatma Bhat
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
19th Aug 2019, 6:23 AM
Rahul Verma
Rahul Verma - avatar
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
27th Sep 2019, 7:55 PM
AJAY LINGAYAT
AJAY LINGAYAT - avatar
0
2. Perform & execute a python program that prints absolute value, square root and cube of a number by importin9 appropriate module.
13th May 2021, 4:48 AM
Tushar Sharma
Tushar Sharma - avatar
- 1
print(9**(1/2))
24th Dec 2017, 2:50 AM
Vishwatma Bhat
- 1
9 is the variable number
24th Dec 2017, 2:51 AM
Vishwatma Bhat
- 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
24th Dec 2017, 4:47 AM
H Chiang
- 2
here is the program to find square root without importing. it works for every number
24th Dec 2017, 2:50 AM
Vishwatma Bhat