當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Sieve.IsPrime方法代碼示例

本文整理匯總了Golang中euler.Sieve.IsPrime方法的典型用法代碼示例。如果您正苦於以下問題:Golang Sieve.IsPrime方法的具體用法?Golang Sieve.IsPrime怎麽用?Golang Sieve.IsPrime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在euler.Sieve的用法示例。


在下文中一共展示了Sieve.IsPrime方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Run

func Run() {
	var sieve euler.Sieve
	limit := 1000000
	ps := sieve.PrimesUpto(limit)

	longestLen := 0
	longestVal := 0

	for a := 1; a < len(ps); a++ {
		total := 0
		for b := a; b < len(ps); b++ {
			total += ps[b]
			if total >= limit {
				break
			}

			if b-a+1 > longestLen && sieve.IsPrime(total) {
				longestLen = b - a + 1
				longestVal = total
			}
		}
	}

	fmt.Printf("%d\n", longestVal)
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:25,代碼來源:pr050.go

示例2: Run

func Run() {
	var s euler.Sieve

	p := 2
	count := 0

	// Move the closure out of the loop for efficiency.  It does
	// save some time.
	circular := true
	pcheck := func(num int) {
		if !s.IsPrime(num) {
			circular = false
		}
	}

	for p < 1000000 {
		circular = true
		eachRotation(p, pcheck)

		if circular {
			// fmt.Printf("%d\n", p)
			count++
		}
		p = s.NextPrime(p)
	}

	fmt.Printf("%d\n", count)
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:28,代碼來源:pr035.go

示例3: familySize

func familySize(sieve *euler.Sieve, base, part int) (size int) {
	orig := euler.DigitsOf(base)
	work := make([]int, len(orig))
	size = 0

	found := false
	for _, d := range orig {
		if d == part {
			found = true
			break
		}
	}
	if !found {
		return
	}

	for value := part; value <= 9; value++ {
		copy(work, orig)
		for i := range orig {
			if work[i] == part {
				work[i] = value
			}
		}

		prime := euler.OfDigits(work)
		if sieve.IsPrime(prime) {
			size++
		}
	}
	return
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:31,代碼來源:pr051.go

示例4: longestSeries

func longestSeries(sieve *euler.Sieve, a, b int) int {
	for n := 0; ; n++ {
		c := n*n + a*n + b
		if c < 2 || !sieve.IsPrime(c) {
			return n
		}
	}
	panic("Not reached")
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:9,代碼來源:pr027.go

示例5: isRightPrime

func isRightPrime(s *euler.Sieve, num int) bool {
	for num > 0 {
		if !s.IsPrime(num) {
			return false
		}
		num /= 10
	}
	return true
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:9,代碼來源:pr037.go

示例6: Run

func Run() {
	var s euler.Sieve

	n := 9
	for ; ; n += 2 {
		if s.IsPrime(n) {
			continue
		}
		_, present := goldbach(&s, n)
		if !present {
			break
		}
	}
	fmt.Printf("%d\n", n)
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:15,代碼來源:pr046.go

示例7: isLeftPrime

func isLeftPrime(s *euler.Sieve, num int) bool {
	mod := 1
	for mod < num {
		mod *= 10
	}

	for mod > 1 {
		num %= mod
		mod /= 10
		if !s.IsPrime(num) {
			return false
		}
	}

	return true
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:16,代碼來源:pr037.go

示例8: TestMR

func TestMR(t *testing.T) {
	var sv euler.Sieve

	limit := 1000000
	if testing.Short() {
		limit = 100000
	}

	for i := 2; i < limit; i++ {
		b := sv.IsPrime(i)
		b2 := euler.IsPrime(i, 20)

		if b != b2 {
			t.Errorf("Mismatch: %d (%v!=%v)", i, b, b2)
		}
	}
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:17,代碼來源:miller_rabin_test.go

示例9: Run

func Run() {
	var sieve euler.Sieve

	var result int64

	// Check if this result is valid, and process it if it is.
	isValid := func(nums []int) {
		if nums[1]-nums[0] != nums[2]-nums[1] {
			return
		}
		for _, num := range nums {
			if !sieve.IsPrime(num) {
				return
			}
		}
		if nums[0] == 1487 {
			// Skip, per problem description.
			return
		}
		result = int64(nums[0])*100000000 + int64(nums[1])*10000 + int64(nums[0])
	}

	// This isn't actually right, but it so happens that the
	// initial value of the result is prime.  The first prime of
	// the result might not be the lowest permutation, and that
	// lowest permutation might not be prime.
	for base := 1009; base < 10000; base++ {
		if !isAscending(base) {
			continue
		}
		perms := allPermutations(base)
		selections(perms, isValid)
	}

	fmt.Printf("%d\n", result)
}
開發者ID:d3zd3z,項目名稱:euler,代碼行數:36,代碼來源:pr049.go


注:本文中的euler.Sieve.IsPrime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。