87 lines
3.5 KiB
Plaintext
87 lines
3.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "802ccaa1",
|
|
"metadata": {},
|
|
"source": [
|
|
"# [Highly Divisible Triangular Number](https://projecteuler.net/problem=12)\n",
|
|
"\n",
|
|
"Yet another problem with a straightforward solution thanks to SageMath's preinstalled functions. We'll also once again apply the closed formula for triangle numbers (see [problem 1](https://projecteuler.net/problem=1))."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "c6249c71",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"76576500\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for n in PositiveIntegers():\n",
|
|
" t = n * (n+1) / 2\n",
|
|
" if number_of_divisors(t) > 500:\n",
|
|
" print(t)\n",
|
|
" break"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b504d3c6",
|
|
"metadata": {},
|
|
"source": [
|
|
"If we wanted to implement our own [divisor counting function](https://en.wikipedia.org/wiki/Divisor_function) (denoted $\\sigma_0$), we could apply a factorization algorithm (as discussed for [problem 3](https://projecteuler.net/problem=3)) to compute each triangle number's prime factorization\n",
|
|
"$$n = 2^{r_1} 3^{r_2} 5^{r_3} 7^{r_4} \\cdots$$\n",
|
|
"(Note that every number can be written in this form, but some exponents will be 0 if the corresponding prime number is not a divisor of $n$.)\n",
|
|
"From there, the number of divisors is simply\n",
|
|
"$$\\sigma(2^{r_1} 3^{r_2} 5^{r_3} 7^{r_4} \\cdots) = (r_1 + 1)(r_2 + 1)(r_3 + 1)\\cdots$$\n",
|
|
"For example, $12 = 2^2 \\times 3$, so $\\sigma_0(12) = (2 + 1)(1 + 1) = 6$ (the factors are 1, 2, 3, 4, 6, and 12).\n",
|
|
"\n",
|
|
"If this formula doesn't make sense to you, try thinking about it like this: a divisor of $n$ will have the same construction $2^{s_1} 3^{s_2} 5^{s_3} 7^{s_4} \\cdots$, but each exponent must be less than or equal to its corresponding exponent in the factorization of $n$ (if it's larger, it won't divide $n$). This means $s_1$ can be any value from $0,1,2,\\ldots,r_1$. There are therefore $r_1 + 1$ options for the value of $s_1$, $r_2 + 1$ options for the value of $s_2$, and so on. Each choice is independent of the others, so we can multiply the number of options for each exponent together to get the total number of options, which gets us the total number of divisors.\n",
|
|
"\n",
|
|
"Continuing with our previous example, to construct a divisor of $12=2^2 \\times 3$, our options for the power of 2 are 0, 1, or 2, and our options for the power of 3 are 0 or 1. This gives us 6 options altogether for the two exponents. Sure enough, here are the 6 divisors of 12, each composed of one of $2^0$, $2^1$, or $2^2$, and one of $3^0$ or $3^1$:\n",
|
|
"\n",
|
|
"* $1=1$\n",
|
|
"* $2=2$\n",
|
|
"* $3=3$\n",
|
|
"* $4=2^2$\n",
|
|
"* $6=2 \\times 3$\n",
|
|
"* $12=2^2 \\times 3$\n",
|
|
"\n",
|
|
"## Relevant sequences\n",
|
|
"* Number of divisors: [A000005](https://oeis.org/A000005)\n",
|
|
"* Triangular numbers: [A000217](https://oeis.org/A000217)\n",
|
|
"* Number of divisors of triangular numbers: [A063440](https://oeis.org/A063440)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "SageMath 9.5",
|
|
"language": "sage",
|
|
"name": "sagemath"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|