示例#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
}
示例#2
0
func GetPermutations(n uint64) map[uint64]bool {
	pMap := make(map[uint64]bool, 24)
	sp := utils.Perm(strconv.FormatUint(n, 10))
	for s := range sp {
		np, _ := strconv.ParseUint(s, 10, 64)
		pMap[np] = true
	}
	return pMap
}
示例#3
0
func Euler024(pNum int) string {
	i := 1
	for v := range utils.Perm(utils.Numerals) {
		if i == pNum {
			return v
		}
		i++
	}
	return ""
}
示例#4
0
/* solution:
since d4d5d6 must be divisible by 5: d6:=is in {0,5}
since d6d7d8 must be divisible by 11 d6 can not be 0 (that would leave only 011,022 etc that break the pandigital condition)
d4 must be even
will brute-force the rest
*/
func Euler043() int {
	s := 0
	for p := range utils.Perm(utils.Numerals) {
		if n, _ := strconv.Atoi(p[7:10]); n%17 != 0 {
			continue
		} else if n, _ := strconv.Atoi(p[6:9]); n%13 != 0 {
			continue
		} else if n, _ := strconv.Atoi(p[5:8]); n%11 != 0 {
			continue
		} else if n, _ := strconv.Atoi(p[4:7]); n%7 != 0 {
			continue
		} else if p[5:6] != "5" {
			continue
		} else if n, _ := strconv.Atoi(p[1:4]); n%2 != 0 {
			continue
		} else if n, _ := strconv.Atoi(p[2:5]); n%3 != 0 {
			continue
		}

		n, _ := strconv.Atoi(p)
		s += n
	}
	return s
}