コード例 #1
0
ファイル: solution.go プロジェクト: roessland/project-euler
func main() {
	TestM()
	var N int64 = 10000000
	primes := primegen.SliceFromMap(primegen.Map(N)) // 80 MB of primes
	values := make([]bool, N+1)

	sqrtN := int64(math.Sqrt(float64(N)))
	for i := int64(0); i <= sqrtN; i++ {
		p := primes[i]
		j := i + 1
		for {
			q := primes[j]
			if p*q > N {
				break
			}
			values[M(p, q, N)] = true
			j++
		}
	}

	S := int64(0)
	for index, found := range values {
		if found {
			S += int64(index)
		}
	}
	fmt.Printf("The sum of distinct values is %v\n", S)
}
コード例 #2
0
ファイル: solution.go プロジェクト: roessland/project-euler
func main() {
	isPrime = primegen.Map(10000000) // 10^7 = sqrt(10^14)
	primes = primegen.SliceFromMap(isPrime)

	N := 14

	// Find all Harshad numbers with N or fewer digits
	rightTruncatable := make([][]int64, N+1)
	rightTruncatable[1] = []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	for i := 1; i < N-1; i++ {
		rightTruncatable[i+1] = ExpandRight(rightTruncatable[i])
	}

	// Keep only the strong ones
	strongTruncatable := []int64{}
	for _, num := range rightTruncatable[N-1] {
		if IsStrongHarshad(num) {
			strongTruncatable = append(strongTruncatable, num)
		}
	}

	// Add 1, 3, 7, 9 to the end of all these numbers and check if they are prime.
	// If so, add them to the sum.
	sum := int64(0)
	for _, num := range strongTruncatable {
		sum += ExpandPrimeSum(num)
	}

	fmt.Printf("%v\n", sum)
}
コード例 #3
0
ファイル: solution.go プロジェクト: roessland/project-euler
func main() {
	isPrime := primegen.Map(1000000)
	p := append([]int64{0}, primegen.SliceFromMap(isPrime)...)

	for n := uint64(1); ; n++ {
		p2 := uint64(p[n] * p[n])
		r := (mu.ModPowUint64(uint64(p[n]-1), n, p2) + mu.ModPowUint64(uint64(p[n]+1), n, p2)) % p2
		if r > 10000000000 {
			fmt.Printf("%v\n", n)
			break
		}
	}
}
コード例 #4
0
ファイル: solution.go プロジェクト: roessland/project-euler
func main() {
	// Tactic: Generate all numbers of this form up to a certain size. Filter
	// out primes. The remainder are odd composities that cannot be written as
	// the sum of a prime and twice a square.
	MAX := int64(10000)

	isPrime := primegen.Map(MAX)
	primes := primegen.SliceFromMap(isPrime)

	_ = isPrime
	_ = primes

	// Generate lots of numbers
	wasGenerated := make([]bool, MAX+1)
	for _, p := range primes {

		// If the prime is this big, the number is bigger than max
		if p >= MAX {
			break
		}

		// Starting from zero removes all primes
		for i := int64(0); ; i++ {
			num := p + 2*i*i
			if num > MAX {
				break
			}
			wasGenerated[num] = true
		}

	}

	// Remove all even numbers
	for i := int64(0); i <= MAX; i += 2 {
		wasGenerated[i] = true
	}

	// Print some numbers
	for num, val := range wasGenerated {
		if !val {
			fmt.Printf("Possible candidate: %v\n", num)
		}
	}

}
コード例 #5
0
ファイル: solution.go プロジェクト: roessland/project-euler
func main() {
	// Generate primes
	N := int64(10000)
	isPrime = primegen.Map(99999999)
	p := primegen.SliceFromMap(primegen.Map(N))

	// Store the twin primes of every small prime
	S = make(map[int][]int)
	for i := 0; i < len(p); i++ {
		pi := int(p[i])
		Spi := []int{}
		for j := i + 1; j < len(p); j++ {
			pj := int(p[j])
			if Twins(pi, pj) {
				S[pi] = append(S[pi], pj)
			}
		}
		if len(Spi) > 0 {
			S[pi] = Spi
		}
	}

	// Try reading this out loud
	for pi, Si := range S {
		for _, pii := range Si {
			Sii := sliceutil.IntersectInt(S[pii], Si)
			for _, piii := range Sii {
				Siii := sliceutil.IntersectInt(S[piii], Sii)
				for _, piiii := range Siii {
					Siiii := sliceutil.IntersectInt(S[piiii], Siii)
					for _, piiiii := range Siiii {
						fmt.Printf("%v, %v, %v, %v, %v = %v\n", pi, pii, piii, piiii, piiiii, pi+pii+piii+piiii+piiiii)
					}
				}
			}
		}
	}

}
コード例 #6
0
func main() {
	// For checking if large numbers are prime
	isPrime = primegen.Map(100000000)

	// For looping trough all combinations of
	smallPrimes := primegen.SliceFromMap(primegen.Map(999))

	// Loop trough possibilites by increasing sum
	for sum := int64(1); sum < 1000; sum++ {
		if sum%10 == 0 {
			fmt.Printf("%v\n", sum)
		}
		setsOfFive := mathutil.Partition(smallPrimes, sum, 5, 0)

		// Check if any of the possibilites satisfy the critera
		for _, setOfFive := range setsOfFive {
			if AwesomePrimes(setOfFive) {
				fmt.Printf("sum=%v, primes=%v\n", sum, setOfFive)
				return
			}
		}
	}
}