Go語言程式:從有理數中獲取分母
在本文中,我們將討論如何從有理數中獲取分母。
有理數 - 在數學中,有理數定義為可以表示為a/b形式的數,其中a和b是整數。例如- 1/3, 5/8 等。
請注意,有理數的分母永遠不能為零。
語法
func NewRat(a, b int64) *Rat func (x *Rat) Denom() *Int
NewRat() 函式以兩個整數作為輸入引數,並返回a/b形式的有理數。其中a是有理數的分子,b是分母。
Go語言中的Denom()函式返回有理數的分母,結果為整數格式。
方法一
Go語言程式:從有理數中獲取分母。
演算法
步驟1 - 匯入fmt和big包。
步驟2 - 呼叫main()函式。
步驟3 - 初始化一個變數來儲存有理數。使用big.NewRat()建立有理數。
步驟4 - 使用denom()函式獲取有理數的分母。
步驟5 - 儲存結果並將其列印到螢幕上
示例
使用庫函式獲取有理數分母的原始碼如下:
// Including the main package
package main
// Importing fmt and math/big
import (
"fmt"
"math/big"
)
// fmt package allows us to print anything on the screen
// big defined in math package allows us to use various predefined methods like Denom()
// Calling main
func main() {
fmt.Println("Golang Program to get the denomenator of a rational number")
// NewRat creates a new Rational number with numerator a and denominator b
number := big.NewRat(6, 8)
// Printing the result on the screen
fmt.Println("The rational number is", number)
// getting the denomenator of the rational number
// storing the result in a result variable
result := number.Denom()
// printing the denomenator on the screen
fmt.Println("The denomenator of rational number", number, "is: ", result)
}
輸出
Golang Program to get the denomenator of a rational number The rational number is 3/4 The denomenator of rational number 3/4 is: 4
描述
首先,我們需要匯入fmt包,它允許我們列印任何內容,以及big包,以便使用各種預定義的方法。
呼叫main()函式。
透過向函式提供兩個輸入作為引數,使用big包中定義的NewRat()函式建立一個有理數。這些輸入是這樣給出的:第一個作為有理數的分子,第二個作為其分母。
然後將數字儲存在一個名為number的變數中。
現在我們需要獲取此數字的分母並將其列印到螢幕上。
要獲取分母,請使用big包中提供的Denom()函式。此函式以int格式返回分母作為結果。
將結果值儲存在一個變數中。
使用println()函式將結果列印到螢幕上。
方法二
Go語言程式:使用兩個函式從有理數中獲取分母。
演算法
步驟1 - 匯入fmt、big和rand包。
步驟2 - 建立getDenominator()函式。
步驟3 - 呼叫main()函式。
步驟4 - 呼叫getDenominator()函式。
步驟5 - 初始化一個變數來儲存其中的有理數。使用big.NewRat()建立有理數。
步驟6 - 使用denom()函式獲取有理數的分母。
步驟7 - 返回此值。
步驟8 - 儲存結果並將其列印到螢幕上。
示例
使用兩個函式獲取有理數分母的原始碼如下:
// Including the main package
package main
// Importing fmt and math/big
import (
"fmt"
"math/big"
"math/rand"
)
// fmt package allows us to print anything on the screen.
// big package allows us to use various predefined mathematical methods like rat
// rand package allows us to generate random numbers.
// Initializing the getDenominator() function.
func getDenominator() *big.Int {
// NewRat creates a new Rational number with numerator a and denominator b
number := big.NewRat(int64(rand.Intn(200)), int64(rand.Intn(200)))
// Printing the result
fmt.Println("The rational number is", number)
// getting the denomenator of the rational number
// storing the result in a result variable
result := number.Denom()
// returning the result
return result
}
// Calling main
func main() {
fmt.Println("Golang Program to get the denomenator of a rational number")
// calling the getDenominator() function
result := getDenominator()
// printing the denomenator on the screen
fmt.Println("The denomenator of rational number is:", result)
}
輸出
Golang Program to get the denomenator of a rational number The rational number is 27/29 The denomenator of rational number is: 29
描述
匯入fmt、big和rand包。
fmt包允許我們將任何內容列印到螢幕上。big包允許我們使用各種預定義的數學方法,例如rat,而rand包允許我們生成隨機數。
建立和定義getDenominator()函式。此函式將生成有理數並返回其分母。
在這裡,我們使用NewRat()方法生成有理數。我們使用RandInt()函式生成高達200的隨機整數值,這些值充當以分子和分母形式提供給此函式的輸入。
我們將以此方式生成的32位int數轉換為64位,因為NewRat()接受64位值作為輸入。
使用Denom()函式獲取分母的值並將結果儲存在一個變數中。
返回此值。
使用fmt.Printf()函式將最終結果列印到螢幕上。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP