Ejemplo n.º 1
0
func result() (r uint64) {
search:
	for r = 3; ; r += 2 {
		if euler.IsPrime(r) {
			continue search
		}
		for i := uint64(1); 2*i*i < r; i++ {
			if euler.IsPrime(r - 2*i*i) {
				continue search
			}
		}
		break
	}
	return r
}
Ejemplo n.º 2
0
func main() {

	fmt.Println("Problem 7. What is the 10001st Prime?")
	start := time.Now()

	// Skip the number 2 so that we can use an increment of 2 and halve the set
	// I know that this is brute force but..
	// 1. It takes 7.1ms on my machine.
	// 2. I would never calculate these primes each time if I needed them. I would pre-calculate a set up front

	possibility := 3
	answer := 3
	count := 1

	for count < 10001 {
		if euler.IsPrime(possibility) {
			count = count + 1
			answer = possibility
		}
		possibility = possibility + 2
	}

	fmt.Printf("-->Answer: The 10,001st prime is %d\n", answer)
	fmt.Printf("took=%s\n\n", time.Since(start))

}
Ejemplo n.º 3
0
func Run() {
	primes := 0
	nonPrimes := 1
	size := 1

	span := 0
	for {
		span += 2
		for i := 0; i < 4; i++ {
			size += span
			if euler.IsPrime(size, 20) {
				primes += 1
			} else {
				nonPrimes += 1
			}
		}

		if primes*10 < (primes + nonPrimes) {
			break
		}

	}

	fmt.Printf("%d\n", span+1)
}
Ejemplo n.º 4
0
func main() {
	var count uint64
	for n := uint64(2); n < 1000000; n++ {
		m := n
		isprime := true
		var ok bool
		for {
			if !euler.IsPrime(m) {
				isprime = false
				break
			}
			m, ok = rot(m)
			if !ok {
				isprime = false
				break
			}
			if m == n {
				break
			}
		}
		if isprime {
			count++
		}
	}
	fmt.Printf("35 %d\n", count)
}
Ejemplo n.º 5
0
func BenchmarkMR(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if !euler.IsPrime(131071, 20) {
			b.Errorf("Prime failure: %s", 131071)
		}
	}
}
Ejemplo n.º 6
0
func main() {
	var count uint64
	var sum uint64
nextcandidate:
	for i := uint64(11); count < 11; i++ {
		d := euler.NtoD(i)
		dlen := len(d)
		for j := 0; j < len(d); j++ {
			a := euler.DtoN(d[j:dlen])
			b := euler.DtoN(d[0 : dlen-j])
			if !euler.IsPrime(a) || !euler.IsPrime(b) {
				continue nextcandidate
			}
		}
		count++
		sum += i
	}
	fmt.Printf("%d\n", sum)
}
Ejemplo n.º 7
0
func main() {
search:
	for i := uint64(1000); i < 10000-(3330*2); i++ {
		if i == 1487 {
			continue search
		}
		if !euler.IsPrime(i) {
			continue search
		}
		if !euler.IsPrime(i + 3330) {
			continue search
		}
		if !euler.IsPrime(i + 2*3330) {
			continue search
		}
		if !euler.IsPerm(euler.NtoD(i), euler.NtoD(i+3330)) {
			continue search
		}
		if !euler.IsPerm(euler.NtoD(i), euler.NtoD(i+3330*2)) {
			continue search
		}
		fmt.Printf("49 %d%d%d\n", i, i+3330, i+3330*2)
	}
}
Ejemplo n.º 8
0
func main() {
	var largest uint64
	digits := []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}
	for i := uint64(1); i <= 9; i++ {
		numperms := euler.Factorial(i)
		for j := uint64(0); j < numperms; j++ {
			dn := euler.NthPerm(j, digits[:i])
			n := euler.DtoN(dn)
			if euler.IsPrime(n) && pandigital(n) {
				if n > largest {
					largest = n
				}
			}
		}
	}
	fmt.Printf("41 %d\n", largest)
}
Ejemplo n.º 9
0
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)
		}
	}
}
Ejemplo n.º 10
0
func main() {
	starttime := time.Now()

	count := 0

	for start := int64(1); difference(start, 1) <= search; start++ {
		for jump := int64(1); difference(start, jump) <= search; jump++ {
			if euler.IsPrime(difference(start, jump)) {
				count++
				fmt.Println(start, jump, difference(start, jump))
			}
		}
	}

	fmt.Println(count)

	fmt.Println("Elapsed time:", time.Since(starttime))
}
Ejemplo n.º 11
0
func main() {
	var maxn, maxa, maxb, n int

	for a := -999; a < 1000; a++ {
		for b := -999; b < 1000; b++ {
			for n = 0; ; n++ {
				q := quad(n, a, b)
				if q < 0 || !euler.IsPrime(uint64(q)) {
					break
				}
			}
			if n > maxn {
				maxn = n
				maxa = a
				maxb = b
			}
		}
	}
	fmt.Printf("27 %d\n", maxa*maxb)
}
Ejemplo n.º 12
0
func test(repeat string, n, d int) (N int, S int64) {

	for i := 0; int64(i) < euler.Choose(int64(n), int64(d)); i++ {
		indices := euler.SplitSeq(d, i)

		for j := 0; int64(j) < euler.IntExp(10, int64(d)); j++ {
			insertstring := strconv.Itoa(j)
			for len(insertstring) < d {
				insertstring = "0" + insertstring
			}

			merged := ""
			current := 0
			for index := 0; index < n; index++ {
				if current < d && index == indices[d-current-1] {
					merged += insertstring[current : current+1]
					current++
				} else {
					merged += repeat
				}
			}

			mergedint, _ := strconv.ParseInt(merged, 10, 64)

			//exclude leading zeroes
			if mergedint > euler.IntExp(10, int64(n)-1) {

				if euler.IsPrime(mergedint) {
					//fmt.Println(mergedint)
					N++
					S += mergedint
				}
			}
		}
	}
	return
}