Example #1
0
// https://projecteuler.net/problem=5
//
// 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
// What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
func main() {
	k := 20

	done := make(chan struct{})
	defer close(done)

	g := primes.Generator(done)

	n := 1
	limit := int(math.Sqrt(float64(k)))
	for p := range g {
		if p > k {
			break
		}

		a := 1
		if p <= limit {
			a = numbers.GreatestPerfectPower(p, k)
		}

		n *= int(math.Pow(float64(p), float64(a)))
	}

	log.Println(n)
}
Example #2
0
// https://projecteuler.net/problem=7
//
// By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
// What is the 10 001st prime number?
func main() {
	n := 10001

	done := make(chan struct{})
	defer close(done)

	g := primes.Generator(done)

	p := 0
	for i := 0; i < n; i++ {
		p = <-g
	}

	log.Println(p)
}
Example #3
0
// https://projecteuler.net/problem=10
//
// The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
// Find the sum of all the primes below two million.
func main() {
	limit := 2000000

	done := make(chan struct{})
	defer close(done)

	g := primes.Generator(done)

	sum := 0
	for p := range g {
		if p >= limit {
			break
		}
		sum += p
	}

	log.Println(sum)
}