From e337c8b004b6ae4082146433622c0c7916d6bd26 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Thu, 30 Jan 2020 16:26:20 +0100 Subject: [PATCH] first commit --- main.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..99f9def --- /dev/null +++ b/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + "runtime" +) + +var MAX uint64 = 1000000000 + +type Res struct{//this struct help communicating with the channel eg. weather channel will terminate, or what number it calculated + n uint64 + nr uint64 + nnr uint64 + thread uint64 + terminate bool +} +func reverseNum(num uint64) uint64{ + var ret uint64 = 0 + for num!=0{ + ret = 10*ret + num%10 + num /=10 + } + return ret +} +func isAllOdd(num uint64) bool { + for num !=0 { + if num%2==0{ + return false + } + num /=10 + } + return true +} +func findRev(start uint64, end uint64, step uint64, ch chan Res){ + for i:=start ; i < end; i+=step{ + if i%10==0{ + continue + } + h := i+reverseNum(i) + if isAllOdd(h){ + r :=new(Res) + r.n =i + r.nr = reverseNum(i) + r.nnr = h + r.thread = start // is equal to thread + r.terminate = false + ch <- *r + } + } + r:= new(Res) + r.terminate = true + ch<-*r +} +func main() { + numThreads := runtime.NumCPU() + ch := make(chan Res) + for i:=0 ;i< numThreads; i++{ + go findRev(uint64(i),MAX, uint64(numThreads),ch) + } + var n uint64 = 0 + for { + h:=<-ch + if h.terminate{ + numThreads-- + if numThreads <=0{ + fmt.Printf("\nTotal reversibles found:%d\n", n) + break + } + continue + } + n++ + //fmt.Printf("zahl=%d rev(zahl)=%d zahl+rev(zahl)=%d n=%d thread=%d\n", h.n,h.nr,h.nnr,n,h.thread) + + + } + +}