📜  红宝石 |整数 sqrt()函数

📅  最后修改于: 2022-05-13 01:55:44.143000             🧑  作者: Mango

红宝石 |整数 sqrt()函数

Ruby中的sqrt()函数返回非负整数n的整数平方根,即小于等于n平方根的最大非负整数

示例 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