package main

import (
	"fmt"
	"time"
)

const TARGET int = 22

func IstTeilbarAlle(n, m int) bool {
	for i := m; i > 1; i-- {
		if n % i != 0 {
			return false
		}
	}
	return true
}

func iterator() chan int {
	c := make(chan int)
	go func() {
		i := 1
		for {
			c <- i
			i++
		}
	}()
	return c
}

func check(c, out chan int, n int) {
	for {
		x := n * <- c
		if IstTeilbarAlle(x, n - 1) {
			out <- x
			return
		}
	}
}

func main() {
	started := time.Seconds()
	fmt.Printf("Suche alle Teiler von %v\n", TARGET)
	c := iterator()
	out := make(chan int)
	go check(c, out, TARGET)
	go check(c, out, TARGET)
	go check(c, out, TARGET)
	go check(c, out, TARGET)
	fmt.Printf("Ergebnis: %v (nach %v Sekunden)\n", <- out, time.Seconds() - started)
}
