I want to find sum of prime numbers ina given range but in cant callback my sum fuction please help me with it


(Athul Baby) #1

package main

import (
“fmt”
)

func prime(x, y int) []int{
z := []int{}
for i := x; i < y; i++ {
for j := 2; j < i; j++ {
if i%j == 0 {
break
} else {
z = append(z, i)
break
}
}
}
return z
}
func sum(xi []int)int {
sum := 0
for _, v := range xi {
sum += v
}
return sum
}
func main() {

s := prime(10, 100)
fmt.Println(s)

}


(Curtis Allyn Green) #2

Try using this method: https://play.golang.org/p/qsSIvlvfSv1


(Athul Baby) #3

thank you very much


(Athul Baby) #4

can we make sum as a separate function


(Johan Dahl) #5

Hi. I made a small guide how to post code on this forum. It will make the code easier to read. How to post code on this forum


(Manxiaqu) #6

Edit based on code posted by Curtis Allyn Green. https://play.golang.org/p/OkHWY9LeJ4O


(Johan Dahl) #7

Nice that you put it in go playground :blush: The error in that code is that in the last example is sum called with 10 for both arguments sum(10, 10) instead of sum(10, 100) or something similar.