fork download
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "time"
  6. )
  7.  
  8. const TARGET int = 22
  9.  
  10. func IstTeilbarAlle(n, m int) bool {
  11. for i := m; i > 1; i-- {
  12. if n % i != 0 {
  13. return false
  14. }
  15. }
  16. return true
  17. }
  18.  
  19. func iterator() chan int {
  20. c := make(chan int)
  21. go func() {
  22. i := 1
  23. for {
  24. c <- i
  25. i++
  26. }
  27. }()
  28. return c
  29. }
  30.  
  31. func check(c, out chan int, n int) {
  32. for {
  33. x := n * <- c
  34. if IstTeilbarAlle(x, n - 1) {
  35. out <- x
  36. return
  37. }
  38. }
  39. }
  40.  
  41. func main() {
  42. started := time.Seconds()
  43. fmt.Printf("Suche alle Teiler von %v\n", TARGET)
  44. c := iterator()
  45. out := make(chan int)
  46. go check(c, out, TARGET)
  47. go check(c, out, TARGET)
  48. go check(c, out, TARGET)
  49. go check(c, out, TARGET)
  50. fmt.Printf("Ergebnis: %v (nach %v Sekunden)\n", <- out, time.Seconds() - started)
  51. }
  52.  
Success #stdin #stdout 4.35s 3120KB
stdin
Standard input is empty
stdout
Suche alle Teiler von 22
Ergebnis: 232792560 (nach 4 Sekunden)