コード例 #1
0
ファイル: main.go プロジェクト: jasonmobley/euler-go
func main() {
	var primeSieve chan int = primes.Sieve()
	defer close(primeSieve)

	primeSlice := make([]int, 1000, 1000)

	for i := 0; i < 1000; i++ {
		primeSlice[i] = <-primeSieve
	}

	var a, b, n, result int
	var maxN, maxA, maxB int
	var stillPrime = true

	for a = -999; a < 1000; a++ {
		for b = -999; b < 1000; b++ {
			stillPrime = true
			for n = 0; stillPrime; n++ {
				result = (n * n) + (a * n) + b
				if isPrime(result, &primeSlice) {
					if n > maxN {
						maxN = n
						maxA = a
						maxB = b
					}
				} else {
					stillPrime = false
				}
			}
			fmt.Println()
		}
	}

	fmt.Printf("Got %d consecutive primes with a = %d and b = %d\n", maxN, maxA, maxB)
}
コード例 #2
0
ファイル: main.go プロジェクト: jasonmobley/euler-go
func main() {
	var d, currentCycleLength, maxCycleLength, dMax int
	var primeSieve chan int = primes.Sieve()
	defer close(primeSieve)

	for d = <-primeSieve; d < 1000; d = <-primeSieve {
		currentCycleLength = cycleLength(d)

		if currentCycleLength > maxCycleLength {
			maxCycleLength = currentCycleLength
			dMax = d
		}
		//fmt.Printf("%d -> %d\n", d, cycleLength(d))
	}

	fmt.Println(dMax)
}
コード例 #3
0
ファイル: main.go プロジェクト: jasonmobley/euler-go
			}

			// Remove the current factor to continue
			n /= p
		}
		// Bail when the number to test is less than the prime divisor
		if n < p {
			break
		}
	}

	return factorMap, factorSlice
}

var primeCache []int = make([]int, 0, 10000)
var primeSieve chan int = primes.Sieve()

func primesUpTo(limit int) []int {
	primesNeeded := 0

	for i := 0; i < len(primeCache) && primeCache[i] < limit; i++ {
		primesNeeded += 1
	}

	for len(primeCache) == 0 || primeCache[len(primeCache)-1] < limit {
		primesNeeded += 1
		primeCache = append(primeCache, <-primeSieve)
	}

	return primeCache[0 : primesNeeded-1]
}