top of page

sqrt() and int()

sqrt() function is an inbuilt function in Python programming language that returns the square root of any number.

Syntax:

math.sqrt(x)

Parameter:

x is any number such that x>=0

Returns:

It returns the square root of the number passed in the parameter.

Reference: https://www.geeksforgeeks.org/python-math-function-sqrt

 

The int() function not only can change float to integer, but also simply truncates the number at the decimal point.

 

My code:

blocks = int(input("Enter the number of blocks: ")) import math # height = (math.sqrt(8*blocks+1)-1)/2 # Write your code here. height = int(height) # print("The height of the pyramid:", height)

######if we want the float one, we just write

######height = (math.sqrt(8*blocks+1)-1)//2

######and delete height = int(height)

 

Test Data

Sample input: 6

Expected output: The height of the pyramid: 3

Sample input: 20

Expected output: The height of the pyramid: 5

Sample input: 1000

Expected output: The height of the pyramid: 44

Sample input: 2

Expected output: The height of the pyramid: 1

 

Reference:

https://stackoverflow.com/questions/6569528/python-float-to-int-conversion

Mathematics reference:

https://www.xarg.org/puzzle/codingame/pyramid-height

bottom of page