-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressions.go
86 lines (71 loc) · 1.63 KB
/
progressions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"math"
)
type Progression interface {
Nth(int) float64
ToNth(int) []float64
SumToNth(int) float64
}
type ArithmeticProgression struct {
Start float64
Difference float64
}
func (ap ArithmeticProgression) Nth(n int) float64 {
return ap.Start + float64(n-1)*ap.Difference
}
func (ap ArithmeticProgression) ToNth(n int) []float64 {
a := []float64{ap.Start}
for i := 1; i < n; i++ {
a = append(a, a[len(a)-1]+ap.Difference)
}
return a
}
func (ap ArithmeticProgression) SumToNth(n int) float64 {
return float64(n) * (ap.Start + ap.Nth(n)) / 2
}
type GeometricProgression struct {
Start float64
Ratio float64
}
func (gp GeometricProgression) Nth(n int) float64 {
return gp.Start * math.Pow(gp.Ratio, float64(n-1))
}
func (gp GeometricProgression) ToNth(n int) []float64 {
b := []float64{gp.Start}
for i := 1; i < n; i++ {
b = append(b, b[len(b)-1]*gp.Ratio)
}
return b
}
func (gp GeometricProgression) SumToNth(n int) float64 {
if gp.Ratio == 1 {
return float64(n) * gp.Start
} else {
return gp.Start*math.Pow(gp.Ratio, float64(n)) - 1/(gp.Ratio-1)
}
}
type HarmonicProgression struct {
Start float64
Difference float64
}
func (hp HarmonicProgression) Nth(n int) float64 {
return math.Pow((math.Pow(hp.Start, -1) + float64(n-1)*hp.Difference), -1)
}
func (hp HarmonicProgression) ToNth(n int) []float64 {
c := []float64{hp.Start}
for i := 1; i < n; i++ {
c = append(c, math.Pow((math.Pow(c[len(c)-1], -1)+hp.Difference), -1))
}
return c
}
func (hp HarmonicProgression) SumToNth(n int) float64 {
sum := 0.0
c := hp.ToNth(n)
for _, i := range c {
sum += i
}
return sum
}
func main() {
}