Guest User

Untitled

a guest
Jan 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type Sequencer interface {
  6. Next() Sequencer
  7. }
  8.  
  9. type Elem struct {
  10. b int
  11. res float64
  12. }
  13. type OddElem Elem
  14. type EvenElem Elem
  15.  
  16. func (o *OddElem) Next() Sequencer {
  17. b := o.b + 1
  18. e := &EvenElem{b, o.res - 1.0/float64(b)}
  19. fmt.Println(e.res)
  20. return e
  21. }
  22.  
  23. func (e *EvenElem) Next() Sequencer {
  24. b := e.b + 1
  25. o := &OddElem{b, e.res + 1.0/float64(b)}
  26. fmt.Println(o.res)
  27. return o
  28. }
  29.  
  30. //1-1/2+1/3-1/4...
  31.  
  32. func main() {
  33. var s Sequencer
  34. s = &OddElem{1, 1}
  35. for i := 0; i < 10; i++ {
  36. s = s.Next()
  37. }
  38. }
  39.  
  40. /*
  41. func main() {
  42. res := 1.0
  43. for b := 2; b < 10; b++ {
  44. if b%2 == 0 {
  45. res -= 1.0 / float64(b)
  46. } else {
  47. res += 1.0 / float64(b)
  48. }
  49. fmt.Println(res)
  50. }
  51. }
  52. */
Add Comment
Please, Sign In to add comment