Created
July 12, 2018 01:47
-
-
Save ksakae1216/3ed2096a8508c50067cedb3bab9042d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// エラトステネスの篩 | |
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