Skip to content

Instantly share code, notes, and snippets.

@ksakae1216
Created July 12, 2018 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksakae1216/3ed2096a8508c50067cedb3bab9042d4 to your computer and use it in GitHub Desktop.
Save ksakae1216/3ed2096a8508c50067cedb3bab9042d4 to your computer and use it in GitHub Desktop.
// エラトステネスの篩
func furui(list []int) {
// ループ最大値取得(maxNumの平方根)
loopMax := math.Sqrt(maxNum)
var tmp []int
var target = 1
// 素数を調べる(maxNumの平方根までループすればいい)
for index := 0; target < int(loopMax); index++ {
target = list[index] // 素数候補取得
for i := 0; i < len(list); i++ {
if list[i]%target != 0 {
// 素数候補で割り切れる数をふるい落とす(割り切れない数をtmpにセット)
tmp = append(tmp, list[i])
} else if list[i] == target {
// 素数候補自身はOK、tmpにセット
tmp = append(tmp, list[i])
}
}
list = tmp // ふるい落とした結果をセット
tmp = nil
}
// 素数を表示
// for _, sosu := range list {
// fmt.Println(sosu)
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment