This page contains spoilers!
You might have more fun figuring out some of these tricks yourself.
I tried to catalogue only the "open secrets" that tend to come up on Discord. You don't need advanced mathematics to solve this in an efficient way. You can create an efficient algorithm to solve it from first principles and it can be fun to find it yourself. But if you are out of ideas and really want a spoiler, see the following Python code.12 Days of Christmas
Twelve
).brainfuck
--
in your language: that's a perfectly fine brainfuck program producing no output.eval
, that's probably the way to go.Catalan's Constant
Collatz
Emirp Numbers
CSS Colors
args
is always a complete permutation of all possible inputs.arg => sorted(args).index(arg)
as a perfect hash function.Evil Numbers, Odious Numbers
{2n, 2n+1}
is evil (and the other is odious), so you can hardcode 25 bits.Fibonacci
t=a;a=b;b+=t;
(you can avoid a temp variable in any language).Happy Numbers
ISBN
Lucky Tickets
def lucky_tickets(digits, base):
"""
You are given an even number of digits and a base.
The problem is to determine how many integers exist with the given number of digits in the
given base, where the sum of the first half of the digits is the same as the sum of the second
half of the digits. This is called the number of lucky tickets.
"""
half_digits = digits//2
# Compute the maximum sum of each group of half of the digits.
max_sum = half_digits * (base - 1)
# Determine number of times each sum occurs for each group of half of the digits.
empty_sum_counts = [0] * (max_sum + 1)
sum_counts = empty_sum_counts.copy()
# Each of the digits 0 through base - 1 occurs once in the first digit.
sum_counts[:base] = [1] * base
# For each additional digit.
for d in range(1, half_digits):
# previous_counts is the sum_counts for all numbers consisting of fewer digits.
previous_counts = sum_counts
# Start with an array of zeros.
sum_counts = empty_sum_counts.copy()
# For each digit b in our base that will contribute to the sum.
for b in range(base):
for i in range(d * (base - 1) + 1):
# Add occurrences of the sum i + b using occurrences of the
# sum i using only the previous digits.
sum_counts[i + b] += previous_counts[i]
# Sum the squares of the counts for each sum of digits in a group.
# Imagine going through all possible sums of digits in a group.
# Say that the sum s occurs x = counts[s] times for each group
# of half of the digits. For each of the x occurrences in the first group,
# there are x occurrences of that sum in the second group, so add x * x instances
# where the sum of the two digit groups is the same.
# The total is the number of lucky tickets.
return sum(x*x for x in sum_counts)
Mahjong
Niven Numbers
Pernicious Numbers
Prime Numbers
Proximity Grid
Quine
/tmp/code.abc
.United States
Vampire Numbers
λ, π, τ…
floor(10^1000 * π)
and then print that but with a decimal point squeezed in.