Anonymous user
Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Continued fraction/Arithmetic/Construct from rational number (view source)
Revision as of 14:47, 27 June 2018
, 5 years ago→{{header|Go}}: Update Go to match other (upcoming) Continued_fraction/* tasks.
(→{{header|Go}}: Add Go solution) |
(→{{header|Go}}: Update Go to match other (upcoming) Continued_fraction/* tasks.) |
||
Line 568:
=={{header|Go}}==
File <code>cf.go</code>:
<lang Go>package cf
import (
"fmt"
"strings"
)
// ContinuedFraction is a regular continued fraction.
type ContinuedFraction func() NextFn
// NextFn is a function/closure that can return
// a posibly infinite sequence of values.
type NextFn func() (term int64, ok bool)
// String implements fmt.Stringer.
// It formats a maximum of 20 values, ending the
// sequence with ", ..." if the sequence is longer.
func (cf ContinuedFraction) String() string {
var buf strings.Builder
buf.WriteByte('[')
sep := "; "
const maxTerms = 20
t, ok := next()
if !ok {
break
}
buf.WriteString(sep)
sep = ", "
}
if n >= maxTerms {
buf.WriteString("...")
break
}
fmt.Fprint(&buf, t)
}
buf.WriteByte(']')
return buf.String()
}
// Sqrt2 is the continued fraction for √2, [1; 2, 2, 2, ...].
func Sqrt2() NextFn {
first := true
return func() (int64, bool) {
if first {
first = false
return 1, true
}
return 2, true
}
}
// Phi is the continued fraction for ϕ, [1; 1, 1, 1, ...].
func Phi() NextFn {
return func() (int64, bool) { return 1, true }
}
// E is the continued fraction for e,
// [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, ...].
func E() NextFn {
var i int
return func() (int64, bool) {
i++
switch {
case i == 1:
return 2, true
case i%3 == 0:
return int64(i/3) * 2, true
default:
return 1, true
}
}
}</lang>
File <code>rat.go</code>:
<lang Go>package cf
import "fmt"
// A Rat represents a quotient N/D.
type Rat struct {
N, D int64
}
// String implements fmt.Stringer and returns a string
// representation of `r` in the form "N/D" (even if D == 1).
func (r Rat) String() string {
return fmt.Sprintf("%d/%d", r.N, r.D)
}
// As ContinuedFraction returns a contined fraction representation of `r`.
func (r Rat) AsContinuedFraction() ContinuedFraction { return r.CFTerms }
func (r Rat) CFTerms() NextFn {
return func() (int64, bool) {
}
q := r.N / r.D
r.N, r.D = r.D, r.N-q*r.D
return q, true
}
}
// Rosetta Code task explicitly asked for this function
// so here it is. We'll just use the types above instead.
func r2cf(n1, n2 int64) ContinuedFraction { return Rat{n1, n2}.CFTerms }</lang>
File <code>rat_test.go</code>:
<lang Go>package cf
import (
"fmt"
"math"
)
func
cases := [...]Rat{
{1, 2},
Line 653 ⟶ 701:
approx float64
cf ContinuedFraction
d1, d2 int64
}{
{"√2", math.Sqrt2, Sqrt2, 1e4, 1e8},
{"π", math.Pi, nil, 10, 1e10},
{"ϕ", math.Phi, Phi, 10, 1e5},
{"e", math.E, E, 1e5, 1e9},
} {
fmt.Printf("\nApproximating %s ≅ %v:\n", tc.name, tc.approx)
for d :=
n := int64(math.
r := Rat{n, d}
fmt.Println(r, "=", r.AsContinuedFraction())
}
if tc.cf != nil {
wid := int(math.Log10(float64(tc.d2)))*2 + 2 // ick
fmt.Printf("%*s: %v\n", wid, "Actual", tc.cf)
}
}
// Output:
// [… commented output used by go test omitted for
// Rosetta Code listing; it is the same as below …]
}</lang>
{{out}}
Line 679 ⟶ 734:
Approximating √2 ≅ 1.4142135623730951:
14142/10000 = [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 = [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
Approximating π ≅ 3.141592653589793:
31/10 = [3; 10]
314/100 = [3; 7, 7]
314159/100000 = [3; 7, 15, 1, 25, 1, 7, 4]
314159265/100000000 = [3; 7, 15, 1, 288, 1, 2, 1, 3, 1, 7, 4]
Approximating ϕ ≅ 1.618033988749895:
16/10 = [1; 1, 1, 2]
1618/1000 = [1; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5]
16180/10000 = [1; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5]
Approximating e ≅ 2.718281828459045:
Actual: [
</pre>
|