红宝石 |整数 sqrt()函数
Ruby中的sqrt()函数返回非负整数n的整数平方根,即小于等于n平方根的最大非负整数
Syntax: Integer.sqrt(number)
Parameter: The function takes the integer whose square root is to be returned. It throws an error “out of domain” if a negative number is passed.
Return Value: The function returns the integer square root.
示例 1 :
#Ruby program for sqrt() function
#Initializing the number
num1 = 25 num2 = 16 num3 = 100 num4 = 5
#Prints the sqrt of a number
puts Integer.sqrt(num1)
puts Integer.sqrt(num2)
puts Integer.sqrt(num3)
puts Integer.sqrt(num4)
输出:
5
4
10
2
示例 2 :
#Ruby program for sqrt() function
#Initializing the number
num1 = 64 num2 = 81 num3 = 49 num4 = 36
#Prints the sqrt of a number
puts Integer.sqrt(num1)
puts Integer.sqrt(num2)
puts Integer.sqrt(num3)
puts Integer.sqrt(num4)
输出:
8
9
7
6
参考:https://devdocs.io/ruby~2.5/integer#method-c-sqrt