Ejemplo n.º 1
0
func Euler041() int {
	//process 7 digit pandigitals
	p7d := make([]int, 0, 5040) //7!
	for v := range utils.Perm(utils.Numerals[1:8]) {
		n, _ := strconv.Atoi(v)
		p7d = append(p7d, n)
	}
	sort.Sort(sort.Reverse(sort.IntSlice(p7d)))

	for i := 0; i < 5040; i++ {
		if utils.IsPrime(p7d[i]) {
			return p7d[i]
		}
	}

	//process 4 digit pandigitals
	p4d := make([]int, 0, 24) //4!
	for v := range utils.Perm(utils.Numerals[1:5]) {
		n, _ := strconv.Atoi(v)
		p4d = append(p4d, n)
	}
	sort.Sort(sort.Reverse(sort.IntSlice(p4d)))

	for i := 0; i < 5040; i++ {
		if utils.IsPrime(p4d[i]) {
			return p4d[i]
		}
	}

	return -1
}
Ejemplo n.º 2
0
func GetFactors(n int) (fSlice []uint64) {
	if utils.IsPrime(n) {
		return []uint64{uint64(n)}
	}
	un := uint64(n)
	for _, v := range pCache {
		if un%v == 0 {
			fSlice = append(fSlice, v)
		}
	}

	return fSlice
}
Ejemplo n.º 3
0
func Euler027(absLimit int) int {
	maxA, maxB, maxN := 0, 0, 0
	for a := -absLimit; a < absLimit; a++ {
		for b := -absLimit; b < absLimit; b++ {
			n := 0
			for {
				if utils.IsPrime(n*n + a*n + b) {
					n++
				} else {
					break
				}
			}
			if n > maxN {
				maxA, maxB, maxN = a, b, n
			}
		}
	}
	return maxA * maxB
}
Ejemplo n.º 4
0
func Euler049() int {

	pg := primegen.New()
	pg.SkipTo(1489) //skip primes with less 4 digits and the example starting prime.
	for p := pg.Next(); p < 9999; p = pg.Next() {
		seq := make([]uint64, 0, 3)
		isPerm := GetPermutations(p)
		seq = append(seq, p)
		for n := p + 3330; utils.IsPrime(int(n)) && isPerm[n]; n += 3330 {
			seq = append(seq, n)
			if len(seq) == 3 {
				num, _ := strconv.Atoi(fmt.Sprintf("%v%v%v", seq[0], seq[1], seq[2]))
				return num
			}
		}
	}

	return 0
}
Ejemplo n.º 5
0
func Euler035() int {
	primes := utils.AtkinSieve(1000000)
	circulars := make(map[int]bool)
	for _, p := range primes {
		if circulars[p] {
			continue
		}
		circular := true
		rots := rotations(p)
		for _, r := range rots {
			if !utils.IsPrime(r) {
				circular = false
				break
			}
		}
		if circular {
			for _, r := range rots {
				circulars[r] = true
			}
		}
	}
	return len(circulars)
}