Пример #1
0
func TestCoprime(t *testing.T) {
	ps := []int{
		2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
		127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
		877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953,
		2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153,
		9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931,
		1000003, 1000033, 1000037,
	}

	// Distinct primes are always relatively coprime
	for i, p := range ps {
		for j := 0; j <= i; j++ {
			q := ps[j]
			got := primes.Coprime(p, q)
			want := p != q
			if got != want {
				t.Errorf("Coprime(%d,%d) == %v, want %v", p, q, got, want)
			}
		}
	}

	// Numbers sharing a common factor are never relatively coprime
	factors := []int64{2, 5, 19, 47, 137, 877, 2089, 9931}
	for i, p := range ps {
		for j := 0; j <= i; j++ {
			q := ps[j]
			for _, f := range factors {
				a := f * int64(p)
				b := f * int64(q)
				if a <= math.MaxInt32 && b <= math.MaxInt32 {
					if primes.Coprime(int(a), int(b)) {
						t.Errorf("Coprime(%d,%d) == true, want false", a, b)
					}
				}
			}
		}
	}
}
Пример #2
0
func ExampleCoprime() {
	// Check which combinations are coprime
	ns := []int{2, 3, 4, 5, 6}
	for i, a := range ns {
		for _, b := range ns[i+1:] {
			if primes.Coprime(a, b) {
				fmt.Printf("%d and %d are coprime\n", a, b)
			}
		}
	}

	// Output:
	// 2 and 3 are coprime
	// 2 and 5 are coprime
	// 3 and 4 are coprime
	// 3 and 5 are coprime
	// 4 and 5 are coprime
	// 5 and 6 are coprime
}