Ejemplo n.º 1
0
// nまでのすべての素数を返す(2乗を記憶して計算量を減らす&メモリを最初に設定)
func ReadyMemRootUpTo(n *uint64) []uint64 {
	arr := make([]uint64, 0, int(*n/2))
	var num uint64 = 2
	square := make([]Square, 0, int(*n/2))
	for ; num <= *n; num++ {
		if len(arr) == 0 {
			arrAppend(&arr, &num, &square)
			continue
		}
		if num <= square[0].i2 {
			smalledArr := arr[:square[0].i]
			if !check.CheckAll(&num, &smalledArr) {
				arrAppend(&arr, &num, &square)
				continue
			}
		} else {
			square = square[1:len(square)]
			if num <= square[0].i2 {
				smalledArr := arr[:square[0].i]
				if !check.CheckAll(&num, &smalledArr) {
					arrAppend(&arr, &num, &square)
					continue
				}
			}
		}
	}
	return arr
}
Ejemplo n.º 2
0
// n個のすべての素数を返す
func MemRootToNum(n *uint64) []uint64 {
	arr := []uint64{}
	var num uint64 = 2
	square := []Square{}
	for ; uint64(len(arr)) < *n; num++ {
		if len(arr) == 0 {
			arrAppend(&arr, &num, &square)
			continue
		}
		if num <= square[0].i2 {
			smalledArr := arr[:square[0].i]
			if !check.CheckAll(&num, &smalledArr) {
				arrAppend(&arr, &num, &square)
				continue
			}
		} else {
			square = square[1:len(square)]
			if num <= square[0].i2 {
				smalledArr := arr[:square[0].i]
				if !check.CheckAll(&num, &smalledArr) {
					arrAppend(&arr, &num, &square)
					continue
				}
			}
		}
	}
	return arr
}
Ejemplo n.º 3
0
// nまでのすべての素数を返す
func AllUpTo(n *uint64) []uint64 {
	arr := []uint64{}
	var num uint64 = 2
	for ; num <= *n; num++ {
		if len(arr) == 0 {
			arr = append(arr, num)
			continue
		}
		if !check.CheckAll(&num, &arr) {
			arr = append(arr, num)
			continue
		}
	}
	return arr
}