Пример #1
0
func Benchmark1e8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sieve.New(1e8).Iterate(1, 1e8, func(uint64) (terminate bool) {
			return
		})
	}
}
Пример #2
0
// Test the two generic access functions in prime.go, Primes() and
// Iterator().  Also test that the generators agree on all primes under
// a somewhat bigger limit.
//
// Use basic sieve as a reference, compare it to other generators.
func TestGeneric(t *testing.T) {
	const limit = 1000
	// exercise generic function Primes on sieve.
	reference := prime.Primes(sieve.New(limit), 0, limit)
gloop:
	for _, gen := range []prime.Generator{
		segment.New(limit),
		queue.PQueue{},
		sprp.New(),
	} {
		// exercise generic function Iterator on other generators.
		ch := prime.Iterator(gen, 0, limit)
		for i, pRef := range reference {
			switch p, ok := <-ch; {
			case !ok:
				t.Errorf("%s.Iterator(0, %d) didn't find all primes.",
					reflect.TypeOf(gen), limit)
				continue gloop
			case p != pRef:
				t.Errorf("%dth prime?  sieve says %d, %s says %d\n",
					i+1, pRef, reflect.TypeOf(gen), p)
				continue gloop
			}
		}
		if _, ok := <-ch; ok {
			t.Errorf("%s.Iterator(0, %d) returning too many primes.\n",
				reflect.TypeOf(gen), limit)
		}
	}
}
Пример #3
0
func TestBinomialS(t *testing.T) {
	p := sieve.New(uint64(tcs[len(tcs)-1].n))
	var b big.Int
	for _, tc := range tcs {
		if a := binomial.BinomialS(&b, p, tc.n, tc.k).String(); a != tc.s {
			t.Error("Binomial(%d, %d) expected %s, got %s", tc.n, tc.k, tc.s, a)
		}
	}
}
Пример #4
0
// a little different that the generic test a directory up. creates sieves
// with different limits, starts iterating at 1, verifies number of primes.
func TestSieve(t *testing.T) {
	for _, tc := range tcs {
		var count uint64
		sieve.New(tc.n).Iterate(1, tc.n, func(uint64) (terminate bool) {
			count++
			return
		})
		if count != tc.nPrimes {
			t.Errorf("Wrong number of primes <= %d.  expected %d, found %d",
				tc.n, tc.nPrimes, count)
		}
	}
}
Пример #5
0
func BenchmarkBinomial1e6(b *testing.B) {
	var c big.Int
	for i := 0; i < b.N; i++ {
		binomial.Binomial(&c, 1e6, uint(1e6)/3)
	}
}

func BenchmarkBinomial2e6(b *testing.B) {
	var c big.Int
	for i := 0; i < b.N; i++ {
		binomial.Binomial(&c, 2e6, uint(2e6)/3)
	}
}

var p = sieve.New(2e6)

func BenchmarkBinomialS1e2(b *testing.B) {
	var c big.Int
	for i := 0; i < b.N; i++ {
		binomial.BinomialS(&c, p, 1e2, uint(1e2)/3)
	}
}

func BenchmarkBinomialS1e3(b *testing.B) {
	var c big.Int
	for i := 0; i < b.N; i++ {
		binomial.BinomialS(&c, p, 1e3, uint(1e3)/3)
	}
}
Пример #6
0
// Exercise Limit and Iterate methods of each implemented generator.
func TestMethods(t *testing.T) {
	t100(t, segment.New(100))
	t100(t, sieve.New(100))
	t100(t, queue.PQueue{})
	t100(t, sprp.New())
}
Пример #7
0
// New is a constructor that generates the underlying prime sieve.
// (If you have a sieve already, you can just assign the Sieve member of
// a zero value Swing object.)
func New(n uint) *Swing {
	return &Swing{Sieve: sieve.New(uint64(n))}
}
Пример #8
0
// Binomal computes the binomial coefficient C(n,k) leaving the result in z.
// It replaces the existing value of z and returns z.
func Binomial(z *big.Int, n, k uint) *big.Int {
	if k > n {
		return z.SetInt64(0)
	}
	return BinomialS(z, sieve.New(uint64(n)), n, k)
}