Skip to content
This repository has been archived by the owner on Jun 20, 2020. It is now read-only.

Commit

Permalink
Added 10.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed Jul 29, 2011
1 parent 3551b51 commit ab0debb
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions problem_041/10.go
@@ -0,0 +1,76 @@
package main

import (
"math"
)

func GenerateCompleteSequence(number int) chan int {
channel := make(chan int)
go func(){
for i := int(2); i <= number; i++ {
channel <- i
}
channel <- 0
}()

return channel
}

func FilterSequence(prime int, channel chan int) chan int {
out := make(chan int)
go func(){
i := <-channel
for i != 0{
if i % prime != 0 {
out <- i
}
i = <-channel
}
out <- 0
}()
return out
}

func GetPrimes(number float64) chan int {
var greater int
square := int(math.Sqrt(number))
value := int(number)

out := make(chan int)
go func(){
for i := int(2); i <= int(number); i++ {
if i > square {
break
}
greater = i
}

channel := GenerateCompleteSequence(value)
prime := <-channel
for prime != 0 {
out <- prime
if prime <= square {
channel = FilterSequence(prime, channel)
}
prime = <-channel
}
out <- 0
}()

return out
}

func SumPrimes(number float64) int64 {
primesChannel := GetPrimes(number)
prime := <-primesChannel
sum := int64(0)
for {
if (prime == 0) {
break
}
sum += int64(prime)
prime = <-primesChannel
}

return sum
}

0 comments on commit ab0debb

Please sign in to comment.