Пример #1
0
/*	solution:
	Will rapresent the moves as R=right, D=Down....
	In any grid of size N, we need N moves of each (R,D) (total 2N moves)
		ex for the 2x2 grid we have RRDD,RDRD,RDDR,DRRD,DRDR,DDRR (6 moves)
	If we arbitrary choose N of one direction first and fill the rest
	with other direction,
	the problem becomes a combination problem for N choices out of 2N
	so we could use the C(n,k) = n!/(k!(n-k)!) for the answer
	where n = 2N and k=N
*/
func Euler015(N int) uint64 {
	n, k := (2 * N), N

	num := utils.Fact(n)
	den := new(big.Int).Mul(utils.Fact(k), utils.Fact(n-k))
	res := new(big.Int).Div(num, den)

	return res.Uint64()
}
Пример #2
0
func Euler020() int {
	sum := 0
	sf := utils.Fact(100).String()
	for _, c := range sf {
		d, _ := strconv.Atoi(string(c))
		sum += d
	}
	return sum
}
Пример #3
0
/*solution:
will use a similar analysis of that for problem 30.
we find that we need to consider numbers with max 7 digits as for 8-digit nums max sum of factorials will be with 7 digits.
so max = 9,999,999 at first pas.
but the max num we can obtain by summing factorials on a 7 digit number is 7*9! = 2,540,160
that will be our upper bound
*/
func Euler034() int {
	limit := int(7 * utils.Fact(9).Int64())

	facts := make([]int, 10)
	for i := 0; i < 10; i++ {
		facts[i] = int(utils.Fact(i).Int64())
	}

	sum := 0
	for i := 3; i < limit; i++ {
		sNum := strconv.Itoa(i)
		iSum := 0
		for _, c := range sNum {
			ic, _ := strconv.Atoi(string(c))
			iSum += facts[ic]
		}

		if i == iSum {
			sum += i
		}
	}
	return sum
}