96 lines
3.8 KiB
Plaintext
96 lines
3.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "802ccaa1",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Highly Divisible Triangular Number\n",
|
|
"> The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be $1+2+3+4+5+6+7=28$. The first ten terms would be:\n",
|
|
"> $$1,3,6,10,15,21,28,36,45,55,\\ldots$$\n",
|
|
"> Let us list the factors of the first seven triangle numbers:\n",
|
|
"> * 1: 1\n",
|
|
"> * 3: 1,3\n",
|
|
"> * 6: 1,2,3,6\n",
|
|
"> * 10: 1,2,5,10\n",
|
|
"> * 15: 1,3,5,15\n",
|
|
"> * 21: 1,3,7,21\n",
|
|
"> * 28: 1,2,4,7,14,28\n",
|
|
"> \n",
|
|
"> We can see that 28 is the first triangle number to have over five divisors.\n",
|
|
">\n",
|
|
"> What is the value of the first triangle number to have over five hundred divisors?\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."
|
|
]
|
|
},
|
|
{
|
|
"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"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|