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

 

aboutME

I am John Fan Zhang, a data analyst and finance researcher. I hold a PhD in finance, CFA charter and full membership of CFA New Zealand Society. I have fifteen-year experience in corporate investment and eight-year experience in advanced data analysis. My research focuses on the effect of social psychology (culture) on financial decisions. Finance research involves heaps of data analyses that lead me to the data field. I am a Microsoft Certified Solutions Expert (MCSE): Data Management and Analytics (Excel, Power BI, and SQL). Aside from Excel, Power BI and SQL, I am also familiar with econometric tools such as Stata, Eviews, and MATLAB. I use OX and Python for programming. I am an active data community event participant, volunteer, speaker, moderator, program reviewer, including PASS Marathon 2020, Global AI BootCamp Auckland 2019, SQL Saturday Auckland (2017, 2018, 2019), and Definity Conference (2018, 2019, 2020, Auckland, New Zealand).

Auckland, New Zealand

  • Google Site
  • Twitter
  • LinkedIn

©2016 BY JOHN'S DATA STORY

bottom of page