Example #1
0
func init() {
	ways = memoize.Memoize(func(p, max int) int {
		if p == 0 {
			return 1
		}

		var total int
		if p >= 1 && max >= 1 {
			total += ways(p-1, 1)
		}
		if p >= 2 && max >= 2 {
			total += ways(p-2, 2)
		}
		if p >= 5 && max >= 5 {
			total += ways(p-5, 5)
		}
		if p >= 10 && max >= 10 {
			total += ways(p-10, 10)
		}
		if p >= 20 && max >= 20 {
			total += ways(p-20, 20)
		}
		if p >= 50 && max >= 50 {
			total += ways(p-50, 50)
		}
		if p >= 100 && max >= 100 {
			total += ways(p-100, 100)
		}
		if p >= 200 && max >= 200 {
			total += ways(p-200, 200)
		}
		return total
	}).(func(int, int) int)
}
Example #2
0
func TestPanic(t *testing.T) {
	count := 0
	f := func(i int) {
		count++
		if count%2 == 1 {
			panic(count)
		}
	}
	f = memoize.Memoize(f).(func(int))

	expect := func(p interface{}, i int) {
		defer func() {
			if r := recover(); p != r {
				t.Errorf("for input %d:\nexpected: %v\nactual: %v", i, p, r)
			}
		}()

		f(i)
	}

	expect(1, 1)
	expect(1, 1)
	expect(nil, 2)
	expect(nil, 2)
	expect(1, 1)
	expect(3, 100)
}
Example #3
0
func TestVariadic(t *testing.T) {
	count := 0
	var concat func(string, ...string) string
	concat = func(s0 string, s1 ...string) string {
		count++

		if len(s1) == 0 {
			return s0
		}
		return concat(s0+s1[0], s1[1:]...)
	}
	concat = memoize.Memoize(concat).(func(string, ...string) string)

	expect := func(actual, expected string, n int) {
		if actual != expected || n != count {
			t.Errorf("expected: %q\nactual: %q\nexpected count: %d\nactual count: %d", expected, actual, n, count)
		}
	}

	expect("", "", 0)
	expect(concat("string"), "string", 1)
	expect(concat("string", "one"), "stringone", 3)
	expect(concat("string", "one"), "stringone", 3)
	expect(concat("string", "two"), "stringtwo", 5)
	expect(concat("string", "one"), "stringone", 5)
	expect(concat("stringone", "two"), "stringonetwo", 7)
	expect(concat("string", "one", "two"), "stringonetwo", 8)
}
Example #4
0
func init() {
	paths = memoize.Memoize(func(w, h int) int {
		if w == 0 || h == 0 {
			return 1
		}

		n := paths(w-1, h) + paths(w, h-1)
		return n
	}).(func(w, h int) int)
}
Example #5
0
func init() {
	compute = memoize.Memoize(func(x, y int) int {
		if y >= len(triangle) {
			return 0
		}

		n := compute(x, y+1)
		if m := compute(x+1, y+1); m > n {
			n = m
		}

		n += triangle[y][x]

		return n
	}).(func(x, y int) int)
}
Example #6
0
}

func nonAbundantSum(n int) bool {
	for i := 12; i < n; i++ {
		if !isAbundant(i) {
			continue
		}

		if isAbundant(n - i) {
			return false
		}
	}

	return true
}

func isAbundant(n int) bool {
	return d(n) > n
}

var d = memoize.Memoize(func(n int) int {
	var sum int
	for i := 1; i < n; i++ {
		if n%i == 0 {
			sum += i
		}
	}

	return sum
}).(func(n int) int)
Example #7
0
	for a := -999; a <= 999; a++ {
		for b := -999; b <= 999; b++ {
			if c := count(a, b); c > max {
				maxA, maxB, max = a, b, c
			}
		}
	}

	fmt.Println(maxA * maxB)
}

func count(a, b int) int {
	var c int
	for n := 0; isPrime(n*n + a*n + b); n++ {
		c++
	}
	return c
}

var isPrime = memoize.Memoize(func(i int) bool {
	if i < 2 {
		return false
	}
	for j := 2; j*j <= i; j++ {
		if i%j == 0 {
			return false
		}
	}
	return true
}).(func(int) bool)