Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.04 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "time"
  6. )
  7.  
  8. func main() {
  9.     intchan := make(chan int)
  10.     floatchan := make(chan float64)
  11.     var fibratio float64
  12.  
  13.     fiblength := 50
  14.  
  15.     go fib(intchan, floatchan, fiblength)
  16.     go ratio(floatchan, fiblength)
  17.  
  18.     for i := 0; i < fiblength; i++ {
  19.         fibnum := <-intchan
  20.  
  21.         if i > 0 {
  22.             fibratio = <-floatchan
  23.         }
  24.  
  25.         fmt.Println(fibnum, " - ", fibratio)
  26.         time.Sleep(time.Millisecond * 250)
  27.     }
  28. }
  29.  
  30. func fib(intchan chan int, floatchan chan float64, length int) {
  31.     var next, current, last int
  32.  
  33.     for i := 0; i < length; i++ {
  34.         if current == 0 {
  35.             next = 1
  36.         } else {
  37.             next = current + last
  38.         }
  39.  
  40.         last = current
  41.         current = next
  42.  
  43.         intchan <- next
  44.         floatchan <- float64(next)
  45.     }
  46. }
  47.  
  48. func ratio(floatchan chan float64, length int) {
  49.     var first bool = true
  50.     var x, y, fibratio float64
  51.  
  52.     for i := 0; i < length; i++ {
  53.         if first == true {
  54.             y = <-floatchan
  55.             x = <-floatchan
  56.         } else {
  57.             x = <-floatchan
  58.         }
  59.  
  60.         fibratio = x / y
  61.         y = x
  62.         first = false
  63.         floatchan <- fibratio
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement