-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.go
59 lines (48 loc) · 1.21 KB
/
counter.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
package euler_go
////////////////////////////// interface //////////////////////////////
type Counter interface {
Next() int64
}
///////////////////////// infinite linear counter /////////////////////
type LinearCounter struct {
Value, step int64
}
func NewLinearCounter(start, step int64) (c *LinearCounter) {
return &LinearCounter{Value: start, step: step}
}
func (c *LinearCounter) Next() int64 {
c.Value += c.step
return c.Value
}
//////////////////////// infinite fibonacci series /////////////////////
type FibonacciCounter struct {
Value, nextValue int64
}
func NewFibonacciCounter() (c *FibonacciCounter) {
return &FibonacciCounter{1, 1}
}
func (c *FibonacciCounter) Next() int64 {
c.Value, c.nextValue = c.nextValue, c.Value+c.nextValue
return c.Value
}
//////////////////////////// collatz series ////////////////////////////
type CollatzSeries struct {
Value, Length int64
}
func NewCollatzSeries(start int64) (s *CollatzSeries) {
return &CollatzSeries{Value: start, Length: 1}
}
func (s *CollatzSeries) Next() int64 {
if s.Value == 1 {
return s.Value
} else {
if s.Value%2 == 0 {
s.Value /= 2
s.Length += 1
} else {
s.Value = s.Value*3 + 1
s.Length += 1
}
return s.Value
}
}