mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-25 14:01:29 -05:00
chore(i18n): sync chinese translations (#49833
* chore: sync euler files * chore: sync translations
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
---
|
||||
id: 61531b20cc9dfa2741a5b800
|
||||
title: Python 与大学代数认证
|
||||
certification: college-algebra-with-python
|
||||
challengeType: 7
|
||||
isPrivate: true
|
||||
tests:
|
||||
-
|
||||
id: 63d83ff239c73468b059cd3f
|
||||
title: 多功能计算器
|
||||
title: Build a Multi-Function Calculator
|
||||
-
|
||||
id: 63d83ffd39c73468b059cd40
|
||||
title: 图形计算器
|
||||
title: Build a Graphing Calculator
|
||||
-
|
||||
id: 63d8401039c73468b059cd41
|
||||
title: 三个数学游戏
|
||||
title: Build Three Math Games
|
||||
-
|
||||
id: 63d8401e39c73468b059cd42
|
||||
title: 财务计算器
|
||||
title: Build a Financial Calculator
|
||||
-
|
||||
id: 63d8402e39c73468b059cd43
|
||||
title: 数据图表浏览器
|
||||
title: Build a Data Graph Explorer
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
---
|
||||
id: 63d8402e39c73468b059cd43
|
||||
title: "Data Graph Explorer"
|
||||
title: "Build a Data Graph Explorer"
|
||||
challengeType: 10
|
||||
dashedName: data-graph-explorer
|
||||
dashedName: build-a-data-graph-explorer
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
You will be <a href="https://colab.research.google.com/github/freeCodeCamp/boilerplate-cat-and-dog-image-classifier/blob/master/fcc_cat_dog.ipynb" target="_blank" rel="noopener noreferrer nofollow">working on this project with Google Colaboratory</a>.
|
||||
You will be <a href="https://colab.research.google.com/#create=true" target="_blank" rel="noopener noreferrer nofollow">working on this project with Google Colaboratory</a>.
|
||||
|
||||
After going to that link, create a copy of the notebook either in your own account or locally. Once you complete the project and it passes the test (included at that link), submit your project link below. If you are submitting a Google Colaboratory link, make sure to turn on link sharing for "anyone with the link."
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 63d8401e39c73468b059cd42
|
||||
title: "Financial Calculator"
|
||||
title: "Build a Financial Calculator"
|
||||
challengeType: 10
|
||||
dashedName: financial-calculator
|
||||
dashedName: build-a-financial-calculator
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 63d83ffd39c73468b059cd40
|
||||
title: "Graphing Calculator"
|
||||
title: "Build a Graphing Calculator"
|
||||
challengeType: 10
|
||||
dashedName: graphing-calculator
|
||||
dashedName: build-a-graphing-calculator
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 63d83ff239c73468b059cd3f
|
||||
title: "Multi-Function Calculator"
|
||||
title: "Build a Multi-Function Calculator"
|
||||
challengeType: 10
|
||||
dashedName: multi-function-calculator
|
||||
dashedName: build-a-multi-function-calculator
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -1,13 +1,13 @@
|
||||
---
|
||||
id: 63d8401039c73468b059cd41
|
||||
title: "Three Math Games"
|
||||
title: "Build Three Math Games"
|
||||
challengeType: 10
|
||||
dashedName: three-math-games
|
||||
dashedName: build-three-math-games
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
You will be <a href="https://colab.research.google.com/github/freeCodeCamp/boilerplate-cat-and-dog-image-classifier/blob/master/fcc_cat_dog.ipynb" target="_blank" rel="noopener noreferrer nofollow">working on this project with Google Colaboratory</a>.
|
||||
You will be <a href="https://colab.research.google.com/#create=true" target="_blank" rel="noopener noreferrer nofollow">working on this project with Google Colaboratory</a>.
|
||||
|
||||
After going to that link, create a copy of the notebook either in your own account or locally. Once you complete the project and it passes the test (included at that link), submit your project link below. If you are submitting a Google Colaboratory link, make sure to turn on link sharing for "anyone with the link."
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
id: 6363d2959078df117ce4c408
|
||||
title: "More Resources in Colab"
|
||||
challengeType: 15
|
||||
videoId: L5hh3Og9RCU
|
||||
videoId: HNFrRHqpck4
|
||||
dashedName: more-resources-in-colab
|
||||
---
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
id: 6363d2899078df117ce4c407
|
||||
title: "Spreadsheets and Additional Resources"
|
||||
challengeType: 15
|
||||
videoId: HNFrRHqpck4
|
||||
videoId: JH9Uk2mi3Dc
|
||||
dashedName: spreadsheets-and-additional-resources
|
||||
---
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ dashedName: exponents-and-logarithms
|
||||
|
||||
# --description--
|
||||
|
||||
Here is the <a href="https://colab.research.google.com/drive/1IVBaeX84arJXS73raRROaxbz4qMyFVb6?usp=sharing" target="_blank" rel="noopener noreferrer nofollow">Colab notebook to go along with this video.</a>
|
||||
Here is the <a href="https://colab.research.google.com/drive/1hg7ecxGT20B8HR2mV75HzMylj9SHIWH8?usp=sharing" target="_blank" rel="noopener noreferrer nofollow">Colab notebook to go along with this video.</a>
|
||||
|
||||
# --question--
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ dashedName: fractions-and-decimals-extra
|
||||
|
||||
The following video will show you one way to set up your Google Colaboratory notebook, so that you can continue to build your personalized algebra calculator.
|
||||
|
||||
Here is <a href="https://colab.research.google.com/drive/1ND_gULby6RWU4Zs27Z7V5WK2g7fHPk1b?usp=sharing" target="_blank" rel="noopener noreferrer nofollow">the Colab notebook used in this video</a> so you can use it as a model.
|
||||
Here is <a href="https://colab.research.google.com/drive/1a_RtRtVfeO0m2528T4V-bCXozWf3HpM7?usp=sharing" target="_blank" rel="noopener noreferrer nofollow">the Colab notebook used in this video</a> so you can use it as a model.
|
||||
|
||||
# --question--
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ dashedName: graphing-systems
|
||||
|
||||
This first video will show you how to graph systems of equations with with written math, then code.
|
||||
|
||||
Here is the <a href="https://colab.research.google.com/drive/1tB7N3QqHEbGk33v0BdTwZTVkS9ju9yn6?usp=sharing" target="_blank" rel="noopener noreferrer nofollow">Colab notebook used in this video.</a>
|
||||
Here is the <a href="https://colab.research.google.com/drive/1N1JEZJctODxsntROnmg0VqMSHXYdIlFD?usp=sharing" target="_blank" rel="noopener noreferrer nofollow">Colab notebook used in this video.</a>
|
||||
|
||||
# --question--
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ Open the following Colab notebook, run the cell, and <a href="https://colab.rese
|
||||
|
||||
## --text--
|
||||
|
||||
If you import sympy and define x as a variable, what would be the input from the following code?
|
||||
If you import sympy and define x as a variable, what would be the output from the following code?
|
||||
|
||||
```py
|
||||
example = 3*x - 12
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 5900f36e1000cf542c50fe80
|
||||
title: 'Problem 1: Multiples of 3 and 5'
|
||||
challengeType: 1
|
||||
forumTopicId: 301722
|
||||
dashedName: problem-1-multiples-of-3-and-5
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
|
||||
|
||||
Find the sum of all the multiples of 3 or 5 below the provided parameter value `number`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`multiplesOf3and5(10)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof multiplesOf3and5(10) === 'number');
|
||||
```
|
||||
|
||||
`multiplesOf3and5(49)` should return 543.
|
||||
|
||||
```js
|
||||
assert.strictEqual(multiplesOf3and5(49), 543);
|
||||
```
|
||||
|
||||
`multiplesOf3and5(1000)` should return 233168.
|
||||
|
||||
```js
|
||||
assert.strictEqual(multiplesOf3and5(1000), 233168);
|
||||
```
|
||||
|
||||
`multiplesOf3and5(8456)` should return 16687353.
|
||||
|
||||
```js
|
||||
assert.strictEqual(multiplesOf3and5(8456), 16687353);
|
||||
```
|
||||
|
||||
`multiplesOf3and5(19564)` should return 89301183.
|
||||
|
||||
```js
|
||||
assert.strictEqual(multiplesOf3and5(19564), 89301183);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function multiplesOf3and5(number) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
multiplesOf3and5(1000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const multiplesOf3and5 = (number) => {
|
||||
var total = 0;
|
||||
|
||||
for(var i = 0; i < number; i++) {
|
||||
if(i % 3 == 0 || i % 5 == 0) {
|
||||
total += i;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
};
|
||||
```
|
||||
@@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 5900f3761000cf542c50fe89
|
||||
title: 'Problem 10: Summation of primes'
|
||||
challengeType: 1
|
||||
forumTopicId: 301723
|
||||
dashedName: problem-10-summation-of-primes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
|
||||
Find the sum of all the primes below `n`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`primeSummation(17)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof primeSummation(17) === 'number');
|
||||
```
|
||||
|
||||
`primeSummation(17)` should return 41.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeSummation(17), 41);
|
||||
```
|
||||
|
||||
`primeSummation(2001)` should return 277050.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeSummation(2001), 277050);
|
||||
```
|
||||
|
||||
`primeSummation(140759)` should return 873608362.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeSummation(140759), 873608362);
|
||||
```
|
||||
|
||||
`primeSummation(2000000)` should return 142913828922.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeSummation(2000000), 142913828922);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function primeSummation(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
primeSummation(2000000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class PrimeSeive {
|
||||
constructor(num) {
|
||||
const seive = Array(Math.floor((num - 1) / 2)).fill(true);
|
||||
const upper = Math.floor((num - 1) / 2);
|
||||
const sqrtUpper = Math.floor((Math.sqrt(num) - 1) / 2);
|
||||
|
||||
for (let i = 0; i <= sqrtUpper; i++) {
|
||||
if (seive[i]) {
|
||||
// Mark value in seive array
|
||||
const prime = 2 * i + 3;
|
||||
// Mark all multiples of this number as false (not prime)
|
||||
const primeSqaredIndex = 2 * i ** 2 + 6 * i + 3;
|
||||
for (let j = primeSqaredIndex; j < upper; j += prime) {
|
||||
seive[j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this._seive = seive;
|
||||
}
|
||||
|
||||
isPrime(num) {
|
||||
return num === 2
|
||||
? true
|
||||
: num % 2 === 0
|
||||
? false
|
||||
: this.isOddPrime(num);
|
||||
}
|
||||
|
||||
isOddPrime(num) {
|
||||
return this._seive[(num - 3) / 2];
|
||||
}
|
||||
};
|
||||
|
||||
function primeSummation(num) {
|
||||
const primeSeive = new PrimeSeive(num);
|
||||
|
||||
let sum = 2;
|
||||
for (let i = 3; i < num; i += 2) {
|
||||
if (primeSeive.isOddPrime(i)) sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,87 @@
|
||||
---
|
||||
id: 5900f3d01000cf542c50fee3
|
||||
title: 'Problem 100: Arranged probability'
|
||||
challengeType: 1
|
||||
forumTopicId: 301724
|
||||
dashedName: problem-100-arranged-probability
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
If a box contains twenty-one colored discs, composed of fifteen blue discs and six red discs, and two discs were taken at random, it can be seen that the probability of taking two blue discs.
|
||||
|
||||
$${P(BB)} = \frac{15}{21}×\frac{14}{20} = \frac{1}{2}$$
|
||||
|
||||
The next such arrangement, for which there is exactly a 50% chance of taking two blue discs at random, is a box containing eighty-five blue discs and thirty-five red discs.
|
||||
|
||||
By finding the first arrangement to contain over `limit` discs in total, determine the number of blue discs that the box would contain.
|
||||
|
||||
# --hints--
|
||||
|
||||
`arrangedProbability(20)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof arrangedProbability(10) === 'number');
|
||||
```
|
||||
|
||||
`arrangedProbability(20)` should return `15`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(arrangedProbability(20), 15);
|
||||
```
|
||||
|
||||
`arrangedProbability(100)` should return `85`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(arrangedProbability(100), 85);
|
||||
```
|
||||
|
||||
`arrangedProbability(100000)` should return `97513`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(arrangedProbability(100000), 97513);
|
||||
```
|
||||
|
||||
`arrangedProbability(1000000000)` should return `3822685023`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(arrangedProbability(1000000000), 3822685023);
|
||||
```
|
||||
|
||||
`arrangedProbability(1000000000000)` should return `756872327473`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(arrangedProbability(1000000000000), 756872327473);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function arrangedProbability(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
arrangedProbability(20);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function arrangedProbability(limit) {
|
||||
// Based on https://www.mathblog.dk/project-euler-100-blue-discs-two-blue/
|
||||
let blue = 15;
|
||||
let discs = 21;
|
||||
|
||||
while (discs < limit) {
|
||||
const nextBlue = 3 * blue + 2 * discs - 2;
|
||||
const nextDiscs = 4 * blue + 3 * discs - 3;
|
||||
|
||||
blue = nextBlue;
|
||||
discs = nextDiscs;
|
||||
}
|
||||
return blue;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,182 @@
|
||||
---
|
||||
id: 5900f3781000cf542c50fe8a
|
||||
title: 'Problem 11: Largest product in a grid'
|
||||
challengeType: 1
|
||||
forumTopicId: 301734
|
||||
dashedName: problem-11-largest-product-in-a-grid
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
|
||||
|
||||
<div style='text-align: center;'>
|
||||
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08<br>
|
||||
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00<br>
|
||||
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65<br>
|
||||
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91<br>
|
||||
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80<br>
|
||||
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50<br>
|
||||
32 98 81 28 64 23 67 10 <span style='color: red'><b>26</b></span> 38 40 67 59 54 70 66 18 38 64 70<br>
|
||||
67 26 20 68 02 62 12 20 95 <span style='color: red'><b>63</b></span> 94 39 63 08 40 91 66 49 94 21<br>
|
||||
24 55 58 05 66 73 99 26 97 17 <span style='color: red'><b>78</b></span> 78 96 83 14 88 34 89 63 72<br>
|
||||
21 36 23 09 75 00 76 44 20 45 35 <span style='color: red'><b>14</b></span> 00 61 33 97 34 31 33 95<br>
|
||||
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92<br>
|
||||
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57<br>
|
||||
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58<br>
|
||||
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40<br>
|
||||
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66<br>
|
||||
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69<br>
|
||||
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36<br>
|
||||
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16<br>
|
||||
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54<br>
|
||||
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48<br>
|
||||
</div>
|
||||
|
||||
The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
|
||||
|
||||
What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in a given `arr` grid?
|
||||
|
||||
# --hints--
|
||||
|
||||
`largestGridProduct(testGrid)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof largestGridProduct(testGrid) === 'number');
|
||||
```
|
||||
|
||||
`largestGridProduct(testGrid)` should return 14169081.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestGridProduct(testGrid), 14169081);
|
||||
```
|
||||
|
||||
`largestGridProduct(grid)` should return 70600674.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestGridProduct(grid), 70600674);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function largestGridProduct(arr) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
const grid = [
|
||||
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
|
||||
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
|
||||
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
|
||||
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
|
||||
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
|
||||
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
|
||||
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
|
||||
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
|
||||
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
|
||||
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
|
||||
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
|
||||
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
|
||||
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
|
||||
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
|
||||
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
|
||||
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
|
||||
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
|
||||
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
|
||||
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
|
||||
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
|
||||
];
|
||||
|
||||
const testGrid = [
|
||||
[40, 17, 81, 18, 57],
|
||||
[74, 4, 36, 16, 29],
|
||||
[36, 42, 69, 73, 45],
|
||||
[51, 54, 69, 16, 92],
|
||||
[7, 97, 57, 32, 16]
|
||||
];
|
||||
|
||||
largestGridProduct(testGrid);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function largestGridProduct(arr) {
|
||||
let maxProduct = 0;
|
||||
let currProduct = 0;
|
||||
|
||||
function maxProductChecker(n) {
|
||||
if (n > maxProduct) {
|
||||
return maxProduct = n;
|
||||
}
|
||||
}
|
||||
|
||||
// loop rows
|
||||
for (let r = 0; r < arr.length; r++) {
|
||||
// loop columns
|
||||
for (let c = 0; c < arr[r].length; c++) {
|
||||
const limit = arr[r].length - 3;
|
||||
|
||||
// check horizontal
|
||||
if (c < limit) {
|
||||
currProduct = arr[r][c] * arr[r][c + 1] * arr[r][c + 2] * arr[r][c + 3];
|
||||
maxProductChecker(currProduct);
|
||||
}
|
||||
|
||||
// check vertical
|
||||
if (r < limit) {
|
||||
currProduct = arr[r][c] * arr[r + 1][c] * arr[r + 2][c] * arr[r + 3][c];
|
||||
maxProductChecker(currProduct);
|
||||
}
|
||||
|
||||
// check diagonal [\]
|
||||
if (c < limit && r < limit) {
|
||||
currProduct = arr[r][c] * arr[r + 1][c + 1] * arr[r + 2][c + 2] * arr[r + 3][c + 3];
|
||||
maxProductChecker(currProduct);
|
||||
}
|
||||
|
||||
// check diagonal [/]
|
||||
if (c > 3 && r < limit) {
|
||||
currProduct = arr[r][c] * arr[r + 1][c - 1] * arr[r + 2][c - 2] * arr[r + 3][c - 3];
|
||||
maxProductChecker(currProduct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxProduct;
|
||||
}
|
||||
|
||||
const grid = [ [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
|
||||
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
|
||||
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
|
||||
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
|
||||
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
|
||||
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
|
||||
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
|
||||
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
|
||||
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
|
||||
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
|
||||
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
|
||||
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
|
||||
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
|
||||
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
|
||||
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
|
||||
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
|
||||
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
|
||||
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
|
||||
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
|
||||
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
|
||||
];
|
||||
|
||||
const testGrid = [
|
||||
[40, 17, 81, 18, 57],
|
||||
[74, 4, 36, 16, 29],
|
||||
[36, 42, 69, 73, 45],
|
||||
[51, 54, 69, 16, 92],
|
||||
[7, 97, 57, 32, 16]
|
||||
];
|
||||
```
|
||||
@@ -0,0 +1,139 @@
|
||||
---
|
||||
id: 5900f3781000cf542c50fe8b
|
||||
title: 'Problem 12: Highly divisible triangular number'
|
||||
challengeType: 1
|
||||
forumTopicId: 301746
|
||||
dashedName: problem-12-highly-divisible-triangular-number
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
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:
|
||||
|
||||
<div style='text-align: center;'>1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...</div>
|
||||
|
||||
Let us list the factors of the first seven triangle numbers:
|
||||
|
||||
<div style='padding-left: 4em;'><b>1:</b> 1</div>
|
||||
<div style='padding-left: 4em;'><b>3:</b> 1, 3</div>
|
||||
<div style='padding-left: 4em;'><b>6:</b> 1, 2, 3, 6</div>
|
||||
<div style='padding-left: 4em;'><b>10:</b> 1, 2, 5, 10</div>
|
||||
<div style='padding-left: 4em;'><b>15:</b> 1, 3, 5, 15</div>
|
||||
<div style='padding-left: 4em;'><b>21:</b> 1, 3, 7, 21</div>
|
||||
<div style='padding-left: 4em;'><b>28:</b> 1, 2, 4, 7, 14, 28</div>
|
||||
|
||||
We can see that 28 is the first triangle number to have over five divisors.
|
||||
|
||||
What is the value of the first triangle number to have over `n` divisors?
|
||||
|
||||
# --hints--
|
||||
|
||||
`divisibleTriangleNumber(5)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof divisibleTriangleNumber(5) === 'number');
|
||||
```
|
||||
|
||||
`divisibleTriangleNumber(5)` should return 28.
|
||||
|
||||
```js
|
||||
assert.strictEqual(divisibleTriangleNumber(5), 28);
|
||||
```
|
||||
|
||||
`divisibleTriangleNumber(23)` should return 630.
|
||||
|
||||
```js
|
||||
assert.strictEqual(divisibleTriangleNumber(23), 630);
|
||||
```
|
||||
|
||||
`divisibleTriangleNumber(167)` should return 1385280.
|
||||
|
||||
```js
|
||||
assert.strictEqual(divisibleTriangleNumber(167), 1385280);
|
||||
```
|
||||
|
||||
`divisibleTriangleNumber(374)` should return 17907120.
|
||||
|
||||
```js
|
||||
assert.strictEqual(divisibleTriangleNumber(374), 17907120);
|
||||
```
|
||||
|
||||
`divisibleTriangleNumber(500)` should return 76576500.
|
||||
|
||||
```js
|
||||
assert.strictEqual(divisibleTriangleNumber(500), 76576500);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function divisibleTriangleNumber(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
divisibleTriangleNumber(500);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function divisibleTriangleNumber(n) {
|
||||
if (n === 1) return 3;
|
||||
let counter = 1;
|
||||
let triangleNumber = counter++;
|
||||
|
||||
|
||||
while (noOfFactors(triangleNumber) < n) {
|
||||
triangleNumber += counter++;
|
||||
}
|
||||
return triangleNumber;
|
||||
}
|
||||
|
||||
function noOfFactors(num) {
|
||||
const primeFactors = getPrimeFactors(num);
|
||||
let prod = 1;
|
||||
for(let p in primeFactors) {
|
||||
prod *= (primeFactors[p] + 1)
|
||||
}
|
||||
return prod;
|
||||
}
|
||||
|
||||
function getPrimeFactors(num) {
|
||||
let n = num;
|
||||
let primes = {};
|
||||
|
||||
let p = 2;
|
||||
let sqrt = Math.sqrt(num);
|
||||
|
||||
function checkAndUpdate(inc) {
|
||||
if (n % p === 0) {
|
||||
const curr = primes[p];
|
||||
if (curr) {
|
||||
primes[p]++
|
||||
} else {
|
||||
primes[p] = 1;
|
||||
}
|
||||
n /= p;
|
||||
} else {
|
||||
p += inc;
|
||||
}
|
||||
}
|
||||
|
||||
while(p === 2 && p <= n) {
|
||||
checkAndUpdate(1);
|
||||
}
|
||||
|
||||
while (p <= n && p <= sqrt) {
|
||||
checkAndUpdate(2);
|
||||
}
|
||||
if(Object.keys(primes).length === 0) {
|
||||
primes[num] = 1;
|
||||
} else if(n !== 1) {
|
||||
primes[n] = 1;
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,286 @@
|
||||
---
|
||||
id: 5900f37a1000cf542c50fe8c
|
||||
title: 'Problem 13: Large sum'
|
||||
challengeType: 1
|
||||
forumTopicId: 301757
|
||||
dashedName: problem-13-large-sum
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
|
||||
|
||||
<div style='padding-left: 4em;'>
|
||||
37107287533902102798797998220837590246510135740250<br>
|
||||
46376937677490009712648124896970078050417018260538<br>
|
||||
74324986199524741059474233309513058123726617309629<br>
|
||||
91942213363574161572522430563301811072406154908250<br>
|
||||
23067588207539346171171980310421047513778063246676<br>
|
||||
89261670696623633820136378418383684178734361726757<br>
|
||||
28112879812849979408065481931592621691275889832738<br>
|
||||
44274228917432520321923589422876796487670272189318<br>
|
||||
47451445736001306439091167216856844588711603153276<br>
|
||||
70386486105843025439939619828917593665686757934951<br>
|
||||
62176457141856560629502157223196586755079324193331<br>
|
||||
64906352462741904929101432445813822663347944758178<br>
|
||||
92575867718337217661963751590579239728245598838407<br>
|
||||
58203565325359399008402633568948830189458628227828<br>
|
||||
80181199384826282014278194139940567587151170094390<br>
|
||||
35398664372827112653829987240784473053190104293586<br>
|
||||
86515506006295864861532075273371959191420517255829<br>
|
||||
71693888707715466499115593487603532921714970056938<br>
|
||||
54370070576826684624621495650076471787294438377604<br>
|
||||
53282654108756828443191190634694037855217779295145<br>
|
||||
36123272525000296071075082563815656710885258350721<br>
|
||||
45876576172410976447339110607218265236877223636045<br>
|
||||
17423706905851860660448207621209813287860733969412<br>
|
||||
81142660418086830619328460811191061556940512689692<br>
|
||||
51934325451728388641918047049293215058642563049483<br>
|
||||
62467221648435076201727918039944693004732956340691<br>
|
||||
15732444386908125794514089057706229429197107928209<br>
|
||||
55037687525678773091862540744969844508330393682126<br>
|
||||
18336384825330154686196124348767681297534375946515<br>
|
||||
80386287592878490201521685554828717201219257766954<br>
|
||||
78182833757993103614740356856449095527097864797581<br>
|
||||
16726320100436897842553539920931837441497806860984<br>
|
||||
48403098129077791799088218795327364475675590848030<br>
|
||||
87086987551392711854517078544161852424320693150332<br>
|
||||
59959406895756536782107074926966537676326235447210<br>
|
||||
69793950679652694742597709739166693763042633987085<br>
|
||||
41052684708299085211399427365734116182760315001271<br>
|
||||
65378607361501080857009149939512557028198746004375<br>
|
||||
35829035317434717326932123578154982629742552737307<br>
|
||||
94953759765105305946966067683156574377167401875275<br>
|
||||
88902802571733229619176668713819931811048770190271<br>
|
||||
25267680276078003013678680992525463401061632866526<br>
|
||||
36270218540497705585629946580636237993140746255962<br>
|
||||
24074486908231174977792365466257246923322810917141<br>
|
||||
91430288197103288597806669760892938638285025333403<br>
|
||||
34413065578016127815921815005561868836468420090470<br>
|
||||
23053081172816430487623791969842487255036638784583<br>
|
||||
11487696932154902810424020138335124462181441773470<br>
|
||||
63783299490636259666498587618221225225512486764533<br>
|
||||
67720186971698544312419572409913959008952310058822<br>
|
||||
95548255300263520781532296796249481641953868218774<br>
|
||||
76085327132285723110424803456124867697064507995236<br>
|
||||
37774242535411291684276865538926205024910326572967<br>
|
||||
23701913275725675285653248258265463092207058596522<br>
|
||||
29798860272258331913126375147341994889534765745501<br>
|
||||
18495701454879288984856827726077713721403798879715<br>
|
||||
38298203783031473527721580348144513491373226651381<br>
|
||||
34829543829199918180278916522431027392251122869539<br>
|
||||
40957953066405232632538044100059654939159879593635<br>
|
||||
29746152185502371307642255121183693803580388584903<br>
|
||||
41698116222072977186158236678424689157993532961922<br>
|
||||
62467957194401269043877107275048102390895523597457<br>
|
||||
23189706772547915061505504953922979530901129967519<br>
|
||||
86188088225875314529584099251203829009407770775672<br>
|
||||
11306739708304724483816533873502340845647058077308<br>
|
||||
82959174767140363198008187129011875491310547126581<br>
|
||||
97623331044818386269515456334926366572897563400500<br>
|
||||
42846280183517070527831839425882145521227251250327<br>
|
||||
55121603546981200581762165212827652751691296897789<br>
|
||||
32238195734329339946437501907836945765883352399886<br>
|
||||
75506164965184775180738168837861091527357929701337<br>
|
||||
62177842752192623401942399639168044983993173312731<br>
|
||||
32924185707147349566916674687634660915035914677504<br>
|
||||
99518671430235219628894890102423325116913619626622<br>
|
||||
73267460800591547471830798392868535206946944540724<br>
|
||||
76841822524674417161514036427982273348055556214818<br>
|
||||
97142617910342598647204516893989422179826088076852<br>
|
||||
87783646182799346313767754307809363333018982642090<br>
|
||||
10848802521674670883215120185883543223812876952786<br>
|
||||
71329612474782464538636993009049310363619763878039<br>
|
||||
62184073572399794223406235393808339651327408011116<br>
|
||||
66627891981488087797941876876144230030984490851411<br>
|
||||
60661826293682836764744779239180335110989069790714<br>
|
||||
85786944089552990653640447425576083659976645795096<br>
|
||||
66024396409905389607120198219976047599490197230297<br>
|
||||
64913982680032973156037120041377903785566085089252<br>
|
||||
16730939319872750275468906903707539413042652315011<br>
|
||||
94809377245048795150954100921645863754710598436791<br>
|
||||
78639167021187492431995700641917969777599028300699<br>
|
||||
15368713711936614952811305876380278410754449733078<br>
|
||||
40789923115535562561142322423255033685442488917353<br>
|
||||
44889911501440648020369068063960672322193204149535<br>
|
||||
41503128880339536053299340368006977710650566631954<br>
|
||||
81234880673210146739058568557934581403627822703280<br>
|
||||
82616570773948327592232845941706525094512325230608<br>
|
||||
22918802058777319719839450180888072429661980811197<br>
|
||||
77158542502016545090413245809786882778948721859617<br>
|
||||
72107838435069186155435662884062257473692284509516<br>
|
||||
20849603980134001723930671666823555245252804609722<br>
|
||||
53503534226472524250874054075591789781264330331690<br>
|
||||
</div>
|
||||
|
||||
# --hints--
|
||||
|
||||
`largeSum(testNums)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof largeSum(testNums) === 'number');
|
||||
```
|
||||
|
||||
`largeSum(testNums)` should return 8348422521.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largeSum(testNums), 8348422521);
|
||||
```
|
||||
|
||||
`largeSum(fiftyDigitNums)` should return 5537376230.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largeSum(fiftyDigitNums), 5537376230);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
const fiftyDigitNums = [
|
||||
'37107287533902102798797998220837590246510135740250',
|
||||
'46376937677490009712648124896970078050417018260538',
|
||||
'74324986199524741059474233309513058123726617309629',
|
||||
'91942213363574161572522430563301811072406154908250',
|
||||
'23067588207539346171171980310421047513778063246676',
|
||||
'89261670696623633820136378418383684178734361726757',
|
||||
'28112879812849979408065481931592621691275889832738',
|
||||
'44274228917432520321923589422876796487670272189318',
|
||||
'47451445736001306439091167216856844588711603153276',
|
||||
'70386486105843025439939619828917593665686757934951',
|
||||
'62176457141856560629502157223196586755079324193331',
|
||||
'64906352462741904929101432445813822663347944758178',
|
||||
'92575867718337217661963751590579239728245598838407',
|
||||
'58203565325359399008402633568948830189458628227828',
|
||||
'80181199384826282014278194139940567587151170094390',
|
||||
'35398664372827112653829987240784473053190104293586',
|
||||
'86515506006295864861532075273371959191420517255829',
|
||||
'71693888707715466499115593487603532921714970056938',
|
||||
'54370070576826684624621495650076471787294438377604',
|
||||
'53282654108756828443191190634694037855217779295145',
|
||||
'36123272525000296071075082563815656710885258350721',
|
||||
'45876576172410976447339110607218265236877223636045',
|
||||
'17423706905851860660448207621209813287860733969412',
|
||||
'81142660418086830619328460811191061556940512689692',
|
||||
'51934325451728388641918047049293215058642563049483',
|
||||
'62467221648435076201727918039944693004732956340691',
|
||||
'15732444386908125794514089057706229429197107928209',
|
||||
'55037687525678773091862540744969844508330393682126',
|
||||
'18336384825330154686196124348767681297534375946515',
|
||||
'80386287592878490201521685554828717201219257766954',
|
||||
'78182833757993103614740356856449095527097864797581',
|
||||
'16726320100436897842553539920931837441497806860984',
|
||||
'48403098129077791799088218795327364475675590848030',
|
||||
'87086987551392711854517078544161852424320693150332',
|
||||
'59959406895756536782107074926966537676326235447210',
|
||||
'69793950679652694742597709739166693763042633987085',
|
||||
'41052684708299085211399427365734116182760315001271',
|
||||
'65378607361501080857009149939512557028198746004375',
|
||||
'35829035317434717326932123578154982629742552737307',
|
||||
'94953759765105305946966067683156574377167401875275',
|
||||
'88902802571733229619176668713819931811048770190271',
|
||||
'25267680276078003013678680992525463401061632866526',
|
||||
'36270218540497705585629946580636237993140746255962',
|
||||
'24074486908231174977792365466257246923322810917141',
|
||||
'91430288197103288597806669760892938638285025333403',
|
||||
'34413065578016127815921815005561868836468420090470',
|
||||
'23053081172816430487623791969842487255036638784583',
|
||||
'11487696932154902810424020138335124462181441773470',
|
||||
'63783299490636259666498587618221225225512486764533',
|
||||
'67720186971698544312419572409913959008952310058822',
|
||||
'95548255300263520781532296796249481641953868218774',
|
||||
'76085327132285723110424803456124867697064507995236',
|
||||
'37774242535411291684276865538926205024910326572967',
|
||||
'23701913275725675285653248258265463092207058596522',
|
||||
'29798860272258331913126375147341994889534765745501',
|
||||
'18495701454879288984856827726077713721403798879715',
|
||||
'38298203783031473527721580348144513491373226651381',
|
||||
'34829543829199918180278916522431027392251122869539',
|
||||
'40957953066405232632538044100059654939159879593635',
|
||||
'29746152185502371307642255121183693803580388584903',
|
||||
'41698116222072977186158236678424689157993532961922',
|
||||
'62467957194401269043877107275048102390895523597457',
|
||||
'23189706772547915061505504953922979530901129967519',
|
||||
'86188088225875314529584099251203829009407770775672',
|
||||
'11306739708304724483816533873502340845647058077308',
|
||||
'82959174767140363198008187129011875491310547126581',
|
||||
'97623331044818386269515456334926366572897563400500',
|
||||
'42846280183517070527831839425882145521227251250327',
|
||||
'55121603546981200581762165212827652751691296897789',
|
||||
'32238195734329339946437501907836945765883352399886',
|
||||
'75506164965184775180738168837861091527357929701337',
|
||||
'62177842752192623401942399639168044983993173312731',
|
||||
'32924185707147349566916674687634660915035914677504',
|
||||
'99518671430235219628894890102423325116913619626622',
|
||||
'73267460800591547471830798392868535206946944540724',
|
||||
'76841822524674417161514036427982273348055556214818',
|
||||
'97142617910342598647204516893989422179826088076852',
|
||||
'87783646182799346313767754307809363333018982642090',
|
||||
'10848802521674670883215120185883543223812876952786',
|
||||
'71329612474782464538636993009049310363619763878039',
|
||||
'62184073572399794223406235393808339651327408011116',
|
||||
'66627891981488087797941876876144230030984490851411',
|
||||
'60661826293682836764744779239180335110989069790714',
|
||||
'85786944089552990653640447425576083659976645795096',
|
||||
'66024396409905389607120198219976047599490197230297',
|
||||
'64913982680032973156037120041377903785566085089252',
|
||||
'16730939319872750275468906903707539413042652315011',
|
||||
'94809377245048795150954100921645863754710598436791',
|
||||
'78639167021187492431995700641917969777599028300699',
|
||||
'15368713711936614952811305876380278410754449733078',
|
||||
'40789923115535562561142322423255033685442488917353',
|
||||
'44889911501440648020369068063960672322193204149535',
|
||||
'41503128880339536053299340368006977710650566631954',
|
||||
'81234880673210146739058568557934581403627822703280',
|
||||
'82616570773948327592232845941706525094512325230608',
|
||||
'22918802058777319719839450180888072429661980811197',
|
||||
'77158542502016545090413245809786882778948721859617',
|
||||
'72107838435069186155435662884062257473692284509516',
|
||||
'20849603980134001723930671666823555245252804609722',
|
||||
'53503534226472524250874054075591789781264330331690'
|
||||
];
|
||||
|
||||
const testNums = [
|
||||
'37107287533902102798797998220837590246510135740250',
|
||||
'46376937677490009712648124896970078050417018260538'
|
||||
];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function largeSum(arr) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
const testNums = [
|
||||
'37107287533902102798797998220837590246510135740250',
|
||||
'46376937677490009712648124896970078050417018260538'
|
||||
];
|
||||
|
||||
largeSum(testNums);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function largeSum(arr) {
|
||||
|
||||
let sum = 0;
|
||||
|
||||
arr.forEach(function(num) {
|
||||
sum += parseInt(num, 10);
|
||||
});
|
||||
|
||||
sum = sum.toString(10);
|
||||
|
||||
sum = sum.substring(0, 1) + sum.substring(2);
|
||||
|
||||
let firstTen = sum.slice(0, 10);
|
||||
return parseInt(firstTen, 10);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,111 @@
|
||||
---
|
||||
id: 5900f37a1000cf542c50fe8d
|
||||
title: 'Problem 14: Longest Collatz sequence'
|
||||
challengeType: 1
|
||||
forumTopicId: 301768
|
||||
dashedName: problem-14-longest-collatz-sequence
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The following iterative sequence is defined for the set of positive integers:
|
||||
|
||||
<div style='padding-left: 4em;'><var>n</var> → <var>n</var>/2 (<var>n</var> is even)</div>
|
||||
|
||||
<div style='padding-left: 4em;'><var>n</var> → 3<var>n</var> + 1 (<var>n</var> is odd)</div>
|
||||
|
||||
Using the rule above and starting with 13, we generate the following sequence:
|
||||
|
||||
<div style='text-align: center;'>13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1</div>
|
||||
|
||||
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
|
||||
|
||||
Which starting number, under the given `limit`, produces the longest chain?
|
||||
|
||||
**Note:** Once the chain starts the terms are allowed to go above `limit`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`longestCollatzSequence(14)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof longestCollatzSequence(14) === 'number');
|
||||
```
|
||||
|
||||
`longestCollatzSequence(14)` should return 9.
|
||||
|
||||
```js
|
||||
assert.strictEqual(longestCollatzSequence(14), 9);
|
||||
```
|
||||
|
||||
`longestCollatzSequence(5847)` should return 3711.
|
||||
|
||||
```js
|
||||
assert.strictEqual(longestCollatzSequence(5847), 3711);
|
||||
```
|
||||
|
||||
`longestCollatzSequence(46500)` should return 35655.
|
||||
|
||||
```js
|
||||
assert.strictEqual(longestCollatzSequence(46500), 35655);
|
||||
```
|
||||
|
||||
`longestCollatzSequence(54512)` should return 52527.
|
||||
|
||||
```js
|
||||
assert.strictEqual(longestCollatzSequence(54512), 52527);
|
||||
```
|
||||
|
||||
`longestCollatzSequence(100000)` should return 77031.
|
||||
|
||||
```js
|
||||
assert.strictEqual(longestCollatzSequence(100000), 77031);
|
||||
```
|
||||
|
||||
`longestCollatzSequence(1000000)` should return 837799.
|
||||
|
||||
```js
|
||||
assert.strictEqual(longestCollatzSequence(1000000), 837799);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function longestCollatzSequence(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
longestCollatzSequence(14);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function longestCollatzSequence(limit) {
|
||||
let longest = 1;
|
||||
let maxLength = 1;
|
||||
for (let i = Math.floor(limit / 2); i < limit; i++) {
|
||||
let len = colLen(i);
|
||||
if (len > maxLength) {
|
||||
longest = i;
|
||||
maxLength = len;
|
||||
}
|
||||
}
|
||||
return longest;
|
||||
}
|
||||
|
||||
const knownSequence = { '1': 1 };
|
||||
|
||||
function colLen(n) {
|
||||
if (knownSequence[n]) {
|
||||
return knownSequence[n];
|
||||
} else {
|
||||
const len = n % 2 === 0 ? colLen(n / 2) + 1 : colLen((3 * n + 1) / 2) + 2;
|
||||
knownSequence[n] = len;
|
||||
return len;
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 5900f37b1000cf542c50fe8e
|
||||
title: 'Problem 15: Lattice paths'
|
||||
challengeType: 1
|
||||
forumTopicId: 301780
|
||||
dashedName: problem-15-lattice-paths
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
|
||||
|
||||
<img class="img-responsive center-block" alt="a diagram of 6 2 by 2 grids showing all the routes to the bottom right corner" src="https://cdn-media-1.freecodecamp.org/project-euler/1Atixoj.gif" style="background-color: white; padding: 10px;" />
|
||||
|
||||
How many such routes are there through a given `gridSize`?
|
||||
|
||||
# --hints--
|
||||
|
||||
`latticePaths(4)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof latticePaths(4) === 'number');
|
||||
```
|
||||
|
||||
`latticePaths(4)` should return 70.
|
||||
|
||||
```js
|
||||
assert.strictEqual(latticePaths(4), 70);
|
||||
```
|
||||
|
||||
`latticePaths(9)` should return 48620.
|
||||
|
||||
```js
|
||||
assert.strictEqual(latticePaths(9), 48620);
|
||||
```
|
||||
|
||||
`latticePaths(20)` should return 137846528820.
|
||||
|
||||
```js
|
||||
assert.strictEqual(latticePaths(20), 137846528820);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function latticePaths(gridSize) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
latticePaths(4);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function latticePaths(gridSize) {
|
||||
let paths = 1;
|
||||
|
||||
for (let i = 0; i < gridSize; i++) {
|
||||
paths *= (2 * gridSize) - i;
|
||||
paths /= i + 1;
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 5900f37d1000cf542c50fe8f
|
||||
title: 'Problem 16: Power digit sum'
|
||||
challengeType: 1
|
||||
forumTopicId: 301791
|
||||
dashedName: problem-16-power-digit-sum
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
2<sup>15</sup> = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
|
||||
|
||||
What is the sum of the digits of the number 2<sup><code>exponent</code></sup>?
|
||||
|
||||
# --hints--
|
||||
|
||||
`powerDigitSum(15)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof powerDigitSum(15) === 'number');
|
||||
```
|
||||
|
||||
`powerDigitSum(15)` should return 26.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerDigitSum(15), 26);
|
||||
```
|
||||
|
||||
`powerDigitSum(128)` should return 166.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerDigitSum(128), 166);
|
||||
```
|
||||
|
||||
`powerDigitSum(1000)` should return 1366.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerDigitSum(1000), 1366);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function powerDigitSum(exponent) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
powerDigitSum(15);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function powerDigitSum(exponent) {
|
||||
const bigNum = [1];
|
||||
let sum = 0;
|
||||
|
||||
for (let i = 1; i <= exponent; i++) {
|
||||
let count = bigNum.length + 1;
|
||||
let overflow = 0;
|
||||
for (let j = 0; j < count; j++) {
|
||||
let digit = bigNum[j] || 0;
|
||||
digit = 2 * digit + overflow;
|
||||
|
||||
if (digit > 9) {
|
||||
digit -= 10;
|
||||
overflow = 1;
|
||||
} else {
|
||||
overflow = 0;
|
||||
}
|
||||
|
||||
bigNum[j] = digit;
|
||||
}
|
||||
}
|
||||
|
||||
bigNum.forEach(function(num) {
|
||||
return sum += num;
|
||||
});
|
||||
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,130 @@
|
||||
---
|
||||
id: 5900f37d1000cf542c50fe90
|
||||
title: 'Problem 17: Number letter counts'
|
||||
challengeType: 1
|
||||
forumTopicId: 301804
|
||||
dashedName: problem-17-number-letter-counts
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
|
||||
|
||||
If all the numbers from 1 to given `limit` inclusive were written out in words, how many letters would be used?
|
||||
|
||||
**Note:** Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
|
||||
|
||||
# --hints--
|
||||
|
||||
`numberLetterCounts(5)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof numberLetterCounts(5) === 'number');
|
||||
```
|
||||
|
||||
`numberLetterCounts(5)` should return 19.
|
||||
|
||||
```js
|
||||
assert.strictEqual(numberLetterCounts(5), 19);
|
||||
```
|
||||
|
||||
`numberLetterCounts(150)` should return 1903.
|
||||
|
||||
```js
|
||||
assert.strictEqual(numberLetterCounts(150), 1903);
|
||||
```
|
||||
|
||||
`numberLetterCounts(1000)` should return 21124.
|
||||
|
||||
```js
|
||||
assert.strictEqual(numberLetterCounts(1000), 21124);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function numberLetterCounts(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
numberLetterCounts(5);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function numberLetterCounts(limit) {
|
||||
const dictionary = {
|
||||
0: '',
|
||||
1: 'one',
|
||||
2: 'two',
|
||||
3: 'three',
|
||||
4: 'four',
|
||||
5: 'five',
|
||||
6: 'six',
|
||||
7: 'seven',
|
||||
8: 'eight',
|
||||
9: 'nine',
|
||||
10: 'ten',
|
||||
11: 'eleven',
|
||||
12: 'twelve',
|
||||
13: 'thirteen',
|
||||
14: 'fourteen',
|
||||
15: 'fifteen',
|
||||
16: 'sixteen',
|
||||
17: 'seventeen',
|
||||
18: 'eighteen',
|
||||
19: 'nineteen',
|
||||
20: 'twenty',
|
||||
30: 'thirty',
|
||||
40: 'forty',
|
||||
50: 'fifty',
|
||||
60: 'sixty',
|
||||
70: 'seventy',
|
||||
80: 'eighty',
|
||||
90: 'ninety',
|
||||
1000: 'onethousand'
|
||||
};
|
||||
|
||||
let numString = '';
|
||||
|
||||
function convertToString(num) {
|
||||
// check dictionary for number
|
||||
if (dictionary[num]) {
|
||||
return dictionary[num];
|
||||
} else {
|
||||
const hundreds = Math.floor(num / 100);
|
||||
const tens = Math.floor((num / 10) % 10) * 10;
|
||||
const remainder = num % 10;
|
||||
|
||||
let tempStr = '';
|
||||
|
||||
if (hundreds === 0) {
|
||||
tempStr += dictionary[tens] + dictionary[remainder];
|
||||
} else {
|
||||
tempStr += dictionary[hundreds] + 'hundred';
|
||||
|
||||
if (tens !== 0 || remainder !== 0) {
|
||||
tempStr += 'and';
|
||||
}
|
||||
|
||||
if (tens < 20) {
|
||||
const lessThanTwenty = tens + remainder;
|
||||
tempStr += dictionary[lessThanTwenty];
|
||||
} else {
|
||||
tempStr += dictionary[tens] + dictionary[remainder];
|
||||
}
|
||||
}
|
||||
return tempStr;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 1; i <= limit; i++) {
|
||||
numString += convertToString(i);
|
||||
}
|
||||
return numString.length;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 5900f37e1000cf542c50fe91
|
||||
title: 'Problem 18: Maximum path sum I'
|
||||
challengeType: 1
|
||||
forumTopicId: 301815
|
||||
dashedName: problem-18-maximum-path-sum-i
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
|
||||
|
||||
<span style='display: block; text-align: center;'>
|
||||
<strong style='color: red;'>3</strong><br>
|
||||
<strong style='color: red;'>7</strong> 4<br>
|
||||
2 <strong style='color: red;'>4</strong> 6<br>
|
||||
8 5 <strong style='color: red;'>9</strong> 3
|
||||
</span>
|
||||
|
||||
That is, 3 + 7 + 4 + 9 = 23.
|
||||
|
||||
Find the maximum total from top to bottom of the triangle below:
|
||||
|
||||
75
|
||||
95 64
|
||||
17 47 82
|
||||
18 35 87 10
|
||||
20 04 82 47 65
|
||||
19 01 23 75 03 34
|
||||
88 02 77 73 07 63 67
|
||||
99 65 04 28 06 16 70 92
|
||||
41 41 26 56 83 40 80 70 33
|
||||
41 48 72 33 47 32 37 16 94 29
|
||||
53 71 44 65 25 43 91 52 97 51 14
|
||||
70 11 33 28 77 73 17 78 39 68 17 57
|
||||
91 71 52 38 17 14 91 43 58 50 27 29 48
|
||||
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
||||
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
||||
|
||||
**NOTE:** As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
|
||||
|
||||
# --hints--
|
||||
|
||||
`maximumPathSumI(testTriangle)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof maximumPathSumI(testTriangle) === 'number');
|
||||
```
|
||||
|
||||
`maximumPathSumI(testTriangle)` should return 23.
|
||||
|
||||
```js
|
||||
assert.strictEqual(maximumPathSumI(testTriangle), 23);
|
||||
```
|
||||
|
||||
`maximumPathSumI(numTriangle)` should return 1074.
|
||||
|
||||
```js
|
||||
assert.strictEqual(maximumPathSumI(numTriangle), 1074);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
const numTriangle = [[75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [95, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [17, 47, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [18, 35, 87, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [20, 4, 82, 47, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [19, 1, 23, 75, 3, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0], [88, 2, 77, 73, 7, 63, 67, 0, 0, 0, 0, 0, 0, 0, 0], [99, 65, 4, 28, 6, 16, 70, 92, 0, 0, 0, 0, 0, 0, 0], [41, 41, 26, 56, 83, 40, 80, 70, 33, 0, 0, 0, 0, 0, 0], [41, 48, 72, 33, 47, 32, 37, 16, 94, 29, 0, 0, 0, 0, 0], [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14, 0, 0, 0, 0], [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57, 0, 0, 0], [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48, 0, 0], [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31, 0], [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function maximumPathSumI(triangle) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const testTriangle = [[3, 0, 0, 0],
|
||||
[7, 4, 0, 0],
|
||||
[2, 4, 6, 0],
|
||||
[8, 5, 9, 3]];
|
||||
|
||||
maximumPathSumI(testTriangle);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const testTriangle = [[3, 0, 0, 0],
|
||||
[7, 4, 0, 0],
|
||||
[2, 4, 6, 0],
|
||||
[8, 5, 9, 3]];
|
||||
|
||||
function maximumPathSumI(triangle) {
|
||||
let maxSum = triangle.slice();
|
||||
|
||||
for (let i = triangle.length - 1; i > 0; i--) {
|
||||
let currentRow = maxSum[i];
|
||||
let previousRow = maxSum[i - 1];
|
||||
const temp = [];
|
||||
for (let j = 0; j < i; j++) {
|
||||
temp.push(Math.max((currentRow[j] + previousRow[j]), (currentRow[j + 1] + previousRow[j])));
|
||||
}
|
||||
maxSum[i - 1] = temp;
|
||||
maxSum.pop();
|
||||
}
|
||||
return maxSum[0][0];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 5900f37f1000cf542c50fe92
|
||||
title: 'Problem 19: Counting Sundays'
|
||||
challengeType: 1
|
||||
forumTopicId: 301827
|
||||
dashedName: problem-19-counting-sundays
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
You are given the following information, but you may prefer to do some research for yourself.
|
||||
|
||||
<ul>
|
||||
<li>1 Jan 1900 was a Monday.</li>
|
||||
<li>Thirty days has September,<br>April, June and November.<br>All the rest have thirty-one,<br>Saving February alone,<br>Which has twenty-eight, rain or shine.<br>And on leap years, twenty-nine.</li>
|
||||
<li>A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.</li>
|
||||
</ul>
|
||||
|
||||
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
||||
|
||||
# --hints--
|
||||
|
||||
`countingSundays(1943, 1946)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof countingSundays(1943, 1946) === 'number');
|
||||
```
|
||||
|
||||
`countingSundays(1943, 1946)` should return 6.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingSundays(1943, 1946), 6);
|
||||
```
|
||||
|
||||
`countingSundays(1995, 2000)` should return 10.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingSundays(1995, 2000), 10);
|
||||
```
|
||||
|
||||
`countingSundays(1901, 2000)` should return 171.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingSundays(1901, 2000), 171);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function countingSundays(firstYear, lastYear) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
countingSundays(1943, 1946);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function countingSundays(firstYear, lastYear) {
|
||||
let sundays = 0;
|
||||
|
||||
for (let year = firstYear; year <= lastYear; year++) {
|
||||
for (let month = 0; month <= 11; month++) {
|
||||
const thisDate = new Date(year, month, 1);
|
||||
if (thisDate.getDay() === 0) {
|
||||
sundays++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sundays;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 5900f36e1000cf542c50fe81
|
||||
title: 'Problem 2: Even Fibonacci Numbers'
|
||||
challengeType: 1
|
||||
forumTopicId: 301838
|
||||
dashedName: problem-2-even-fibonacci-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
|
||||
|
||||
<div style='text-align: center;'>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</div>
|
||||
|
||||
By considering the terms in the Fibonacci sequence whose values do not exceed `n`, find the sum of the even-valued terms.
|
||||
|
||||
# --hints--
|
||||
|
||||
`fiboEvenSum(10)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof fiboEvenSum(10) === 'number');
|
||||
```
|
||||
|
||||
Your function should return an even value.
|
||||
|
||||
```js
|
||||
assert.equal(fiboEvenSum(10) % 2 === 0, true);
|
||||
```
|
||||
|
||||
Your function should sum the even-valued Fibonacci numbers: `fiboEvenSum(8)` should return 10.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fiboEvenSum(8), 10);
|
||||
```
|
||||
|
||||
`fiboEvenSum(10)` should return 10.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fiboEvenSum(10), 10);
|
||||
```
|
||||
|
||||
`fiboEvenSum(34)` should return 44.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fiboEvenSum(34), 44);
|
||||
```
|
||||
|
||||
`fiboEvenSum(60)` should return 44.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fiboEvenSum(60), 44);
|
||||
```
|
||||
|
||||
`fiboEvenSum(1000)` should return 798.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fiboEvenSum(1000), 798);
|
||||
```
|
||||
|
||||
`fiboEvenSum(100000)` should return 60696.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fiboEvenSum(100000), 60696);
|
||||
```
|
||||
|
||||
`fiboEvenSum(4000000)` should return 4613732.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fiboEvenSum(4000000), 4613732);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function fiboEvenSum(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const fiboEvenSum = (number) => {
|
||||
if (number <= 1) {
|
||||
return 0;
|
||||
} else {
|
||||
let evenSum = 0,
|
||||
prevFibNum = 1,
|
||||
fibNum = 2; // According to problem description our Fibonacci series starts with 1, 2
|
||||
for (let i = 2; fibNum <= number; i++) {
|
||||
if (fibNum % 2 == 0) {
|
||||
evenSum += fibNum;
|
||||
}
|
||||
[prevFibNum, fibNum] = [fibNum, prevFibNum + fibNum];
|
||||
}
|
||||
return evenSum;
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 5900f3801000cf542c50fe93
|
||||
title: 'Problem 20: Factorial digit sum'
|
||||
challengeType: 1
|
||||
forumTopicId: 301839
|
||||
dashedName: problem-20-factorial-digit-sum
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`n`! means `n` × (`n` − 1) × ... × 3 × 2 × 1
|
||||
|
||||
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
|
||||
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
|
||||
|
||||
Find the sum of the digits `n`!
|
||||
|
||||
# --hints--
|
||||
|
||||
`sumFactorialDigits(10)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof sumFactorialDigits(10) === 'number');
|
||||
```
|
||||
|
||||
`sumFactorialDigits(10)` should return 27.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumFactorialDigits(10), 27);
|
||||
```
|
||||
|
||||
`sumFactorialDigits(25)` should return 72.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumFactorialDigits(25), 72);
|
||||
```
|
||||
|
||||
`sumFactorialDigits(50)` should return 216.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumFactorialDigits(50), 216);
|
||||
```
|
||||
|
||||
`sumFactorialDigits(75)` should return 432.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumFactorialDigits(75), 432);
|
||||
```
|
||||
|
||||
`sumFactorialDigits(100)` should return 648.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumFactorialDigits(100), 648);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sumFactorialDigits(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
sumFactorialDigits(100);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let factorial = (n) => n <= 1 ? BigInt(n) : BigInt(n) * BigInt(factorial(--n));
|
||||
|
||||
let sumDigits = n => n.toString().split('').map(x => parseInt(x)).reduce((a,b) => a + b);
|
||||
|
||||
function sumFactorialDigits(n) {
|
||||
return sumDigits(factorial(n));
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,84 @@
|
||||
---
|
||||
id: 5900f3811000cf542c50fe94
|
||||
title: 'Problem 21: Amicable numbers'
|
||||
challengeType: 1
|
||||
forumTopicId: 301851
|
||||
dashedName: problem-21-amicable-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Let d(`n`) be defined as the sum of proper divisors of `n` (numbers less than `n` which divide evenly into `n`).
|
||||
|
||||
If d(`a`) = `b` and d(`b`) = `a`, where `a` ≠ `b`, then `a` and `b` are an amicable pair and each of `a` and `b` are called amicable numbers.
|
||||
|
||||
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
|
||||
|
||||
Evaluate the sum of all the amicable numbers under `n`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sumAmicableNum(1000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof sumAmicableNum(1000) === 'number');
|
||||
```
|
||||
|
||||
`sumAmicableNum(1000)` should return 504.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumAmicableNum(1000), 504);
|
||||
```
|
||||
|
||||
`sumAmicableNum(2000)` should return 2898.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumAmicableNum(2000), 2898);
|
||||
```
|
||||
|
||||
`sumAmicableNum(5000)` should return 8442.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumAmicableNum(5000), 8442);
|
||||
```
|
||||
|
||||
`sumAmicableNum(10000)` should return 31626.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumAmicableNum(10000), 31626);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sumAmicableNum(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
sumAmicableNum(10000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const sumAmicableNum = (n) => {
|
||||
const fsum = (n) => {
|
||||
let sum = 1;
|
||||
for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++)
|
||||
if (Math.floor(n % i) === 0)
|
||||
sum += i + Math.floor(n / i);
|
||||
return sum;
|
||||
};
|
||||
let d = [];
|
||||
let amicableSum = 0;
|
||||
for (let i=2; i<n; i++) d[i] = fsum(i);
|
||||
for (let i=2; i<n; i++) {
|
||||
let dsum = d[i];
|
||||
if (d[dsum]===i && i!==dsum) amicableSum += i+dsum;
|
||||
}
|
||||
return amicableSum/2;
|
||||
};
|
||||
```
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,101 @@
|
||||
---
|
||||
id: 5900f3831000cf542c50fe96
|
||||
title: 'Problem 23: Non-abundant sums'
|
||||
challengeType: 1
|
||||
forumTopicId: 301873
|
||||
dashedName: problem-23-non-abundant-sums
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
|
||||
|
||||
A number `n` is called deficient if the sum of its proper divisors is less than `n` and it is called abundant if this sum exceeds `n`.
|
||||
|
||||
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
|
||||
|
||||
Find the sum of all positive integers <= `n` which cannot be written as the sum of two abundant numbers.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sumOfNonAbundantNumbers(10000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof sumOfNonAbundantNumbers(10000) === 'number');
|
||||
```
|
||||
|
||||
`sumOfNonAbundantNumbers(10000)` should return 3731004.
|
||||
|
||||
```js
|
||||
assert(sumOfNonAbundantNumbers(10000) === 3731004);
|
||||
```
|
||||
|
||||
`sumOfNonAbundantNumbers(15000)` should return 4039939.
|
||||
|
||||
```js
|
||||
assert(sumOfNonAbundantNumbers(15000) === 4039939);
|
||||
```
|
||||
|
||||
`sumOfNonAbundantNumbers(20000)` should return 4159710.
|
||||
|
||||
```js
|
||||
assert(sumOfNonAbundantNumbers(20000) === 4159710);
|
||||
```
|
||||
|
||||
`sumOfNonAbundantNumbers(28123)` should return 4179871.
|
||||
|
||||
```js
|
||||
assert(sumOfNonAbundantNumbers(28123) === 4179871);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sumOfNonAbundantNumbers(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
sumOfNonAbundantNumbers(28123);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function abundantCheck(number) {
|
||||
let sum = 1;
|
||||
|
||||
for (let i = 2; i <= Math.sqrt(number); i += 1) {
|
||||
if(number % i === 0) {
|
||||
sum += i + +(i !== Math.sqrt(number) && number / i);
|
||||
}
|
||||
}
|
||||
return sum > number;
|
||||
}
|
||||
|
||||
function sumOfNonAbundantNumbers(n) {
|
||||
let sum = 0;
|
||||
const memo = {};
|
||||
let abundantList = [];
|
||||
|
||||
// Function checkSum checks if num can be represented as a sum of numbers in the stack (array)
|
||||
const checkSum = (num, stack, memo) => {
|
||||
for (let i = 0; i < stack.length; i += 1) {
|
||||
if ((num - stack[i]) in memo) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
for (let i = 1; i <= n; i += 1) {
|
||||
if (abundantCheck(i)) {
|
||||
abundantList.push(i);
|
||||
memo[i] = 1;
|
||||
}
|
||||
if (checkSum(i, abundantList, memo)) continue;
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 5900f3841000cf542c50fe97
|
||||
title: 'Problem 24: Lexicographic permutations'
|
||||
challengeType: 1
|
||||
forumTopicId: 301885
|
||||
dashedName: problem-24-lexicographic-permutations
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
|
||||
|
||||
<div style='text-align: center;'>012 021 102 120 201 210</div>
|
||||
|
||||
What is the `n`th lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
||||
|
||||
# --hints--
|
||||
|
||||
`lexicographicPermutations(699999)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof lexicographicPermutations(699999) === 'number');
|
||||
```
|
||||
|
||||
`lexicographicPermutations(699999)` should return 1938246570.
|
||||
|
||||
```js
|
||||
assert(lexicographicPermutations(699999) == 1938246570);
|
||||
```
|
||||
|
||||
`lexicographicPermutations(899999)` should return 2536987410.
|
||||
|
||||
```js
|
||||
assert(lexicographicPermutations(899999) == 2536987410);
|
||||
```
|
||||
|
||||
`lexicographicPermutations(900000)` should return 2537014689.
|
||||
|
||||
```js
|
||||
assert(lexicographicPermutations(900000) == 2537014689);
|
||||
```
|
||||
|
||||
`lexicographicPermutations(999999)` should return 2783915460.
|
||||
|
||||
```js
|
||||
assert(lexicographicPermutations(999999) == 2783915460);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function lexicographicPermutations(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
lexicographicPermutations(999999);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 5900f3851000cf542c50fe98
|
||||
title: 'Problem 25: 1000-digit Fibonacci number'
|
||||
challengeType: 1
|
||||
forumTopicId: 301897
|
||||
dashedName: problem-25-1000-digit-fibonacci-number
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The Fibonacci sequence is defined by the recurrence relation:
|
||||
|
||||
<div style='padding-left: 4em;'>F<sub>n</sub> = F<sub>n−1</sub> + F<sub>n−2</sub>, where F<sub>1</sub> = 1 and F<sub>2</sub> = 1.</div>
|
||||
|
||||
Hence the first 12 terms will be:
|
||||
|
||||
<div style='padding-left: 4em; display: inline-grid; grid-template-rows: auto; row-gap: 7px;'><div>F<sub>1</sub> = 1</div><div>F<sub>2</sub> = 1</div><div>F<sub>3</sub> = 2</div><div>F<sub>4</sub> = 3</div><div>F<sub>5</sub> = 5</div><div>F<sub>6</sub> = 8</div><div>F<sub>7</sub> = 13</div><div>F<sub>8</sub> = 21</div><div>F<sub>9</sub> = 34</div><div>F<sub>10</sub> = 55</div><div>F<sub>11</sub> = 89</div><div>F<sub>12</sub> = 144</div></div>
|
||||
|
||||
The 12th term, F<sub>12</sub>, is the first term to contain three digits.
|
||||
|
||||
What is the index of the first term in the Fibonacci sequence to contain `n` digits?
|
||||
|
||||
# --hints--
|
||||
|
||||
`digitFibonacci(5)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof digitFibonacci(5) === 'number');
|
||||
```
|
||||
|
||||
`digitFibonacci(5)` should return 21.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitFibonacci(5), 21);
|
||||
```
|
||||
|
||||
`digitFibonacci(10)` should return 45.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitFibonacci(10), 45);
|
||||
```
|
||||
|
||||
`digitFibonacci(15)` should return 69.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitFibonacci(15), 69);
|
||||
```
|
||||
|
||||
`digitFibonacci(20)` should return 93.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitFibonacci(20), 93);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function digitFibonacci(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
digitFibonacci(20);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const digitFibonacci = (n) => {
|
||||
const digits = (num) => {
|
||||
return num.toString().length;
|
||||
};
|
||||
let f1 = 1;
|
||||
let f2 = 1;
|
||||
let index = 3;
|
||||
while (true) {
|
||||
let fn = f1 + f2;
|
||||
if (digits(fn) === n) return index;
|
||||
[f1, f2] = [f2, fn];
|
||||
index++;
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 5900f3861000cf542c50fe99
|
||||
title: 'Problem 26: Reciprocal cycles'
|
||||
challengeType: 1
|
||||
forumTopicId: 301908
|
||||
dashedName: problem-26-reciprocal-cycles
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
|
||||
|
||||
<div style='padding-left: 4em; display: inline-grid; grid-template-rows: auto; row-gap: 7px;'><div><sup>1</sup>/<sub>2</sub> = 0.5</div><div><sup>1</sup>/<sub>3</sub> = 0.(3)</div><div><sup>1</sup>/<sub>4</sub> = 0.25</div><div><sup>1</sup>/<sub>5</sub> = 0.2</div><div><sup>1</sup>/<sub>6</sub> = 0.1(6)</div><div><sup>1</sup>/<sub>7</sub> = 0.(142857)</div><div><sup>1</sup>/<sub>8</sub> = 0.125</div><div><sup>1</sup>/<sub>9</sub> = 0.(1)</div><div><sup>1</sup>/<sub>10</sub> = 0.1</div></div>
|
||||
|
||||
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that <sup>1</sup>/<sub>7</sub> has a 6-digit recurring cycle.
|
||||
|
||||
Find the value of `d` < `n` for which <sup>1</sup>/<sub>d</sub> contains the longest recurring cycle in its decimal fraction part.
|
||||
|
||||
# --hints--
|
||||
|
||||
`reciprocalCycles(700)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof reciprocalCycles(700) === 'number');
|
||||
```
|
||||
|
||||
`reciprocalCycles(700)` should return 659.
|
||||
|
||||
```js
|
||||
assert(reciprocalCycles(700) == 659);
|
||||
```
|
||||
|
||||
`reciprocalCycles(800)` should return 743.
|
||||
|
||||
```js
|
||||
assert(reciprocalCycles(800) == 743);
|
||||
```
|
||||
|
||||
`reciprocalCycles(900)` should return 887.
|
||||
|
||||
```js
|
||||
assert(reciprocalCycles(900) == 887);
|
||||
```
|
||||
|
||||
`reciprocalCycles(1000)` should return 983.
|
||||
|
||||
```js
|
||||
assert(reciprocalCycles(1000) == 983);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function reciprocalCycles(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
reciprocalCycles(1000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 5900f3871000cf542c50fe9a
|
||||
title: 'Problem 27: Quadratic primes'
|
||||
challengeType: 1
|
||||
forumTopicId: 301919
|
||||
dashedName: problem-27-quadratic-primes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Euler discovered the remarkable quadratic formula:
|
||||
|
||||
<div style='margin-left: 4em;'>$n^2 + n + 41$</div>
|
||||
|
||||
It turns out that the formula will produce 40 primes for the consecutive integer values $0 \\le n \\le 39$. However, when $n = 40, 40^2 + 40 + 41 = 40(40 + 1) + 41$ is divisible by 41, and certainly when $n = 41, 41^2 + 41 + 41$ is clearly divisible by 41.
|
||||
|
||||
The incredible formula $n^2 - 79n + 1601$ was discovered, which produces 80 primes for the consecutive values $0 \\le n \\le 79$. The product of the coefficients, −79 and 1601, is −126479.
|
||||
|
||||
Considering quadratics of the form:
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
$n^2 + an + b$, where $|a| < range$ and $|b| \le range$<br>
|
||||
where $|n|$ is the modulus/absolute value of $n$<br>
|
||||
e.g. $|11| = 11$ and $|-4| = 4$<br>
|
||||
</div>
|
||||
|
||||
Find the product of the coefficients, $a$ and $b$, for the quadratic expression that produces the maximum number of primes for consecutive values of $n$, starting with $n = 0$.
|
||||
|
||||
# --hints--
|
||||
|
||||
`quadraticPrimes(200)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof quadraticPrimes(200) === 'number');
|
||||
```
|
||||
|
||||
`quadraticPrimes(200)` should return -4925.
|
||||
|
||||
```js
|
||||
assert(quadraticPrimes(200) == -4925);
|
||||
```
|
||||
|
||||
`quadraticPrimes(500)` should return -18901.
|
||||
|
||||
```js
|
||||
assert(quadraticPrimes(500) == -18901);
|
||||
```
|
||||
|
||||
`quadraticPrimes(800)` should return -43835.
|
||||
|
||||
```js
|
||||
assert(quadraticPrimes(800) == -43835);
|
||||
```
|
||||
|
||||
`quadraticPrimes(1000)` should return -59231.
|
||||
|
||||
```js
|
||||
assert(quadraticPrimes(1000) == -59231);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function quadraticPrimes(range) {
|
||||
|
||||
return range;
|
||||
}
|
||||
|
||||
quadraticPrimes(1000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 5900f3881000cf542c50fe9b
|
||||
title: 'Problem 28: Number spiral diagonals'
|
||||
challengeType: 1
|
||||
forumTopicId: 301930
|
||||
dashedName: problem-28-number-spiral-diagonals
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
|
||||
|
||||
<div style='padding-left: 4em;'>
|
||||
<div style='color: red; display: inline;'>21</div> 22 23 24 <div style='color: red; display: inline;'>25</div><br>
|
||||
20 <div style='color: red; display: inline;'>7</div> 8 <div style='color: red; display: inline;'>9</div> 10<br>
|
||||
19 6 <div style='color: red; display: inline;'>1</div> 2 11<br>
|
||||
18 <div style='color: red; display: inline;'>5</div> 4 <div style='color: red; display: inline;'>3</div> 12<br>
|
||||
<div style='color: red; display: inline;'>17</div> 16 15 14 <div style='color: red; display: inline;'>13</div><br>
|
||||
</div>
|
||||
|
||||
It can be verified that the sum of the numbers on the diagonals is 101.
|
||||
|
||||
What is the sum of the numbers on the diagonals in an `n` by `n` spiral formed in the same way?
|
||||
|
||||
# --hints--
|
||||
|
||||
`spiralDiagonals(101)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof spiralDiagonals(101) === 'number');
|
||||
```
|
||||
|
||||
`spiralDiagonals(101)` should return 692101.
|
||||
|
||||
```js
|
||||
assert(spiralDiagonals(101) == 692101);
|
||||
```
|
||||
|
||||
`spiralDiagonals(303)` should return 18591725.
|
||||
|
||||
```js
|
||||
assert(spiralDiagonals(303) == 18591725);
|
||||
```
|
||||
|
||||
`spiralDiagonals(505)` should return 85986601.
|
||||
|
||||
```js
|
||||
assert(spiralDiagonals(505) == 85986601);
|
||||
```
|
||||
|
||||
`spiralDiagonals(1001)` should return 669171001.
|
||||
|
||||
```js
|
||||
assert(spiralDiagonals(1001) == 669171001);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function spiralDiagonals(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
spiralDiagonals(1001);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const spiralDiagonals = (n) => {
|
||||
const Sn2 = (n) => {
|
||||
return n*(n+1)*(2*n+1)/6;
|
||||
};
|
||||
const Sn = (n) => {
|
||||
return n*(n+1)/2;
|
||||
};
|
||||
let sum = (Sn2(n-1) + Sn(n-1) + n-1) + (Math.floor(n/2) + Sn2(n));
|
||||
return sum;
|
||||
};
|
||||
```
|
||||
@@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 5900f3891000cf542c50fe9c
|
||||
title: 'Problem 29: Distinct powers'
|
||||
challengeType: 1
|
||||
forumTopicId: 301941
|
||||
dashedName: problem-29-distinct-powers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Consider all integer combinations of $a^b$ for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:
|
||||
|
||||
<div style='padding-left: 4em;'>
|
||||
2<sup>2</sup>=4, 2<sup>3</sup>=8, 2<sup>4</sup>=16, 2<sup>5</sup>=32 <br>
|
||||
3<sup>2</sup>=9, 3<sup>3</sup>=27, 3<sup>4</sup>=81, 3<sup>5</sup>=243 <br>
|
||||
4<sup>2</sup>=16, 4<sup>3</sup>=64, 4<sup>4</sup>=256, 4<sup>5</sup>=1024 <br>
|
||||
5<sup>2</sup>=25, 5<sup>3</sup>=125, 5<sup>4</sup>=625, 5<sup>5</sup>=3125 <br>
|
||||
</div>
|
||||
|
||||
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
|
||||
|
||||
<div style='padding-left: 4em;'>
|
||||
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
|
||||
</div>
|
||||
|
||||
How many distinct terms are in the sequence generated by $a^b$ for 2 ≤ `a` ≤ `n` and 2 ≤ `b` ≤ `n`?
|
||||
|
||||
# --hints--
|
||||
|
||||
`distinctPowers(15)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof distinctPowers(15) === 'number');
|
||||
```
|
||||
|
||||
`distinctPowers(15)` should return 177.
|
||||
|
||||
```js
|
||||
assert.strictEqual(distinctPowers(15), 177);
|
||||
```
|
||||
|
||||
`distinctPowers(20)` should return 324.
|
||||
|
||||
```js
|
||||
assert.strictEqual(distinctPowers(20), 324);
|
||||
```
|
||||
|
||||
`distinctPowers(25)` should return 519.
|
||||
|
||||
```js
|
||||
assert.strictEqual(distinctPowers(25), 519);
|
||||
```
|
||||
|
||||
`distinctPowers(30)` should return 755.
|
||||
|
||||
```js
|
||||
assert.strictEqual(distinctPowers(30), 755);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function distinctPowers(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
distinctPowers(30);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const distinctPowers = (n) => {
|
||||
let list = [];
|
||||
for (let a=2; a<=n; a++) {
|
||||
for (let b=2; b<=n; b++) {
|
||||
let term = Math.pow(a, b);
|
||||
if (list.indexOf(term)===-1) list.push(term);
|
||||
}
|
||||
}
|
||||
return list.length;
|
||||
};
|
||||
```
|
||||
@@ -0,0 +1,95 @@
|
||||
---
|
||||
id: 5900f36f1000cf542c50fe82
|
||||
title: 'Problem 3: Largest prime factor'
|
||||
challengeType: 1
|
||||
forumTopicId: 301952
|
||||
dashedName: problem-3-largest-prime-factor
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
|
||||
What is the largest prime factor of the given `number`?
|
||||
|
||||
# --hints--
|
||||
|
||||
`largestPrimeFactor(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof largestPrimeFactor(2) === 'number');
|
||||
```
|
||||
|
||||
`largestPrimeFactor(2)` should return 2.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPrimeFactor(2), 2);
|
||||
```
|
||||
|
||||
`largestPrimeFactor(3)` should return 3.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPrimeFactor(3), 3);
|
||||
```
|
||||
|
||||
`largestPrimeFactor(5)` should return 5.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPrimeFactor(5), 5);
|
||||
```
|
||||
|
||||
`largestPrimeFactor(7)` should return 7.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPrimeFactor(7), 7);
|
||||
```
|
||||
|
||||
`largestPrimeFactor(8)` should return 2.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPrimeFactor(8), 2);
|
||||
```
|
||||
|
||||
`largestPrimeFactor(13195)` should return 29.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPrimeFactor(13195), 29);
|
||||
```
|
||||
|
||||
`largestPrimeFactor(600851475143)` should return 6857.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPrimeFactor(600851475143), 6857);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function largestPrimeFactor(number) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
largestPrimeFactor(13195);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const largestPrimeFactor = (number) => {
|
||||
let largestFactor = number;
|
||||
|
||||
for (let i = 2; i <= Math.sqrt(largestFactor); i++) {
|
||||
if (!(largestFactor % i)) {
|
||||
let factor = largestFactor / i;
|
||||
let candidate = largestPrimeFactor(factor);
|
||||
|
||||
return i > candidate ? i : candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return largestFactor;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 5900f38a1000cf542c50fe9d
|
||||
title: 'Problem 30: Digit n powers'
|
||||
challengeType: 1
|
||||
forumTopicId: 301953
|
||||
dashedName: problem-30-digit-n-powers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
1634 = 1<sup>4</sup> + 6<sup>4</sup> + 3<sup>4</sup> + 4<sup>4</sup><br>
|
||||
8208 = 8<sup>4</sup> + 2<sup>4</sup> + 0<sup>4</sup> + 8<sup>4</sup><br>
|
||||
9474 = 9<sup>4</sup> + 4<sup>4</sup> + 7<sup>4</sup> + 4<sup>4</sup><br>
|
||||
</div>
|
||||
|
||||
As 1 = 1<sup>4</sup> is not a sum it is not included.
|
||||
|
||||
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
|
||||
|
||||
Find the sum of all the numbers that can be written as the sum of `n` powers of their digits.
|
||||
|
||||
# --hints--
|
||||
|
||||
`digitnPowers(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof digitnPowers(2) === 'number');
|
||||
```
|
||||
|
||||
`digitnPowers(2)` should return 0.
|
||||
|
||||
```js
|
||||
assert(digitnPowers(2) == 0);
|
||||
```
|
||||
|
||||
`digitnPowers(3)` should return 1301.
|
||||
|
||||
```js
|
||||
assert(digitnPowers(3) == 1301);
|
||||
```
|
||||
|
||||
`digitnPowers(4)` should return 19316.
|
||||
|
||||
```js
|
||||
assert(digitnPowers(4) == 19316);
|
||||
```
|
||||
|
||||
`digitnPowers(5)` should return 443839.
|
||||
|
||||
```js
|
||||
assert(digitnPowers(5) == 443839);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function digitnPowers(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
digitnPowers(5);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 5900f38b1000cf542c50fe9e
|
||||
title: 'Problem 31: Coin sums'
|
||||
challengeType: 1
|
||||
forumTopicId: 301965
|
||||
dashedName: problem-31-coin-sums
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
|
||||
|
||||
<div style='margin-left: 4em;'>1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).</div>
|
||||
|
||||
It is possible to make £2 in the following way:
|
||||
|
||||
<div style='margin-left: 4em;'>1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p</div>
|
||||
|
||||
How many different ways can `n` pence be made using any number of coins?
|
||||
|
||||
# --hints--
|
||||
|
||||
`coinSums(50)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof coinSums(50) === 'number');
|
||||
```
|
||||
|
||||
`coinSums(50)` should return 451.
|
||||
|
||||
```js
|
||||
assert(coinSums(50) == 451);
|
||||
```
|
||||
|
||||
`coinSums(100)` should return 4563.
|
||||
|
||||
```js
|
||||
assert(coinSums(100) == 4563);
|
||||
```
|
||||
|
||||
`coinSums(150)` should return 21873.
|
||||
|
||||
```js
|
||||
assert(coinSums(150) == 21873);
|
||||
```
|
||||
|
||||
`coinSums(200)` should return 73682.
|
||||
|
||||
```js
|
||||
assert(coinSums(200) == 73682);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function coinSums(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
coinSums(200);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const coinSums = (n) => {
|
||||
const getWays = (n, m=8, c=[1, 2, 5, 10, 20, 50, 100, 200]) => {
|
||||
if (n === 0) return 1;
|
||||
if (m === 0 || n < 0) return 0;
|
||||
return getWays(n - c[m - 1], m, c) + getWays(n, m - 1, c);
|
||||
};
|
||||
return getWays(n);
|
||||
};
|
||||
```
|
||||
@@ -0,0 +1,118 @@
|
||||
---
|
||||
id: 5900f38c1000cf542c50fe9f
|
||||
title: 'Problem 32: Pandigital products'
|
||||
challengeType: 1
|
||||
forumTopicId: 301976
|
||||
dashedName: problem-32-pandigital-products
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
We shall say that an `n`-digit number is pandigital if it makes use of all the digits 1 to `n` exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
|
||||
|
||||
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
|
||||
|
||||
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through `n` pandigital.
|
||||
|
||||
**Hint:** Some products can be obtained in more than one way so be sure to only include it once in your sum.
|
||||
|
||||
# --hints--
|
||||
|
||||
`pandigitalProducts(4)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof pandigitalProducts(4) === 'number');
|
||||
```
|
||||
|
||||
`pandigitalProducts(4)` should return `12`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pandigitalProducts(4), 12);
|
||||
```
|
||||
|
||||
`pandigitalProducts(6)` should return `162`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pandigitalProducts(6), 162);
|
||||
```
|
||||
|
||||
`pandigitalProducts(7)` should return `0`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pandigitalProducts(7), 0);
|
||||
```
|
||||
|
||||
`pandigitalProducts(8)` should return `13458`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pandigitalProducts(8), 13458);
|
||||
```
|
||||
|
||||
`pandigitalProducts(9)` should return `45228`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pandigitalProducts(9), 45228);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pandigitalProducts(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
pandigitalProducts(4);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pandigitalProducts(n) {
|
||||
function is1toNPandigital(n, digitStr) {
|
||||
// check if length is n
|
||||
if (digitStr.length !== n) {
|
||||
return false;
|
||||
}
|
||||
// check if pandigital
|
||||
for (let i = digitStr.length; i > 0; i--) {
|
||||
if (digitStr.indexOf(i.toString()) === -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function concatenateNums(...numbers) {
|
||||
let digitStr = '';
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
digitStr += numbers[i].toString();
|
||||
}
|
||||
return digitStr;
|
||||
}
|
||||
|
||||
const pandigitalNums = [];
|
||||
const limit = 10 ** Math.floor(n / 2) - 1;
|
||||
let sum = 0;
|
||||
for (let mult1 = 2; mult1 < limit; mult1++) {
|
||||
for (let mult2 = 2; mult2 < limit; mult2++) {
|
||||
const product = mult1 * mult2;
|
||||
const concatenated = concatenateNums(mult1, mult2, product);
|
||||
if (concatenated.length > n) {
|
||||
break;
|
||||
} else if (concatenated.length < n) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
is1toNPandigital(n, concatenated) &&
|
||||
!pandigitalNums.includes(product)
|
||||
) {
|
||||
pandigitalNums.push(product);
|
||||
sum += product;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,102 @@
|
||||
---
|
||||
id: 5900f38d1000cf542c50fea0
|
||||
title: 'Problem 33: Digit cancelling fractions'
|
||||
challengeType: 1
|
||||
forumTopicId: 301987
|
||||
dashedName: problem-33-digit-cancelling-fractions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The fraction <sup>49</sup>/<sub>98</sub> is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that <sup>49</sup>/<sub>98</sub> = <sup>4</sup>/<sub>8</sub>, which is correct, is obtained by cancelling the 9s.
|
||||
|
||||
We shall consider fractions like, <sup>30</sup>/<sub>50</sub> = <sup>3</sup>/<sub>5</sub>, to be trivial examples.
|
||||
|
||||
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
|
||||
|
||||
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
|
||||
|
||||
# --hints--
|
||||
|
||||
`digitCancellingFractions()` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof digitCancellingFractions() === 'number');
|
||||
```
|
||||
|
||||
`digitCancellingFractions()` should return 100.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitCancellingFractions(), 100);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function digitCancellingFractions() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
digitCancellingFractions();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function digitCancellingFractions() {
|
||||
function isCurious(numerator, denominator) {
|
||||
const fraction = numerator / denominator;
|
||||
const numString = numerator.toString();
|
||||
const denString = denominator.toString();
|
||||
|
||||
if (numString[1] === '0' && denString[1] === '0') {
|
||||
// trivial
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < 2; i++) {
|
||||
for (let j = 0; j < 2; j++) {
|
||||
if (numString[i] === denString[j]) {
|
||||
const newNum = parseInt(numString[1 - i], 10);
|
||||
const newDen = parseInt(denString[1 - j], 10);
|
||||
if (newNum / newDen === fraction) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function findLargestDivisor(a, b) {
|
||||
let gcd = a > b ? b : a;
|
||||
while (gcd > 1) {
|
||||
if (a % gcd === 0 && b % gcd === 0) {
|
||||
return gcd;
|
||||
}
|
||||
gcd--;
|
||||
}
|
||||
return gcd;
|
||||
}
|
||||
|
||||
function simplifyFraction(numerator, denominator) {
|
||||
const divisor = findLargestDivisor(numerator, denominator);
|
||||
return [numerator / divisor, denominator / divisor];
|
||||
}
|
||||
|
||||
let multipleNumerator = 1;
|
||||
let multipleDenominator = 1;
|
||||
|
||||
for (let denominator = 11; denominator < 100; denominator++) {
|
||||
for (let numerator = 10; numerator < denominator; numerator++) {
|
||||
if (isCurious(numerator, denominator)) {
|
||||
multipleNumerator *= numerator;
|
||||
multipleDenominator *= denominator;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return simplifyFraction(multipleNumerator, multipleDenominator)[1];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
id: 5900f38e1000cf542c50fea1
|
||||
title: 'Problem 34: Digit factorials'
|
||||
challengeType: 1
|
||||
forumTopicId: 301998
|
||||
dashedName: problem-34-digit-factorials
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
|
||||
|
||||
Find the numbers and the sum of the numbers which are equal to the sum of the factorial of their digits.
|
||||
|
||||
**Note:** as 1! = 1 and 2! = 2 are not sums they are not included.
|
||||
|
||||
# --hints--
|
||||
|
||||
`digitFactorial()` should return an object.
|
||||
|
||||
```js
|
||||
assert.typeOf(digitFactorial(), 'object');
|
||||
```
|
||||
|
||||
`digitFactorial()` should return { sum: 40730, numbers: [145, 40585] }.
|
||||
|
||||
```js
|
||||
assert.deepEqual(digitFactorial(), { sum: 40730, numbers: [145, 40585] });
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function digitFactorial() {
|
||||
|
||||
var sum = 0;
|
||||
var numbers = [];
|
||||
return { sum, numbers };
|
||||
}
|
||||
|
||||
digitFactorial();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,132 @@
|
||||
---
|
||||
id: 5900f38f1000cf542c50fea2
|
||||
title: 'Problem 35: Circular primes'
|
||||
challengeType: 1
|
||||
forumTopicId: 302009
|
||||
dashedName: problem-35-circular-primes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
|
||||
|
||||
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
|
||||
|
||||
How many circular primes are there below `n`, whereas 100 ≤ `n` ≤ 1000000?
|
||||
|
||||
**Note:**
|
||||
|
||||
Circular primes individual rotation can exceed `n`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`circularPrimes(100)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof circularPrimes(100) === 'number');
|
||||
```
|
||||
|
||||
`circularPrimes(100)` should return 13.
|
||||
|
||||
```js
|
||||
assert(circularPrimes(100) == 13);
|
||||
```
|
||||
|
||||
`circularPrimes(100000)` should return 43.
|
||||
|
||||
```js
|
||||
assert(circularPrimes(100000) == 43);
|
||||
```
|
||||
|
||||
`circularPrimes(250000)` should return 45.
|
||||
|
||||
```js
|
||||
assert(circularPrimes(250000) == 45);
|
||||
```
|
||||
|
||||
`circularPrimes(500000)` should return 49.
|
||||
|
||||
```js
|
||||
assert(circularPrimes(500000) == 49);
|
||||
```
|
||||
|
||||
`circularPrimes(750000)` should return 49.
|
||||
|
||||
```js
|
||||
assert(circularPrimes(750000) == 49);
|
||||
```
|
||||
|
||||
`circularPrimes(1000000)` should return 55.
|
||||
|
||||
```js
|
||||
assert(circularPrimes(1000000) == 55);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function circularPrimes(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
circularPrimes(1000000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function rotate(n) {
|
||||
if (n.length == 1) return n;
|
||||
return n.slice(1) + n[0];
|
||||
}
|
||||
|
||||
function circularPrimes(n) {
|
||||
// Nearest n < 10^k
|
||||
const bound = 10 ** Math.ceil(Math.log10(n));
|
||||
const primes = [0, 0, 2];
|
||||
let count = 0;
|
||||
|
||||
// Making primes array
|
||||
for (let i = 4; i <= bound; i += 2) {
|
||||
primes.push(i - 1);
|
||||
primes.push(0);
|
||||
}
|
||||
|
||||
// Getting upperbound
|
||||
const upperBound = Math.ceil(Math.sqrt(bound));
|
||||
|
||||
// Setting other non-prime numbers to 0
|
||||
for (let i = 3; i < upperBound; i += 2) {
|
||||
if (primes[i]) {
|
||||
for (let j = i * i; j < bound; j += i) {
|
||||
primes[j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Iterating through the array
|
||||
for (let i = 2; i < n; i++) {
|
||||
if (primes[i]) {
|
||||
let curr = String(primes[i]);
|
||||
let tmp = 1; // tmp variable to hold the no of rotations
|
||||
for (let x = rotate(curr); x != curr; x = rotate(x)) {
|
||||
if (x > n && primes[x]) {
|
||||
continue;
|
||||
}
|
||||
else if (!primes[x]) {
|
||||
// If the rotated value is 0 then it isn't a circular prime, break the loop
|
||||
tmp = 0;
|
||||
break;
|
||||
}
|
||||
tmp++;
|
||||
primes[x] = 0;
|
||||
}
|
||||
count += tmp;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 5900f3901000cf542c50fea3
|
||||
title: 'Problem 36: Double-base palindromes'
|
||||
challengeType: 1
|
||||
forumTopicId: 302020
|
||||
dashedName: problem-36-double-base-palindromes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The decimal number, 585 = 1001001001<sub>2</sub> (binary), is palindromic in both bases.
|
||||
|
||||
Find the sum of all numbers, less than `n`, whereas 1000 ≤ `n` ≤ 1000000, which are palindromic in base 10 and base 2.
|
||||
|
||||
(Please note that the palindromic number, in either base, may not include leading zeros.)
|
||||
|
||||
# --hints--
|
||||
|
||||
`doubleBasePalindromes(1000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof doubleBasePalindromes(1000) === 'number');
|
||||
```
|
||||
|
||||
`doubleBasePalindromes(1000)` should return 1772.
|
||||
|
||||
```js
|
||||
assert(doubleBasePalindromes(1000) == 1772);
|
||||
```
|
||||
|
||||
`doubleBasePalindromes(50000)` should return 105795.
|
||||
|
||||
```js
|
||||
assert(doubleBasePalindromes(50000) == 105795);
|
||||
```
|
||||
|
||||
`doubleBasePalindromes(500000)` should return 286602.
|
||||
|
||||
```js
|
||||
assert(doubleBasePalindromes(500000) == 286602);
|
||||
```
|
||||
|
||||
`doubleBasePalindromes(1000000)` should return 872187.
|
||||
|
||||
```js
|
||||
assert(doubleBasePalindromes(1000000) == 872187);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function doubleBasePalindromes(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
doubleBasePalindromes(1000000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 5900f3911000cf542c50fea4
|
||||
title: 'Problem 37: Truncatable primes'
|
||||
challengeType: 1
|
||||
forumTopicId: 302031
|
||||
dashedName: problem-37-truncatable-primes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
|
||||
|
||||
Find the sum of the only `n` (8 ≤ `n` ≤ 11) primes that are both truncatable from left to right and right to left.
|
||||
|
||||
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
|
||||
|
||||
# --hints--
|
||||
|
||||
`truncatablePrimes(8)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof truncatablePrimes(8) === 'number');
|
||||
```
|
||||
|
||||
`truncatablePrimes(8)` should return 1986.
|
||||
|
||||
```js
|
||||
assert(truncatablePrimes(8) == 1986);
|
||||
```
|
||||
|
||||
`truncatablePrimes(9)` should return 5123.
|
||||
|
||||
```js
|
||||
assert(truncatablePrimes(9) == 5123);
|
||||
```
|
||||
|
||||
`truncatablePrimes(10)` should return 8920.
|
||||
|
||||
```js
|
||||
assert(truncatablePrimes(10) == 8920);
|
||||
```
|
||||
|
||||
`truncatablePrimes(11)` should return 748317.
|
||||
|
||||
```js
|
||||
assert(truncatablePrimes(11) == 748317);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function truncatablePrimes(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
truncatablePrimes(11);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 5900f3931000cf542c50fea5
|
||||
title: 'Problem 38: Pandigital multiples'
|
||||
challengeType: 1
|
||||
forumTopicId: 302042
|
||||
dashedName: problem-38-pandigital-multiples
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Take the number 192 and multiply it by each of 1, 2, and 3:
|
||||
|
||||
$$\begin{align} 192 × 1 = 192\\\\
|
||||
192 × 2 = 384\\\\ 192 × 3 = 576\\\\
|
||||
\end{align}$$
|
||||
|
||||
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1, 2, 3).
|
||||
|
||||
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1, 2, 3, 4, 5).
|
||||
|
||||
What is the largest 1 to `k` pandigital `k`-digit number that can be formed as the concatenated product of an integer with (1, 2, ..., `n`) where `n` > 1?
|
||||
|
||||
# --hints--
|
||||
|
||||
`pandigitalMultiples(8)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof pandigitalMultiples(8) === 'number');
|
||||
```
|
||||
|
||||
`pandigitalMultiples(8)` should return `78156234`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pandigitalMultiples(8), 78156234);
|
||||
```
|
||||
|
||||
`pandigitalMultiples(9)` should return `932718654`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pandigitalMultiples(9), 932718654);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pandigitalMultiples(k) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
pandigitalMultiples(8);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pandigitalMultiples(k) {
|
||||
function getKDigitConcatenatedProduct(num, k) {
|
||||
// returns false if concatenated product is not k digits
|
||||
let concatenatedProduct = num.toString();
|
||||
for (let i = 2; concatenatedProduct.length < k; i++) {
|
||||
concatenatedProduct += num * i;
|
||||
}
|
||||
return concatenatedProduct.length === k ? concatenatedProduct : false;
|
||||
}
|
||||
|
||||
function is1toKPandigital(num, k) {
|
||||
const numStr = num.toString();
|
||||
|
||||
// check if length is not k
|
||||
if (numStr.length !== k) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check if pandigital
|
||||
for (let i = k; i > 0; i--) {
|
||||
if (numStr.indexOf(i.toString()) === -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
let largestNum = 0;
|
||||
for (let i = 10 ** Math.floor(k / 2) + 1; i >= 1; i--) {
|
||||
const concatenatedProduct = getKDigitConcatenatedProduct(i, k);
|
||||
if (is1toKPandigital(concatenatedProduct, k)) {
|
||||
const number = parseInt(concatenatedProduct, 10);
|
||||
if (number > largestNum) {
|
||||
largestNum = number;
|
||||
}
|
||||
}
|
||||
}
|
||||
return largestNum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 5900f3931000cf542c50fea6
|
||||
title: 'Problem 39: Integer right triangles'
|
||||
challengeType: 1
|
||||
forumTopicId: 302054
|
||||
dashedName: problem-39-integer-right-triangles
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
If `p` is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
|
||||
|
||||
{20,48,52}, {24,45,51}, {30,40,50}
|
||||
|
||||
For which value of `p` ≤ `n`, is the number of solutions maximized?
|
||||
|
||||
# --hints--
|
||||
|
||||
`intRightTriangles(500)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof intRightTriangles(500) === 'number');
|
||||
```
|
||||
|
||||
`intRightTriangles(500)` should return 420.
|
||||
|
||||
```js
|
||||
assert(intRightTriangles(500) == 420);
|
||||
```
|
||||
|
||||
`intRightTriangles(800)` should return 720.
|
||||
|
||||
```js
|
||||
assert(intRightTriangles(800) == 720);
|
||||
```
|
||||
|
||||
`intRightTriangles(900)` should return 840.
|
||||
|
||||
```js
|
||||
assert(intRightTriangles(900) == 840);
|
||||
```
|
||||
|
||||
`intRightTriangles(1000)` should return 840.
|
||||
|
||||
```js
|
||||
assert(intRightTriangles(1000) == 840);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function intRightTriangles(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
intRightTriangles(500);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// Original idea for this solution came from
|
||||
// https://www.xarg.org/puzzle/project-euler/problem-39/
|
||||
|
||||
function intRightTriangles(n) {
|
||||
// store the number of triangles with a given perimeter
|
||||
let triangles = {};
|
||||
// a is the shortest side
|
||||
for (let a = 3; a < n / 3; a++)
|
||||
// o is the opposite side and is at least as long as a
|
||||
for (let o = a; o < n / 2; o++) {
|
||||
let h = Math.sqrt(a * a + o * o); // hypotenuse
|
||||
let p = a + o + h; // perimeter
|
||||
if ((h % 1) === 0 && p <= n) {
|
||||
triangles[p] = (triangles[p] || 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
let max = 0, maxp = null;
|
||||
for (let p in triangles) {
|
||||
if (max < triangles[p]) {
|
||||
max = triangles[p];
|
||||
maxp = parseInt(p);
|
||||
}
|
||||
}
|
||||
return maxp;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 5900f3701000cf542c50fe83
|
||||
title: 'Problem 4: Largest palindrome product'
|
||||
challengeType: 1
|
||||
forumTopicId: 302065
|
||||
dashedName: problem-4-largest-palindrome-product
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
|
||||
|
||||
Find the largest palindrome made from the product of two `n`-digit numbers.
|
||||
|
||||
# --hints--
|
||||
|
||||
`largestPalindromeProduct(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof largestPalindromeProduct(2) === 'number');
|
||||
```
|
||||
|
||||
`largestPalindromeProduct(2)` should return 9009.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPalindromeProduct(2), 9009);
|
||||
```
|
||||
|
||||
`largestPalindromeProduct(3)` should return 906609.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestPalindromeProduct(3), 906609);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function largestPalindromeProduct(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
largestPalindromeProduct(3);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const largestPalindromeProduct = (digit)=>{
|
||||
let start = 1;
|
||||
let end = Number(`1e${digit}`) - 1;
|
||||
let palindrome = [];
|
||||
for(let i=start;i<=end;i++){
|
||||
for(let j=start;j<=end;j++){
|
||||
let product = i*j;
|
||||
let palindromeRegex = /\b(\d)(\d?)(\d?).?\3\2\1\b/gi;
|
||||
palindromeRegex.test(product) && palindrome.push(product);
|
||||
}
|
||||
}
|
||||
return Math.max(...palindrome);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 5900f3941000cf542c50fea7
|
||||
title: 'Problem 40: Champernowne''s constant'
|
||||
challengeType: 1
|
||||
forumTopicId: 302066
|
||||
dashedName: problem-40-champernownes-constant
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
An irrational decimal fraction is created by concatenating the positive integers:
|
||||
|
||||
0.12345678910**1**112131415161718192021...
|
||||
|
||||
It can be seen that the 12<sup>th</sup> digit of the fractional part is 1.
|
||||
|
||||
If *d<sub>n</sub>* represents the *n*<sup>th</sup> digit of the fractional part, find the value of the following expression.
|
||||
|
||||
d<sub>1</sub> × d<sub>10</sub> × d<sub>100</sub> × d<sub>1000</sub> × d<sub>10000</sub> × d<sub>100000</sub> × d<sub>1000000</sub>
|
||||
|
||||
# --hints--
|
||||
|
||||
`champernownesConstant(100)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof champernownesConstant(100) === 'number');
|
||||
```
|
||||
|
||||
`champernownesConstant(100)` should return 5.
|
||||
|
||||
```js
|
||||
assert.strictEqual(champernownesConstant(100), 5);
|
||||
```
|
||||
|
||||
`champernownesConstant(1000)` should return 15.
|
||||
|
||||
```js
|
||||
assert.strictEqual(champernownesConstant(1000), 15);
|
||||
```
|
||||
|
||||
`champernownesConstant(1000000)` should return 210.
|
||||
|
||||
```js
|
||||
assert.strictEqual(champernownesConstant(1000000), 210);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function champernownesConstant(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
champernownesConstant(100);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function champernownesConstant(n) {
|
||||
let fractionalPart = '';
|
||||
for (let i = 0; fractionalPart.length <= n; i++) {
|
||||
fractionalPart += i.toString();
|
||||
}
|
||||
|
||||
let product = 1;
|
||||
for (let i = 0; i < n.toString().length; i++) {
|
||||
const index = 10 ** i;
|
||||
product *= parseInt(fractionalPart[index], 10);
|
||||
}
|
||||
|
||||
return product;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,117 @@
|
||||
---
|
||||
id: 5900f3951000cf542c50fea8
|
||||
title: 'Problem 41: Pandigital prime'
|
||||
challengeType: 1
|
||||
forumTopicId: 302078
|
||||
dashedName: problem-41-pandigital-prime
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
We shall say that an `n`-digit number is pandigital if it makes use of all the digits 1 to `n` exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
|
||||
|
||||
What is the largest `n`-length digit pandigital prime that exists?
|
||||
|
||||
# --hints--
|
||||
|
||||
`pandigitalPrime(4)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof pandigitalPrime(4) === 'number');
|
||||
```
|
||||
|
||||
`pandigitalPrime(4)` should return 4231.
|
||||
|
||||
```js
|
||||
assert(pandigitalPrime(4) == 4231);
|
||||
```
|
||||
|
||||
`pandigitalPrime(7)` should return 7652413.
|
||||
|
||||
```js
|
||||
assert(pandigitalPrime(7) == 7652413);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pandigitalPrime(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
pandigitalPrime(7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pandigitalPrime(n) {
|
||||
function isPrime(num) {
|
||||
for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
|
||||
if (num % i === 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return num !== 1;
|
||||
}
|
||||
|
||||
function getPermutations(n) {
|
||||
if (n === 1) {
|
||||
permutations.push(digitsArr.join(''));
|
||||
} else {
|
||||
for (let i = 0; i < n - 1; i++) {
|
||||
getPermutations(n - 1);
|
||||
// swap(n % 2 === 0 ? i : 0, n - 1);
|
||||
if (n % 2 === 0) {
|
||||
swap(i, n - 1);
|
||||
} else {
|
||||
swap(0, n - 1);
|
||||
}
|
||||
}
|
||||
getPermutations(n - 1);
|
||||
}
|
||||
}
|
||||
function swap(x, y) {
|
||||
let temp = digitsArr[x];
|
||||
digitsArr[x] = digitsArr[y];
|
||||
digitsArr[y] = temp;
|
||||
}
|
||||
let max = 0;
|
||||
let permutations = [];
|
||||
let digitsArr;
|
||||
let pandigitalNum = '';
|
||||
|
||||
for (let max = n; max > 0; max--) {
|
||||
pandigitalNum += max;
|
||||
}
|
||||
|
||||
for (let i = 0; i < pandigitalNum.length; i++) {
|
||||
if (max > 0) {
|
||||
break;
|
||||
} else {
|
||||
permutations = [];
|
||||
const currMax = pandigitalNum.slice(i);
|
||||
digitsArr = currMax.split('');
|
||||
getPermutations(digitsArr.length);
|
||||
|
||||
// sort permutations in descending order
|
||||
permutations.sort(function(a, b) {
|
||||
return b - a;
|
||||
});
|
||||
|
||||
for (let perm of permutations) {
|
||||
const thisPerm = parseInt(perm);
|
||||
if (isPrime(thisPerm)) {
|
||||
max = thisPerm;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
```
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,126 @@
|
||||
---
|
||||
id: 5900f3971000cf542c50feaa
|
||||
title: 'Problem 43: Sub-string divisibility'
|
||||
challengeType: 1
|
||||
forumTopicId: 302100
|
||||
dashedName: problem-43-sub-string-divisibility
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
|
||||
|
||||
Let $d_1$ be the $1^{st}$ digit, $d_2$ be the $2^{nd}$ digit, and so on. In this way, we note the following:
|
||||
|
||||
- ${d_2}{d_3}{d_4} = 406$ is divisible by 2
|
||||
- ${d_3}{d_4}{d_5} = 063$ is divisible by 3
|
||||
- ${d_4}{d_5}{d_6} = 635$ is divisible by 5
|
||||
- ${d_5}{d_6}{d_7} = 357$ is divisible by 7
|
||||
- ${d_6}{d_7}{d_8} = 572$ is divisible by 11
|
||||
- ${d_7}{d_8}{d_9} = 728$ is divisible by 13
|
||||
- ${d_8}{d_9}{d_{10}} = 289$ is divisible by 17
|
||||
|
||||
Find the sum of all 0 to `n` pandigital numbers with sub-strings fulfilling `n - 2` of these divisibility properties.
|
||||
|
||||
**Note:** Pandigital numbers starting with `0` are to be considered in the result.
|
||||
|
||||
# --hints--
|
||||
|
||||
`substringDivisibility(5)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof substringDivisibility(5) === 'number');
|
||||
```
|
||||
|
||||
`substringDivisibility(5)` should return `12444480`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(substringDivisibility(5), 12444480)
|
||||
```
|
||||
|
||||
`substringDivisibility(7)` should return `1099210170`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(substringDivisibility(7), 1099210170)
|
||||
```
|
||||
|
||||
`substringDivisibility(8)` should return `1113342912`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(substringDivisibility(8), 1113342912)
|
||||
```
|
||||
|
||||
`substringDivisibility(9)` should return `16695334890`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(substringDivisibility(9), 16695334890)
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function substringDivisibility(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
substringDivisibility(5);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function substringDivisibility(n) {
|
||||
function isSubDivisable(digits) {
|
||||
const factors = [2, 3, 5, 7, 11, 13, 17];
|
||||
|
||||
for (let i = 1; i < digits.length - 2; i++) {
|
||||
const subNumber = digits[i] * 100 + digits[i + 1] * 10 + digits[i + 2];
|
||||
if (subNumber % factors[i - 1] !== 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function heapsPermutations(k, digits, conditionCheck, results) {
|
||||
if (k === 1) {
|
||||
if (conditionCheck(digits)) {
|
||||
const number = parseInt(digits.join(''), 10);
|
||||
results.push(number);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
heapsPermutations(k - 1, digits, conditionCheck, results);
|
||||
|
||||
for (let i = 0; i < k - 1; i++) {
|
||||
if (k % 2 === 0) {
|
||||
[digits[i], digits[k - 1]] = [digits[k - 1], digits[i]];
|
||||
} else {
|
||||
[digits[0], digits[k - 1]] = [digits[k - 1], digits[0]];
|
||||
}
|
||||
heapsPermutations(k - 1, digits, conditionCheck, results);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const allowedDigits = [...new Array(n + 1).keys()];
|
||||
const divisablePandigitals = [];
|
||||
heapsPermutations(
|
||||
allowedDigits.length,
|
||||
allowedDigits,
|
||||
isSubDivisable,
|
||||
divisablePandigitals
|
||||
);
|
||||
|
||||
let sum = 0;
|
||||
for (let i = 0; i < divisablePandigitals.length; i++) {
|
||||
sum += divisablePandigitals[i];
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 5900f3981000cf542c50feab
|
||||
title: 'Problem 44: Pentagon numbers'
|
||||
challengeType: 1
|
||||
forumTopicId: 302111
|
||||
dashedName: problem-44-pentagon-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Pentagonal numbers are generated by the formula, P<sub>n</sub>=`n`(3`n`−1)/2. The first ten pentagonal numbers are:
|
||||
|
||||
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
|
||||
|
||||
It can be seen that P<sub>4</sub> + P<sub>7</sub> = 22 + 70 = 92 = P<sub>8</sub>. However, their difference, 70 − 22 = 48, is not pentagonal.
|
||||
|
||||
Find the pair of pentagonal numbers, P<sub>j</sub> and P<sub>k</sub>, for which their sum and difference are pentagonal and D = |P<sub>k</sub> − P<sub>j</sub>| is minimized; what is the value of D?
|
||||
|
||||
# --hints--
|
||||
|
||||
`pentagonNumbers()` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof pentagonNumbers() === 'number');
|
||||
```
|
||||
|
||||
`pentagonNumbers()` should return 5482660.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pentagonNumbers(), 5482660);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pentagonNumbers() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
pentagonNumbers();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pentagonNumbers() {
|
||||
function isPentagonal(num) {
|
||||
// Formula found by solving pentagonal number
|
||||
// equation for n.
|
||||
const n = (Math.sqrt((24 * num) + 1) + 1) / 6;
|
||||
return n % 1 === 0;
|
||||
}
|
||||
|
||||
function pentagonal(num) {
|
||||
return (num * ((3 * num) - 1)) / 2;
|
||||
}
|
||||
let result;
|
||||
let i = 1;
|
||||
while (!result) {
|
||||
i++;
|
||||
const num1 = (i * ((3 * i) - 1)) / 2; // Pentagonal num formula
|
||||
const minDiff = num1 - (((i - 1) * ((3 * (i - 1)) - 1)) / 2);
|
||||
let j = i - 1;
|
||||
while (j > 0 && !result) {
|
||||
const num2 = (j * ((3 * j) - 1)) / 2;
|
||||
if (isPentagonal(num1 - num2) && isPentagonal(num1 + num2)) {
|
||||
result = num1 - num2;
|
||||
}
|
||||
j--;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 5900f3991000cf542c50feac
|
||||
title: 'Problem 45: Triangular, pentagonal, and hexagonal'
|
||||
challengeType: 1
|
||||
forumTopicId: 302122
|
||||
dashedName: problem-45-triangular-pentagonal-and-hexagonal
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
|
||||
|
||||
<div style='display: inline-grid; text-align: center; grid-template-columns: 135px 135px 260px; grid-template-rows: auto;'><div>Triangle</div><div>T<sub>n</sub>=<var>n</var>(<var>n</var>+1)/2</div><div>1, 3, 6, 10, 15, ...</div></div>
|
||||
<div style='display: inline-grid; text-align: center; grid-template-columns: 135px 135px 260px; grid-template-rows: auto;'><div>Pentagonal</div><div>P<sub>n</sub>=<var>n</var>(3<var>n</var>−1)/2</div><div>1, 5, 12, 22, 35, ...</div></div>
|
||||
<div style='display: inline-grid; text-align: center; grid-template-columns: 135px 135px 260px; grid-template-rows: auto;'><div>Hexagonal</div><div>H<sub>n</sub>=<var>n</var>(2<var>n</var>−1)</div><div>1, 6, 15, 28, 45, ...</div></div>
|
||||
|
||||
It can be verified that T<sub>285</sub> = P<sub>165</sub> = H<sub>143</sub> = 40755.
|
||||
|
||||
Find the next triangle number that is also pentagonal and hexagonal.
|
||||
|
||||
# --hints--
|
||||
|
||||
`triPentaHexa(40756)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof triPentaHexa(40756) === 'number');
|
||||
```
|
||||
|
||||
`triPentaHexa(40756)` should return 1533776805.
|
||||
|
||||
```js
|
||||
assert.strictEqual(triPentaHexa(40756), 1533776805);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function triPentaHexa(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
triPentaHexa(40756);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function triPentaHexa(n) {
|
||||
function triangular(num) {
|
||||
return (num * (num + 1)) / 2;
|
||||
}
|
||||
|
||||
function isPentagonal(num) {
|
||||
// Formula found by completing the square and
|
||||
// solving for n.
|
||||
const n = (Math.sqrt((24 * num) + 1) + 1) / 6;
|
||||
return n % 1 === 0;
|
||||
}
|
||||
|
||||
function isHexagonal(num) {
|
||||
// Formula found by completing the square and
|
||||
// solving for n.
|
||||
const n = Math.sqrt(0.5 * (num + (1 / 8))) + 0.25;
|
||||
return n % 1 === 0;
|
||||
}
|
||||
|
||||
let iTri = n;
|
||||
let tri;
|
||||
let found = false;
|
||||
while (!found) {
|
||||
iTri++;
|
||||
tri = triangular(iTri);
|
||||
if (isPentagonal(tri) && isHexagonal(tri)) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
return tri;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,99 @@
|
||||
---
|
||||
id: 5900f39a1000cf542c50fead
|
||||
title: 'Problem 46: Goldbach''s other conjecture'
|
||||
challengeType: 1
|
||||
forumTopicId: 302134
|
||||
dashedName: problem-46-goldbachs-other-conjecture
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
|
||||
|
||||
<div style='margin-left: 2em;'>
|
||||
9 = 7 + 2×1<sup>2</sup><br>
|
||||
15 = 7 + 2×2<sup>2</sup><br>
|
||||
21 = 3 + 2×3<sup>2</sup><br>
|
||||
25 = 7 + 2×3<sup>2</sup><br>
|
||||
27 = 19 + 2×2<sup>2</sup><br>
|
||||
33 = 31 + 2×1<sup>2</sup>
|
||||
</div>
|
||||
|
||||
It turns out that the conjecture was false.
|
||||
|
||||
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
|
||||
|
||||
# --hints--
|
||||
|
||||
`goldbachsOtherConjecture()` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof goldbachsOtherConjecture() === 'number');
|
||||
```
|
||||
|
||||
`goldbachsOtherConjecture()` should return 5777.
|
||||
|
||||
```js
|
||||
assert.strictEqual(goldbachsOtherConjecture(), 5777);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function goldbachsOtherConjecture() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
goldbachsOtherConjecture();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function goldbachsOtherConjecture() { function isPrime(num) {
|
||||
if (num < 2) {
|
||||
return false;
|
||||
} else if (num === 2) {
|
||||
return true;
|
||||
}
|
||||
const sqrtOfNum = Math.floor(num ** 0.5);
|
||||
for (let i = 2; i <= sqrtOfNum + 1; i++) {
|
||||
if (num % i === 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function isSquare(num) {
|
||||
return Math.sqrt(num) % 1 === 0;
|
||||
}
|
||||
|
||||
// construct a list of prime numbers
|
||||
const primes = [];
|
||||
for (let i = 2; primes.length < 1000; i++) {
|
||||
if (isPrime(i)) primes.push(i);
|
||||
}
|
||||
|
||||
let num = 3;
|
||||
let answer;
|
||||
while (!answer) {
|
||||
num += 2;
|
||||
if (!isPrime(num)) {
|
||||
let found = false;
|
||||
for (let primeI = 0; primeI < primes.length && !found; primeI++) {
|
||||
const square = (num - primes[primeI]) / 2;
|
||||
if (isSquare(square)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) answer = num;
|
||||
}
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,94 @@
|
||||
---
|
||||
id: 5900f39c1000cf542c50feae
|
||||
title: 'Problem 47: Distinct primes factors'
|
||||
challengeType: 1
|
||||
forumTopicId: 302145
|
||||
dashedName: problem-47-distinct-primes-factors
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The first two consecutive numbers to have two distinct prime factors are:
|
||||
|
||||
<div style='padding-left: 4em;'>
|
||||
14 = 2 × 7<br>
|
||||
15 = 3 × 5
|
||||
</div>
|
||||
|
||||
The first three consecutive numbers to have three distinct prime factors are:
|
||||
|
||||
<div style='padding-left: 4em;'>
|
||||
644 = 2<sup>2</sup> × 7 × 23<br>
|
||||
645 = 3 × 5 × 43<br>
|
||||
646 = 2 × 17 × 19
|
||||
</div>
|
||||
|
||||
Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
|
||||
|
||||
# --hints--
|
||||
|
||||
`distinctPrimeFactors(2, 2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof distinctPrimeFactors(2, 2) === 'number');
|
||||
```
|
||||
|
||||
`distinctPrimeFactors(2, 2)` should return 14.
|
||||
|
||||
```js
|
||||
assert.strictEqual(distinctPrimeFactors(2, 2), 14);
|
||||
```
|
||||
|
||||
`distinctPrimeFactors(3, 3)` should return 644.
|
||||
|
||||
```js
|
||||
assert.strictEqual(distinctPrimeFactors(3, 3), 644);
|
||||
```
|
||||
|
||||
`distinctPrimeFactors(4, 4)` should return 134043.
|
||||
|
||||
```js
|
||||
assert.strictEqual(distinctPrimeFactors(4, 4), 134043);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
distinctPrimeFactors(4, 4);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function distinctPrimeFactors(targetNumPrimes, targetConsecutive) {
|
||||
const primeLimit = targetNumPrimes * targetConsecutive * 10000;
|
||||
const numFactors = Array(primeLimit).fill(0);
|
||||
|
||||
let numConsecutive = 0;
|
||||
for (let i = 2; i < primeLimit; i++) {
|
||||
if (numFactors[i] === targetNumPrimes) {
|
||||
// Current number is composite with target num factors
|
||||
numConsecutive++;
|
||||
if (numConsecutive === targetConsecutive) {
|
||||
return i - numConsecutive + 1;
|
||||
}
|
||||
} else {
|
||||
// Current number is not matching composite
|
||||
numConsecutive = 0;
|
||||
if (numFactors[i] === 0) {
|
||||
// Current number is prime
|
||||
for (let j = i; j < primeLimit; j += i) {
|
||||
numFactors[j]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 5900f39c1000cf542c50feaf
|
||||
title: 'Problem 48: Self powers'
|
||||
challengeType: 1
|
||||
forumTopicId: 302157
|
||||
dashedName: problem-48-self-powers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The series, 1<sup>1</sup> + 2<sup>2</sup> + 3<sup>3</sup> + ... + 10<sup>10</sup> = 10405071317.
|
||||
|
||||
Find the last ten digits of the series, 1<sup>1</sup> + 2<sup>2</sup> + 3<sup>3</sup> + ... + 1000<sup>1000</sup>.
|
||||
|
||||
# --hints--
|
||||
|
||||
`selfPowers(10, 3)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof selfPowers(10, 3) === 'number');
|
||||
```
|
||||
|
||||
`selfPowers(10, 3)` should return 317.
|
||||
|
||||
```js
|
||||
assert.strictEqual(selfPowers(10, 3), 317);
|
||||
```
|
||||
|
||||
`selfPowers(150, 6)` should return 29045.
|
||||
|
||||
```js
|
||||
assert.strictEqual(selfPowers(150, 6), 29045);
|
||||
```
|
||||
|
||||
`selfPowers(673, 7)` should return 2473989.
|
||||
|
||||
```js
|
||||
assert.strictEqual(selfPowers(673, 7), 2473989);
|
||||
```
|
||||
|
||||
`selfPowers(1000, 10)` should return 9110846700.
|
||||
|
||||
```js
|
||||
assert.strictEqual(selfPowers(1000, 10), 9110846700);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function selfPowers(power, lastDigits) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
selfPowers(1000, 10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function selfPowers(power, lastDigits) {
|
||||
let sum = 0;
|
||||
const modulo = Math.pow(10, lastDigits);
|
||||
|
||||
for (let i = 1; i <= power; i++) {
|
||||
let temp = i;
|
||||
for (let j = 1; j < i; j++) {
|
||||
temp *= i;
|
||||
temp %= modulo;
|
||||
}
|
||||
|
||||
sum += temp;
|
||||
sum %= modulo;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 5900f39d1000cf542c50feb0
|
||||
title: 'Problem 49: Prime permutations'
|
||||
challengeType: 1
|
||||
forumTopicId: 302159
|
||||
dashedName: problem-49-prime-permutations
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
|
||||
|
||||
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
|
||||
|
||||
What 12-digit number do you form by concatenating the three terms in this sequence?
|
||||
|
||||
# --hints--
|
||||
|
||||
`primePermutations()` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof primePermutations() === 'number');
|
||||
```
|
||||
|
||||
`primePermutations()` should return 296962999629.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primePermutations(), 296962999629);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function primePermutations() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
primePermutations();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function primePermutations() {
|
||||
function arePermutations(num1, num2) {
|
||||
const numStr1 = num1.toString();
|
||||
let numStr2 = num2.toString();
|
||||
if (numStr1.length !== numStr2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < numStr1.length; i++) {
|
||||
const index = numStr2.indexOf(numStr1[i]);
|
||||
if (index === -1) {
|
||||
return false;
|
||||
}
|
||||
numStr2 = numStr2.slice(0, index) + numStr2.slice(index + 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function isPrime(num) {
|
||||
if (num < 2) {
|
||||
return false;
|
||||
} else if (num === 2) {
|
||||
return true;
|
||||
}
|
||||
const sqrtOfNum = Math.floor(num ** 0.5);
|
||||
for (let i = 2; i <= sqrtOfNum + 1; i++) {
|
||||
if (num % i === 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
for (let num1 = 1000; num1 <= 9999; num1++) {
|
||||
const num2 = num1 + 3330;
|
||||
const num3 = num2 + 3330;
|
||||
if (isPrime(num1) && isPrime(num2) && isPrime(num3)) {
|
||||
if (arePermutations(num1, num2) && arePermutations(num1, num3)
|
||||
&& num1 !== 1487) {
|
||||
// concatenate and return numbers
|
||||
return (num1 * 100000000) + (num2 * 10000) + num3;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 5900f3711000cf542c50fe84
|
||||
title: 'Problem 5: Smallest multiple'
|
||||
challengeType: 1
|
||||
forumTopicId: 302160
|
||||
dashedName: problem-5-smallest-multiple
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
|
||||
|
||||
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to `n`?
|
||||
|
||||
# --hints--
|
||||
|
||||
`smallestMult(5)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof smallestMult(5) === 'number');
|
||||
```
|
||||
|
||||
`smallestMult(5)` should return 60.
|
||||
|
||||
```js
|
||||
assert.strictEqual(smallestMult(5), 60);
|
||||
```
|
||||
|
||||
`smallestMult(7)` should return 420.
|
||||
|
||||
```js
|
||||
assert.strictEqual(smallestMult(7), 420);
|
||||
```
|
||||
|
||||
`smallestMult(10)` should return 2520.
|
||||
|
||||
```js
|
||||
assert.strictEqual(smallestMult(10), 2520);
|
||||
```
|
||||
|
||||
`smallestMult(13)` should return 360360.
|
||||
|
||||
```js
|
||||
assert.strictEqual(smallestMult(13), 360360);
|
||||
```
|
||||
|
||||
`smallestMult(20)` should return 232792560.
|
||||
|
||||
```js
|
||||
assert.strictEqual(smallestMult(20), 232792560);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function smallestMult(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
smallestMult(20);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function smallestMult(n){
|
||||
function gcd(a, b) {
|
||||
return b === 0 ? a : gcd(b, a%b); // Euclidean algorithm
|
||||
}
|
||||
|
||||
function lcm(a, b) {
|
||||
return a * b / gcd(a, b);
|
||||
}
|
||||
var result = 1;
|
||||
for(var i = 2; i <= n; i++) {
|
||||
result = lcm(result, i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,139 @@
|
||||
---
|
||||
id: 5900f39e1000cf542c50feb1
|
||||
title: 'Problem 50: Consecutive prime sum'
|
||||
challengeType: 1
|
||||
forumTopicId: 302161
|
||||
dashedName: problem-50-consecutive-prime-sum
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The prime 41, can be written as the sum of six consecutive primes:
|
||||
|
||||
<div style='text-align: center;'>41 = 2 + 3 + 5 + 7 + 11 + 13</div>
|
||||
|
||||
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
|
||||
|
||||
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
|
||||
|
||||
Which prime, below one-million, can be written as the sum of the most consecutive primes?
|
||||
|
||||
# --hints--
|
||||
|
||||
`consecutivePrimeSum(1000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof consecutivePrimeSum(1000) === 'number');
|
||||
```
|
||||
|
||||
`consecutivePrimeSum(1000)` should return 953.
|
||||
|
||||
```js
|
||||
assert.strictEqual(consecutivePrimeSum(1000), 953);
|
||||
```
|
||||
|
||||
`consecutivePrimeSum(1000000)` should return 997651.
|
||||
|
||||
```js
|
||||
assert.strictEqual(consecutivePrimeSum(1000000), 997651);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function consecutivePrimeSum(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
consecutivePrimeSum(1000000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class PrimeSeive {
|
||||
constructor(num) {
|
||||
const seive = Array(Math.floor((num - 1) / 2)).fill(true);
|
||||
const primes = [2];
|
||||
const upper = Math.floor((num - 1) / 2);
|
||||
const sqrtUpper = Math.floor((Math.sqrt(num) - 1) / 2);
|
||||
|
||||
for (let i = 0; i <= sqrtUpper; i++) {
|
||||
if (seive[i]) {
|
||||
// Mark value in seive array
|
||||
const prime = 2 * i + 3;
|
||||
primes.push(prime);
|
||||
// Mark all multiples of this number as false (not prime)
|
||||
const primeSqaredIndex = 2 * i ** 2 + 6 * i + 3;
|
||||
for (let j = primeSqaredIndex; j < upper; j += prime) {
|
||||
seive[j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let i = sqrtUpper + 1; i < upper; i++) {
|
||||
if (seive[i]) {
|
||||
primes.push(2 * i + 3);
|
||||
}
|
||||
}
|
||||
|
||||
this._seive = seive;
|
||||
this._primes = primes;
|
||||
}
|
||||
|
||||
isPrime(num) {
|
||||
return num === 2
|
||||
? true
|
||||
: num % 2 === 0
|
||||
? false
|
||||
: this.isOddPrime(num);
|
||||
}
|
||||
|
||||
isOddPrime(num) {
|
||||
return this._seive[(num - 3) / 2];
|
||||
}
|
||||
|
||||
get primes() {
|
||||
return this._primes;
|
||||
}
|
||||
};
|
||||
|
||||
function consecutivePrimeSum(limit) {
|
||||
// Initalize seive
|
||||
const primeSeive = new PrimeSeive(limit);
|
||||
|
||||
// Initalize for longest sum < 100
|
||||
let bestPrime = 41;
|
||||
let bestI = 0;
|
||||
let bestJ = 5;
|
||||
|
||||
// Find longest sum < limit
|
||||
let sumOfCurrRange = 41;
|
||||
let i = 0, j = 5;
|
||||
// -- Loop while current some starting at i is < limit
|
||||
while (sumOfCurrRange < limit) {
|
||||
let currSum = sumOfCurrRange;
|
||||
// -- Loop while pushing j towards end of PRIMES list
|
||||
// keeping sum under limit
|
||||
while (currSum < limit) {
|
||||
if (primeSeive.isPrime(currSum)) {
|
||||
bestPrime = sumOfCurrRange = currSum;
|
||||
bestI = i;
|
||||
bestJ = j;
|
||||
}
|
||||
// -- Increment inner loop
|
||||
j++;
|
||||
currSum += primeSeive.primes[j];
|
||||
}
|
||||
// -- Increment outer loop
|
||||
i++;
|
||||
j = i + (bestJ - bestI);
|
||||
sumOfCurrRange -= primeSeive.primes[i - 1];
|
||||
sumOfCurrRange += primeSeive.primes[j];
|
||||
}
|
||||
// Return
|
||||
return bestPrime;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,140 @@
|
||||
---
|
||||
id: 5900f39f1000cf542c50feb2
|
||||
title: 'Problem 51: Prime digit replacements'
|
||||
challengeType: 1
|
||||
forumTopicId: 302162
|
||||
dashedName: problem-51-prime-digit-replacements
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
By replacing the 1st digit of the 2-digit number \*3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.
|
||||
|
||||
By replacing the 3rd and 4th digits of 56\*\*3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
|
||||
|
||||
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an `n` prime value family.
|
||||
|
||||
# --hints--
|
||||
|
||||
`primeDigitReplacements(6)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof primeDigitReplacements(6) === 'number');
|
||||
```
|
||||
|
||||
`primeDigitReplacements(6)` should return `13`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeDigitReplacements(6), 13);
|
||||
```
|
||||
|
||||
`primeDigitReplacements(7)` should return `56003`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeDigitReplacements(7), 56003);
|
||||
```
|
||||
|
||||
`primeDigitReplacements(8)` should return `121313`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeDigitReplacements(8), 121313);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function primeDigitReplacements(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
primeDigitReplacements(6);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class PrimeSeive {
|
||||
constructor(num) {
|
||||
const seive = Array(Math.floor((num - 1) / 2)).fill(true);
|
||||
const upper = Math.floor((num - 1) / 2);
|
||||
const sqrtUpper = Math.floor((Math.sqrt(num) - 1) / 2);
|
||||
|
||||
for (let i = 0; i <= sqrtUpper; i++) {
|
||||
if (seive[i]) {
|
||||
// Mark value in seive array
|
||||
const prime = 2 * i + 3;
|
||||
// Mark all multiples of this number as false (not prime)
|
||||
const primeSqaredIndex = 2 * i ** 2 + 6 * i + 3;
|
||||
for (let j = primeSqaredIndex; j < upper; j += prime) {
|
||||
seive[j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this._seive = seive;
|
||||
}
|
||||
|
||||
isPrime(num) {
|
||||
return num === 2
|
||||
? true
|
||||
: num % 2 === 0
|
||||
? false
|
||||
: this.isOddPrime(num);
|
||||
}
|
||||
|
||||
isOddPrime(num) {
|
||||
return this._seive[(num - 3) / 2];
|
||||
}
|
||||
};
|
||||
|
||||
function primeDigitReplacements(n) {
|
||||
const primeSeive = new PrimeSeive(n * n * n * 2000);
|
||||
|
||||
function isNFamily(number, n) {
|
||||
const prime = number.toString();
|
||||
const lastDigit = prime[prime.length - 1];
|
||||
return doesReplacingMakeFamily(prime, '0', n) ||
|
||||
doesReplacingMakeFamily(prime, '2', n) ||
|
||||
(lastDigit !== '1' && doesReplacingMakeFamily(prime, '1', n));
|
||||
}
|
||||
|
||||
function doesReplacingMakeFamily(prime, digitToReplace, family) {
|
||||
let miss = 0;
|
||||
const base = parseInt(
|
||||
prime
|
||||
.split('')
|
||||
.map(digit => digit == digitToReplace ? '0' : digit)
|
||||
.join('')
|
||||
);
|
||||
const replacements = parseInt(
|
||||
prime
|
||||
.split('')
|
||||
.map(digit => digit === digitToReplace ? '1' : '0')
|
||||
.join('')
|
||||
);
|
||||
const start = prime[0] === digitToReplace ? 1 : 0;
|
||||
for (let i = start; i < 10; i++) {
|
||||
const nextNumber = base + i * replacements;
|
||||
if (!isPartOfFamily(nextNumber, prime)) miss++;
|
||||
if (10 - start - miss < family) break;
|
||||
}
|
||||
return 10 - start - miss === family;
|
||||
}
|
||||
|
||||
function isPartOfFamily(number, prime) {
|
||||
return (
|
||||
primeSeive.isPrime(number) && number.toString().length === prime.length
|
||||
);
|
||||
}
|
||||
|
||||
for (let number = 1; number < 125000; number++) {
|
||||
if (primeSeive.isPrime(number) && isNFamily(number, n)) {
|
||||
return number;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 5900f3a01000cf542c50feb3
|
||||
title: 'Problem 52: Permuted multiples'
|
||||
challengeType: 1
|
||||
forumTopicId: 302163
|
||||
dashedName: problem-52-permuted-multiples
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
|
||||
|
||||
Find the smallest positive integer, such that multiplied by integers $\\{2, 3, \ldots, n\\}$, contain the same digits.
|
||||
|
||||
# --hints--
|
||||
|
||||
`permutedMultiples(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof permutedMultiples(2) === 'number');
|
||||
```
|
||||
|
||||
`permutedMultiples(2)` should return `125874`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permutedMultiples(2), 125874);
|
||||
```
|
||||
|
||||
`permutedMultiples(6)` should return `142857`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permutedMultiples(6), 142857);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function permutedMultiples(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
permutedMultiples(2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function permutedMultiples(n) {
|
||||
const isPermutation = (a, b) =>
|
||||
a.length !== b.length
|
||||
? false
|
||||
: a.split('').sort().join() === b.split('').sort().join();
|
||||
|
||||
|
||||
let start = 1;
|
||||
let found = false;
|
||||
let result = 0;
|
||||
|
||||
while (!found) {
|
||||
start *= 10;
|
||||
for (let i = start; i < start * 10 / n; i++) {
|
||||
found = true;
|
||||
for (let j = 2; j <= n; j++) {
|
||||
if (!isPermutation(i + '', j * i + '')) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found) {
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 5900f3a11000cf542c50feb4
|
||||
title: 'Problem 53: Combinatoric selections'
|
||||
challengeType: 1
|
||||
forumTopicId: 302164
|
||||
dashedName: problem-53-combinatoric-selections
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
There are exactly ten ways of selecting three from five, 12345:
|
||||
|
||||
<div style='text-align: center;'>123, 124, 125, 134, 135, 145, 234, 235, 245, and 345</div>
|
||||
|
||||
In combinatorics, we use the notation, $\\displaystyle \\binom 5 3 = 10$
|
||||
|
||||
In general, $\\displaystyle \\binom n r = \\dfrac{n!}{r!(n-r)!}$, where $r \\le n$, $n! = n \\times (n-1) \\times ... \\times 3 \\times 2 \\times 1$, and $0! = 1$.
|
||||
|
||||
It is not until $n = 23$, that a value exceeds one-million: $\\displaystyle \\binom {23} {10} = 1144066$.
|
||||
|
||||
How many, not necessarily distinct, values of $\\displaystyle \\binom n r$ for $1 \\le n \\le 100$, are greater than one-million?
|
||||
|
||||
# --hints--
|
||||
|
||||
`combinatoricSelections(1000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof combinatoricSelections(1000) === 'number');
|
||||
```
|
||||
|
||||
`combinatoricSelections(1000)` should return 4626.
|
||||
|
||||
```js
|
||||
assert.strictEqual(combinatoricSelections(1000), 4626);
|
||||
```
|
||||
|
||||
`combinatoricSelections(10000)` should return 4431.
|
||||
|
||||
```js
|
||||
assert.strictEqual(combinatoricSelections(10000), 4431);
|
||||
```
|
||||
|
||||
`combinatoricSelections(100000)` should return 4255.
|
||||
|
||||
```js
|
||||
assert.strictEqual(combinatoricSelections(100000), 4255);
|
||||
```
|
||||
|
||||
`combinatoricSelections(1000000)` should return 4075.
|
||||
|
||||
```js
|
||||
assert.strictEqual(combinatoricSelections(1000000), 4075);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function combinatoricSelections(limit) {
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
combinatoricSelections(1000000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function combinatoricSelections(limit) {
|
||||
const factorial = n =>
|
||||
Array.apply(null, { length: n })
|
||||
.map((_, i) => i + 1)
|
||||
.reduce((p, c) => p * c, 1);
|
||||
|
||||
let result = 0;
|
||||
const nMax = 100;
|
||||
|
||||
for (let n = 1; n <= nMax; n++) {
|
||||
for (let r = 0; r <= n; r++) {
|
||||
if (factorial(n) / (factorial(r) * factorial(n - r)) >= limit)
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,113 @@
|
||||
---
|
||||
id: 5900f3a31000cf542c50feb6
|
||||
title: 'Problem 55: Lychrel numbers'
|
||||
challengeType: 1
|
||||
forumTopicId: 302166
|
||||
dashedName: problem-55-lychrel-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.
|
||||
|
||||
Not all numbers produce palindromes so quickly. For example,
|
||||
|
||||
<div style="margin-left: 4em;">
|
||||
349 + 943 = 1292,<br>
|
||||
1292 + 2921 = 4213<br>
|
||||
4213 + 3124 = 7337<br>
|
||||
</div>
|
||||
|
||||
That is, 349 took three iterations to arrive at a palindrome.
|
||||
|
||||
Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).
|
||||
|
||||
Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.
|
||||
|
||||
How many Lychrel numbers are there below `num`?
|
||||
|
||||
**Note:** Wording was modified slightly on 24 April 2007 to emphasize the theoretical nature of Lychrel numbers.
|
||||
|
||||
# --hints--
|
||||
|
||||
`countLychrelNumbers(1000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof countLychrelNumbers(1000) === 'number');
|
||||
```
|
||||
|
||||
`countLychrelNumbers(1000)` should return 13.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countLychrelNumbers(1000), 13);
|
||||
```
|
||||
|
||||
`countLychrelNumbers(3243)` should return 39.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countLychrelNumbers(3243), 39);
|
||||
```
|
||||
|
||||
`countLychrelNumbers(5000)` should return 76.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countLychrelNumbers(5000), 76);
|
||||
```
|
||||
|
||||
`countLychrelNumbers(7654)` should return 140.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countLychrelNumbers(7654), 140);
|
||||
```
|
||||
|
||||
`countLychrelNumbers(10000)` should return 249.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countLychrelNumbers(10000), 249);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function countLychrelNumbers(num) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
countLychrelNumbers(10000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const countLychrelNumbers = (size) => {
|
||||
const numReverse = (num) => {
|
||||
return Number(num.toString().split('').reverse().join(''));
|
||||
};
|
||||
const isPalin = (num) => {
|
||||
if (numReverse(num) === num) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
let total = 0;
|
||||
for (let i = 1; i < size; i++) {
|
||||
let loopCount = 1;
|
||||
let sum = i;
|
||||
while (loopCount < 50) {
|
||||
sum = sum + numReverse(sum);
|
||||
if (isPalin(sum)) {
|
||||
break;
|
||||
} else {
|
||||
loopCount++;
|
||||
}
|
||||
}
|
||||
if (loopCount === 50) {
|
||||
total++;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,101 @@
|
||||
---
|
||||
id: 5900f3a41000cf542c50feb7
|
||||
title: 'Problem 56: Powerful digit sum'
|
||||
challengeType: 1
|
||||
forumTopicId: 302167
|
||||
dashedName: problem-56-powerful-digit-sum
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A googol ($10^{100}$) is a massive number: one followed by one-hundred zeros; $100^{100}$ is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
|
||||
|
||||
Considering natural numbers of the form, $a^b$, where `a`, `b` < `n`, what is the maximum digital sum?
|
||||
|
||||
# --hints--
|
||||
|
||||
`powerfulDigitSum(3)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof powerfulDigitSum(3) === 'number');
|
||||
```
|
||||
|
||||
`powerfulDigitSum(3)` should return `4`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitSum(3), 4);
|
||||
```
|
||||
|
||||
`powerfulDigitSum(10)` should return `45`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitSum(10), 45);
|
||||
```
|
||||
|
||||
`powerfulDigitSum(50)` should return `406`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitSum(50), 406);
|
||||
```
|
||||
|
||||
`powerfulDigitSum(75)` should return `684`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitSum(75), 684);
|
||||
```
|
||||
|
||||
`powerfulDigitSum(100)` should return `972`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitSum(100), 972);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function powerfulDigitSum(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
powerfulDigitSum(3);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function powerfulDigitSum(n) {
|
||||
function sumDigitsOfPower(numA, numB) {
|
||||
let digitsSum = 0;
|
||||
let number = power(numA, numB);
|
||||
while (number > 0n) {
|
||||
const digit = number % 10n;
|
||||
digitsSum += parseInt(digit, 10);
|
||||
number = number / 10n;
|
||||
}
|
||||
return digitsSum;
|
||||
}
|
||||
|
||||
function power(numA, numB) {
|
||||
let sum = 1n;
|
||||
for (let b = 0; b < numB; b++) {
|
||||
sum = sum * BigInt(numA);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
const limit = n - 1;
|
||||
let maxDigitsSum = 0;
|
||||
for (let a = limit; a > 0; a--) {
|
||||
for (let b = limit; b > 0; b--) {
|
||||
const curDigitSum = sumDigitsOfPower(a, b);
|
||||
if (curDigitSum > maxDigitsSum) {
|
||||
maxDigitsSum = curDigitSum;
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxDigitsSum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 5900f3a51000cf542c50feb8
|
||||
title: 'Problem 57: Square root convergents'
|
||||
challengeType: 1
|
||||
forumTopicId: 302168
|
||||
dashedName: problem-57-square-root-convergents
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
It is possible to show that the square root of two can be expressed as an infinite continued fraction.
|
||||
|
||||
<div style='text-align: center;'>$\sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac 1 {2+ \dots}}}$</div>
|
||||
|
||||
By expanding this for the first four iterations, we get:
|
||||
|
||||
$1 + \\frac 1 2 = \\frac 32 = 1.5$
|
||||
|
||||
$1 + \\frac 1 {2 + \\frac 1 2} = \\frac 7 5 = 1.4$
|
||||
|
||||
$1 + \\frac 1 {2 + \\frac 1 {2+\\frac 1 2}} = \\frac {17}{12} = 1.41666 \\dots$
|
||||
|
||||
$1 + \\frac 1 {2 + \\frac 1 {2+\\frac 1 {2+\\frac 1 2}}} = \\frac {41}{29} = 1.41379 \\dots$
|
||||
|
||||
The next three expansions are $\\frac {99}{70}$, $\\frac {239}{169}$, and $\\frac {577}{408}$, but the eighth expansion, $\\frac {1393}{985}$, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.
|
||||
|
||||
In the first `n` expansions, how many fractions contain a numerator with more digits than denominator?
|
||||
|
||||
# --hints--
|
||||
|
||||
`squareRootConvergents(10)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof squareRootConvergents(10) === 'number');
|
||||
```
|
||||
|
||||
`squareRootConvergents(10)` should return 1.
|
||||
|
||||
```js
|
||||
assert.strictEqual(squareRootConvergents(10), 1);
|
||||
```
|
||||
|
||||
`squareRootConvergents(100)` should return 15.
|
||||
|
||||
```js
|
||||
assert.strictEqual(squareRootConvergents(100), 15);
|
||||
```
|
||||
|
||||
`squareRootConvergents(1000)` should return 153.
|
||||
|
||||
```js
|
||||
assert.strictEqual(squareRootConvergents(1000), 153);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function squareRootConvergents(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
squareRootConvergents(1000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function squareRootConvergents(n) {
|
||||
function countDigits(number) {
|
||||
let counter = 0;
|
||||
while (number > 0) {
|
||||
counter++;
|
||||
number = number / 10n;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
// Use BigInt as integer won't handle all cases
|
||||
let numerator = 3n;
|
||||
let denominator = 2n;
|
||||
let moreDigitsInNumerator = 0;
|
||||
|
||||
for (let i = 2; i <= n; i++) {
|
||||
[numerator, denominator] = [
|
||||
numerator + 2n * denominator,
|
||||
denominator + numerator
|
||||
];
|
||||
|
||||
if (countDigits(numerator) > countDigits(denominator)) {
|
||||
moreDigitsInNumerator++;
|
||||
}
|
||||
}
|
||||
return moreDigitsInNumerator;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,105 @@
|
||||
---
|
||||
id: 5900f3a61000cf542c50feb9
|
||||
title: 'Problem 58: Spiral primes'
|
||||
challengeType: 1
|
||||
forumTopicId: 302169
|
||||
dashedName: problem-58-spiral-primes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.
|
||||
|
||||
<div style='text-align: center;'>
|
||||
<strong><span style='color: red;'>37</span></strong> 36 35 34 33 32 <strong><span style='color: red;'>31</span></strong><br>
|
||||
38 <strong><span style='color: red;'>17</span></strong> 16 15 14 <strong><span style='color: red;'>13</span></strong> 30<br>
|
||||
39 18 <strong><span style='color: red;'>5</span></strong> 4 <strong><span style='color: red;'>3</span></strong> 12 29<br>
|
||||
40 19 6 1 2 11 28<br>
|
||||
41 20 <strong><span style='color: red;'>7</span></strong> 8 9 10 27<br>
|
||||
42 21 22 23 24 25 26<br>
|
||||
<strong><span style='color: red;'>43</span></strong> 44 45 46 47 48 49<br>
|
||||
</div>
|
||||
|
||||
It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%.
|
||||
|
||||
If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the percent of primes along both diagonals first falls below `percent`?
|
||||
|
||||
# --hints--
|
||||
|
||||
`spiralPrimes(50)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof spiralPrimes(50) === 'number');
|
||||
```
|
||||
|
||||
`spiralPrimes(50)` should return `11`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(spiralPrimes(50), 11);
|
||||
```
|
||||
|
||||
`spiralPrimes(15)` should return `981`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(spiralPrimes(15), 981);
|
||||
```
|
||||
|
||||
`spiralPrimes(10)` should return `26241`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(spiralPrimes(10), 26241);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function spiralPrimes(percent) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
spiralPrimes(50);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function spiralPrimes(percent) {
|
||||
function isPrime(n) {
|
||||
if (n <= 3) {
|
||||
return n > 1;
|
||||
} else if (n % 2 === 0 || n % 3 === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 5; i * i <= n; i += 6) {
|
||||
if (n % i === 0 || n % (i + 2) === 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
let totalCount = 1;
|
||||
let primesCount = 0;
|
||||
let curNumber = 1;
|
||||
let curSideLength = 1;
|
||||
let ratio = 1;
|
||||
const wantedRatio = percent / 100;
|
||||
|
||||
while (ratio >= wantedRatio) {
|
||||
curSideLength += 2;
|
||||
for (let i = 0; i < 4; i++) {
|
||||
curNumber += curSideLength - 1;
|
||||
totalCount++;
|
||||
if (i !== 3 && isPrime(curNumber)) {
|
||||
primesCount++;
|
||||
}
|
||||
}
|
||||
ratio = primesCount / totalCount;
|
||||
}
|
||||
return curSideLength;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
id: 5900f3a81000cf542c50feba
|
||||
title: 'Problem 59: XOR decryption'
|
||||
challengeType: 1
|
||||
forumTopicId: 302170
|
||||
dashedName: problem-59-xor-decryption
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (\*) = 42, and lowercase k = 107.
|
||||
|
||||
A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.
|
||||
|
||||
For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message.
|
||||
|
||||
Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.
|
||||
|
||||
Your task has been made easy, as the encryption key consists of three lower case characters. Using `cipher`, an array containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.
|
||||
|
||||
# --hints--
|
||||
|
||||
`XORDecryption(cipher)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof XORDecryption(cipher) === 'number');
|
||||
```
|
||||
|
||||
`XORDecryption(cipher)` should return 129448.
|
||||
|
||||
```js
|
||||
assert.strictEqual(XORDecryption(cipher), 129448);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function XORDecryption(arr) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
const cipher = [
|
||||
36,22,80,0,0,4,23,25,19,17,88,4,4,19,21,11,88,22,23,23,29,69,12,24,0,88,25,11,12,2,10,28,5,6,12,25,10,22,80,10,30,80,10,22,21,69,23,22,69,61,5,9,29,2,66,11,80,8,23,3,17,88,19,0,20,21,7,10,17,17,29,20,69,8,17,21,29,2,22,84,80,71,60,21,69,11,5,8,21,25,22,88,3,0,10,25,0,10,5,8,88,2,0,27,25,21,10,31,6,25,2,16,21,82,69,35,63,11,88,4,13,29,80,22,13,29,22,88,31,3,88,3,0,10,25,0,11,80,10,30,80,23,29,19,12,8,2,10,27,17,9,11,45,95,88,57,69,16,17,19,29,80,23,29,19,0,22,4,9,1,80,3,23,5,11,28,92,69,9,5,12,12,21,69,13,30,0,0,0,0,27,4,0,28,28,28,84,80,4,22,80,0,20,21,2,25,30,17,88,21,29,8,2,0,11,3,12,23,30,69,30,31,23,88,4,13,29,80,0,22,4,12,10,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,65,69,83,80,84,87,68,69,83,80,84,87,73,69,83,80,84,87,65,83,88,91,69,29,4,6,86,92,69,15,24,12,27,24,69,28,21,21,29,30,1,11,80,10,22,80,17,16,21,69,9,5,4,28,2,4,12,5,23,29,80,10,30,80,17,16,21,69,27,25,23,27,28,0,84,80,22,23,80,17,16,17,17,88,25,3,88,4,13,29,80,17,10,5,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,12,11,80,10,26,4,4,17,30,0,28,92,69,30,2,10,21,80,12,12,80,4,12,80,10,22,19,0,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,69,30,31,9,20,31,18,11,94,69,54,17,8,29,28,28,84,80,44,88,24,4,14,21,69,30,31,16,22,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,25,22,88,17,69,11,25,29,12,24,69,8,17,23,12,80,10,30,80,17,16,21,69,11,1,16,25,2,0,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,23,22,69,12,24,0,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,67,80,10,10,80,7,1,80,21,13,4,17,17,30,2,88,4,13,29,80,22,13,29,69,23,22,69,12,24,12,11,80,22,29,2,12,29,3,69,29,1,16,25,28,69,12,31,69,11,92,69,17,4,69,16,17,22,88,4,13,29,80,23,25,4,12,23,80,22,9,2,17,80,70,76,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,11,80,17,23,80,84,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,12,31,69,12,24,0,88,20,12,25,29,0,12,21,23,86,80,44,88,7,12,20,28,69,11,31,10,22,80,22,16,31,18,88,4,13,25,4,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,17,23,80,7,29,80,4,8,0,23,23,8,12,21,17,17,29,28,28,88,65,75,78,68,81,65,67,81,72,70,83,64,68,87,74,70,81,75,70,81,67,80,4,22,20,69,30,2,10,21,80,8,13,28,17,17,0,9,1,25,11,31,80,17,16,25,22,88,30,16,21,18,0,10,80,7,1,80,22,17,8,73,88,17,11,28,80,17,16,21,11,88,4,4,19,25,11,31,80,17,16,21,69,11,1,16,25,2,0,88,2,10,23,4,73,88,4,13,29,80,11,13,29,7,29,2,69,75,94,84,76,65,80,65,66,83,77,67,80,64,73,82,65,67,87,75,72,69,17,3,69,17,30,1,29,21,1,88,0,23,23,20,16,27,21,1,84,80,18,16,25,6,16,80,0,0,0,23,29,3,22,29,3,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,35,23,28,9,23,7,12,22,23,69,25,23,4,17,30,69,12,24,0,88,3,4,21,21,69,11,4,0,8,3,69,26,9,69,15,24,12,27,24,69,49,80,13,25,20,69,25,2,23,17,6,0,28,80,4,12,80,17,16,25,22,88,3,16,21,92,69,49,80,13,25,6,0,88,20,12,11,19,10,14,21,23,29,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,29,80,22,29,2,12,29,3,69,73,80,78,88,65,74,73,70,69,83,80,84,87,72,84,88,91,69,73,95,87,77,70,69,83,80,84,87,70,87,77,80,78,88,21,17,27,94,69,25,28,22,23,80,1,29,0,0,22,20,22,88,31,11,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,75,88,62,4,21,21,9,1,92,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,65,64,69,31,25,19,29,3,69,12,24,0,88,18,12,9,5,4,28,2,4,12,21,69,80,22,10,13,2,17,16,80,21,23,7,0,10,89,69,23,22,69,12,24,0,88,19,12,10,19,16,21,22,0,10,21,11,27,21,69,23,22,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,36,22,20,69,26,9,69,11,25,8,17,28,4,10,80,23,29,17,22,23,30,12,22,23,69,49,80,13,25,6,0,88,28,12,19,21,18,17,3,0,88,18,0,29,30,69,25,18,9,29,80,17,23,80,1,29,4,0,10,29,12,22,21,69,12,24,0,88,3,16,21,3,69,23,22,69,12,24,0,88,3,16,26,3,0,9,5,0,22,4,69,11,21,23,17,21,22,88,25,11,88,7,13,17,19,13,88,4,13,29,80,0,0,0,10,22,21,11,12,3,69,25,2,0,88,21,19,29,30,69,22,5,8,26,21,23,11,94
|
||||
];
|
||||
|
||||
XORDecryption(cipher);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 5900f3721000cf542c50fe85
|
||||
title: 'Problem 6: Sum square difference'
|
||||
challengeType: 1
|
||||
forumTopicId: 302171
|
||||
dashedName: problem-6-sum-square-difference
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
|
||||
<div style='text-align: center;'>1<sup>2</sup> + 2<sup>2</sup> + ... + 10<sup>2</sup> = 385</div>
|
||||
|
||||
The square of the sum of the first ten natural numbers is,
|
||||
|
||||
<div style='text-align: center;'>(1 + 2 + ... + 10)<sup>2</sup> = 55<sup>2</sup> = 3025</div>
|
||||
|
||||
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
|
||||
|
||||
Find the difference between the sum of the squares of the first `n` natural numbers and the square of the sum.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sumSquareDifference(10)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof sumSquareDifference(10) === 'number');
|
||||
```
|
||||
|
||||
`sumSquareDifference(10)` should return 2640.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumSquareDifference(10), 2640);
|
||||
```
|
||||
|
||||
`sumSquareDifference(20)` should return 41230.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumSquareDifference(20), 41230);
|
||||
```
|
||||
|
||||
`sumSquareDifference(100)` should return 25164150.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumSquareDifference(100), 25164150);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sumSquareDifference(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
sumSquareDifference(100);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const sumSquareDifference = (number)=>{
|
||||
let squareOfSum = Math.pow(sumOfArithmeticSeries(1,1,number),2);
|
||||
let sumOfSquare = sumOfSquareOfNumbers(number);
|
||||
return squareOfSum - sumOfSquare;
|
||||
}
|
||||
|
||||
function sumOfArithmeticSeries(a,d,n){
|
||||
return (n/2)*(2*a+(n-1)*d);
|
||||
}
|
||||
|
||||
function sumOfSquareOfNumbers(n){
|
||||
return (n*(n+1)*(2*n+1))/6;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
id: 5900f3a81000cf542c50febb
|
||||
title: 'Problem 60: Prime pair sets'
|
||||
challengeType: 1
|
||||
forumTopicId: 302172
|
||||
dashedName: problem-60-prime-pair-sets
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
|
||||
|
||||
Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
|
||||
|
||||
# --hints--
|
||||
|
||||
`primePairSets()` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof primePairSets() === 'number');
|
||||
```
|
||||
|
||||
`primePairSets()` should return 26033.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primePairSets(), 26033);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function primePairSets() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
primePairSets();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,252 @@
|
||||
---
|
||||
id: 5900f3a91000cf542c50febc
|
||||
title: 'Problem 61: Cyclical figurate numbers'
|
||||
challengeType: 1
|
||||
forumTopicId: 302173
|
||||
dashedName: problem-61-cyclical-figurate-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:
|
||||
|
||||
| Type of Number | Formula | Sequence |
|
||||
| -------------- | ----------------------------- | --------------------- |
|
||||
| Triangle | $P_3(n) = \frac{n(n+1)}{2}$ | 1, 3, 6, 10, 15, ... |
|
||||
| Square | $P_4(n) = n^2$ | 1, 4, 9, 16, 25, ... |
|
||||
| Pentagonal | $P_5(n) = \frac{n(3n−1)}2$ | 1, 5, 12, 22, 35, ... |
|
||||
| Hexagonal | $P_6(n) = n(2n−1)$ | 1, 6, 15, 28, 45, ... |
|
||||
| Heptagonal | $P_7(n) = \frac{n(5n−3)}{2}$ | 1, 7, 18, 34, 55, ... |
|
||||
| Octagonal | $P_8(n) = n(3n−2)$ | 1, 8, 21, 40, 65, ... |
|
||||
|
||||
The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.
|
||||
|
||||
1. The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).
|
||||
2. Each polygonal type: triangle ($P_3(127) = 8128$), square ($P_4(91) = 8281$), and pentagonal ($P_5(44) = 2882$), is represented by a different number in the set.
|
||||
3. This is the only set of 4-digit numbers with this property.
|
||||
|
||||
Find the sum of all numbers in ordered sets of `n` cyclic 4-digit numbers for which each of the $P_3$ to $P_{n + 2}$ polygonal types, is represented by a different number in the set.
|
||||
|
||||
# --hints--
|
||||
|
||||
`cyclicalFigurateNums(3)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof cyclicalFigurateNums(3) === 'number');
|
||||
```
|
||||
|
||||
`cyclicalFigurateNums(3)` should return `19291`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cyclicalFigurateNums(3), 19291);
|
||||
```
|
||||
|
||||
`cyclicalFigurateNums(4)` should return `28684`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cyclicalFigurateNums(4), 28684);
|
||||
```
|
||||
|
||||
`cyclicalFigurateNums(5)` should return `76255`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cyclicalFigurateNums(5), 76255);
|
||||
```
|
||||
|
||||
`cyclicalFigurateNums(6)` should return `28684`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cyclicalFigurateNums(6), 28684);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function cyclicalFigurateNums(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
cyclicalFigurateNums(3);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function cyclicalFigurateNums(n) {
|
||||
function getChains(chain, n, numberTypes, numsExcludingLastNeededType) {
|
||||
if (chain.length === n) {
|
||||
return [chain];
|
||||
}
|
||||
|
||||
const nextNumbers = getNextNumbersInChain(
|
||||
chain[chain.length - 1],
|
||||
numsExcludingLastNeededType
|
||||
);
|
||||
|
||||
const chains = [];
|
||||
for (let j = 0; j < nextNumbers.length; j++) {
|
||||
const nextNumber = nextNumbers[j];
|
||||
if (chain.indexOf(nextNumber) === -1) {
|
||||
const nextChain = [...chain, nextNumber];
|
||||
chains.push(
|
||||
...getChains(nextChain, n, numberTypes, numsExcludingLastNeededType)
|
||||
);
|
||||
}
|
||||
}
|
||||
return chains;
|
||||
}
|
||||
|
||||
function getNextNumbersInChain(num, numsExcludingLastNeededType) {
|
||||
const results = [];
|
||||
const beginning = num % 100;
|
||||
numsExcludingLastNeededType.forEach(number => {
|
||||
if (Math.floor(number / 100) === beginning) {
|
||||
results.push(number);
|
||||
}
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
function fillNumberTypes(n, numberTypes, numsExcludingLastNeededType) {
|
||||
const [, lastTypeCheck, lastTypeArr] = numberTypes[n - 1];
|
||||
|
||||
for (let i = 1000; i <= 9999; i++) {
|
||||
for (let j = 0; j < n - 1; j++) {
|
||||
const [, typeCheck, typeArr] = numberTypes[j];
|
||||
if (typeCheck(i)) {
|
||||
typeArr.push(i);
|
||||
numsExcludingLastNeededType.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (lastTypeCheck(i)) {
|
||||
lastTypeArr.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isCyclicalChain(chain, n, numberTypes) {
|
||||
const numberTypesInChain = getNumberTypesInChain(chain, numberTypes);
|
||||
|
||||
if (!isChainAllowed(numberTypesInChain, n)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const isChainCyclic =
|
||||
Math.floor(chain[0] / 100) === chain[chain.length - 1] % 100;
|
||||
return isChainCyclic;
|
||||
}
|
||||
|
||||
function getNumberTypesInChain(chain, numberTypes) {
|
||||
const numbersInChain = {};
|
||||
for (let i = 0; i < numberTypes.length; i++) {
|
||||
const numberTypeName = numberTypes[i][0];
|
||||
numbersInChain[numberTypeName] = [];
|
||||
}
|
||||
|
||||
for (let i = 0; i < chain.length; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
const [typeName, , typeNumbers] = numberTypes[j];
|
||||
const typeNumbersInChain = numbersInChain[typeName];
|
||||
if (typeNumbers.indexOf(chain[i]) !== -1) {
|
||||
typeNumbersInChain.push(chain[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return numbersInChain;
|
||||
}
|
||||
|
||||
function isChainAllowed(numberTypesInChain, n) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
const typeName = numberTypes[i][0];
|
||||
const isNumberWithTypeInChain = numberTypesInChain[typeName].length > 0;
|
||||
if (!isNumberWithTypeInChain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
const otherTypeName = numberTypes[j][0];
|
||||
if (
|
||||
isNumberRepeatedAsOnlyNumberInTwoTypes(
|
||||
numberTypesInChain[typeName],
|
||||
numberTypesInChain[otherTypeName]
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function isNumberRepeatedAsOnlyNumberInTwoTypes(
|
||||
typeNumbers,
|
||||
otherTypeNumbers
|
||||
) {
|
||||
return (
|
||||
typeNumbers.length === 1 &&
|
||||
otherTypeNumbers.length === 1 &&
|
||||
typeNumbers[0] === otherTypeNumbers[0]
|
||||
);
|
||||
}
|
||||
|
||||
function isTriangle(num) {
|
||||
return ((8 * num + 1) ** 0.5 - 1) % 2 === 0;
|
||||
}
|
||||
|
||||
function isSquare(num) {
|
||||
return num ** 0.5 === parseInt(num ** 0.5, 10);
|
||||
}
|
||||
|
||||
function isPentagonal(num) {
|
||||
return ((24 * num + 1) ** 0.5 + 1) % 6 === 0;
|
||||
}
|
||||
|
||||
function isHexagonal(num) {
|
||||
return ((8 * num + 1) ** 0.5 + 1) % 4 === 0;
|
||||
}
|
||||
|
||||
function isHeptagonal(num) {
|
||||
return ((40 * num + 9) ** 0.5 + 3) % 10 === 0;
|
||||
}
|
||||
|
||||
function isOctagonal(num) {
|
||||
return ((3 * num + 1) ** 0.5 + 1) % 3 === 0;
|
||||
}
|
||||
|
||||
const numberTypes = [
|
||||
['triangle', isTriangle, []],
|
||||
['square', isSquare, []],
|
||||
['pentagonal', isPentagonal, []],
|
||||
['hexagonal', isHexagonal, []],
|
||||
['heptagonal', isHeptagonal, []],
|
||||
['octagonal', isOctagonal, []]
|
||||
];
|
||||
const numsExcludingLastNeededType = new Set();
|
||||
fillNumberTypes(n, numberTypes, numsExcludingLastNeededType);
|
||||
|
||||
const nNumberChains = [];
|
||||
const [, , lastType] = numberTypes[n - 1];
|
||||
for (let i = 0; i < lastType.length; i++) {
|
||||
const startOfChain = lastType[i];
|
||||
nNumberChains.push(
|
||||
...getChains([startOfChain], n, numberTypes, numsExcludingLastNeededType)
|
||||
);
|
||||
}
|
||||
|
||||
const cyclicalChains = nNumberChains.filter(chain =>
|
||||
isCyclicalChain(chain, n, numberTypes)
|
||||
);
|
||||
|
||||
let sum = 0;
|
||||
for (let i = 0; i < cyclicalChains.length; i++) {
|
||||
for (let j = 0; j < cyclicalChains[0].length; j++) {
|
||||
sum += cyclicalChains[i][j];
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,97 @@
|
||||
---
|
||||
id: 5900f3aa1000cf542c50febd
|
||||
title: 'Problem 62: Cubic permutations'
|
||||
challengeType: 1
|
||||
forumTopicId: 302174
|
||||
dashedName: problem-62-cubic-permutations
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The cube, 41063625 ($345^3$), can be permuted to produce two other cubes: 56623104 ($384^3$) and 66430125 ($405^3$). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
|
||||
|
||||
Find the smallest cube for which exactly `n` permutations of its digits are cube.
|
||||
|
||||
# --hints--
|
||||
|
||||
`cubicPermutations(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof cubicPermutations(2) === 'number');
|
||||
```
|
||||
|
||||
`cubicPermutations(2)` should return `125`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cubicPermutations(2), 125);
|
||||
```
|
||||
|
||||
`cubicPermutations(3)` should return `41063625`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cubicPermutations(3), 41063625);
|
||||
```
|
||||
|
||||
`cubicPermutations(4)` should return `1006012008`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cubicPermutations(4), 1006012008);
|
||||
```
|
||||
|
||||
`cubicPermutations(5)` should return `127035954683`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cubicPermutations(5), 127035954683);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function cubicPermutations(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
cubicPermutations(2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function cubicPermutations(n) {
|
||||
function getDigits(num) {
|
||||
const digits = [];
|
||||
while (num > 0) {
|
||||
digits.push(num % 10);
|
||||
num = Math.floor(num / 10);
|
||||
}
|
||||
return digits;
|
||||
}
|
||||
|
||||
function getCube(num) {
|
||||
return num ** 3;
|
||||
}
|
||||
|
||||
const digitsToCubeCounts = {};
|
||||
let curNum = 1;
|
||||
let digits;
|
||||
|
||||
while (!digitsToCubeCounts[digits] || digitsToCubeCounts[digits].count < n) {
|
||||
const cube = getCube(curNum);
|
||||
digits = getDigits(cube).sort().join();
|
||||
if (!digitsToCubeCounts[digits]) {
|
||||
digitsToCubeCounts[digits] = {
|
||||
count: 1,
|
||||
smallestCube: cube
|
||||
};
|
||||
} else {
|
||||
digitsToCubeCounts[digits].count += 1;
|
||||
}
|
||||
|
||||
curNum++;
|
||||
}
|
||||
return digitsToCubeCounts[digits].smallestCube;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,122 @@
|
||||
---
|
||||
id: 5900f3ab1000cf542c50febe
|
||||
title: 'Problem 63: Powerful digit counts'
|
||||
challengeType: 1
|
||||
forumTopicId: 302175
|
||||
dashedName: problem-63-powerful-digit-counts
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The 5-digit number, 16807 = 7<sup>5</sup>, is also a fifth power. Similarly, the 9-digit number, 134217728 = 8<sup>9</sup>, is a ninth power.
|
||||
|
||||
Complete the function so that it returns how many positive integers are of length `n` and an `n`th power.
|
||||
|
||||
# --hints--
|
||||
|
||||
`powerfulDigitCounts(1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof powerfulDigitCounts(1) === 'number');
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(1)` should return `9`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(1), 9);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(2)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(2), 6);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(3)` should return `5`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(3), 5);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(4)` should return `4`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(4), 4);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(5)` should return `3`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(5), 3);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(6)` should return `3`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(6), 3);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(7)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(7), 2);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(8)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(8), 2);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(10)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(10), 2);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(21)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(21), 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function powerfulDigitCounts(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
powerfulDigitCounts(1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function powerfulDigitCounts(n) {
|
||||
function countDigits(num) {
|
||||
let counter = 0;
|
||||
while (num > 0) {
|
||||
num = Math.floor(num / 10);
|
||||
counter++;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
let numbersCount = 0;
|
||||
|
||||
let curNum = 1;
|
||||
while (curNum < 10) {
|
||||
let power = n;
|
||||
if (power === countDigits(curNum ** power)) {
|
||||
numbersCount++;
|
||||
}
|
||||
curNum++;
|
||||
}
|
||||
|
||||
return numbersCount;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,148 @@
|
||||
---
|
||||
id: 5900f3ac1000cf542c50febf
|
||||
title: 'Problem 64: Odd period square roots'
|
||||
challengeType: 1
|
||||
forumTopicId: 302176
|
||||
dashedName: problem-64-odd-period-square-roots
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
All square roots are periodic when written as continued fractions and can be written in the form:
|
||||
|
||||
$\\displaystyle \\quad \\quad \\sqrt{N}=a_0+\\frac 1 {a_1+\\frac 1 {a_2+ \\frac 1 {a3+ \\dots}}}$
|
||||
|
||||
For example, let us consider $\\sqrt{23}$:
|
||||
|
||||
$\\quad \\quad \\sqrt{23}=4+\\sqrt{23}-4=4+\\frac 1 {\\frac 1 {\\sqrt{23}-4}}=4+\\frac 1 {1+\\frac{\\sqrt{23}-3}7}$
|
||||
|
||||
If we continue we would get the following expansion:
|
||||
|
||||
$\\displaystyle \\quad \\quad \\sqrt{23}=4+\\frac 1 {1+\\frac 1 {3+ \\frac 1 {1+\\frac 1 {8+ \\dots}}}}$
|
||||
|
||||
The process can be summarized as follows:
|
||||
|
||||
$\\quad \\quad a_0=4, \\frac 1 {\\sqrt{23}-4}=\\frac {\\sqrt{23}+4} 7=1+\\frac {\\sqrt{23}-3} 7$
|
||||
|
||||
$\\quad \\quad a_1=1, \\frac 7 {\\sqrt{23}-3}=\\frac {7(\\sqrt{23}+3)} {14}=3+\\frac {\\sqrt{23}-3} 2$
|
||||
|
||||
$\\quad \\quad a_2=3, \\frac 2 {\\sqrt{23}-3}=\\frac {2(\\sqrt{23}+3)} {14}=1+\\frac {\\sqrt{23}-4} 7$
|
||||
|
||||
$\\quad \\quad a_3=1, \\frac 7 {\\sqrt{23}-4}=\\frac {7(\\sqrt{23}+4)} 7=8+\\sqrt{23}-4$
|
||||
|
||||
$\\quad \\quad a_4=8, \\frac 1 {\\sqrt{23}-4}=\\frac {\\sqrt{23}+4} 7=1+\\frac {\\sqrt{23}-3} 7$
|
||||
|
||||
$\\quad \\quad a_5=1, \\frac 7 {\\sqrt{23}-3}=\\frac {7 (\\sqrt{23}+3)} {14}=3+\\frac {\\sqrt{23}-3} 2$
|
||||
|
||||
$\\quad \\quad a_6=3, \\frac 2 {\\sqrt{23}-3}=\\frac {2(\\sqrt{23}+3)} {14}=1+\\frac {\\sqrt{23}-4} 7$
|
||||
|
||||
$\\quad \\quad a_7=1, \\frac 7 {\\sqrt{23}-4}=\\frac {7(\\sqrt{23}+4)} {7}=8+\\sqrt{23}-4$
|
||||
|
||||
It can be seen that the sequence is repeating. For conciseness, we use the notation $\\sqrt{23}=\[4;(1,3,1,8)]$, to indicate that the block (1,3,1,8) repeats indefinitely.
|
||||
|
||||
The first ten continued fraction representations of (irrational) square roots are:
|
||||
|
||||
$\\quad \\quad \\sqrt{2}=\[1;(2)]$, period = 1
|
||||
|
||||
$\\quad \\quad \\sqrt{3}=\[1;(1,2)]$, period = 2
|
||||
|
||||
$\\quad \\quad \\sqrt{5}=\[2;(4)]$, period = 1
|
||||
|
||||
$\\quad \\quad \\sqrt{6}=\[2;(2,4)]$, period = 2
|
||||
|
||||
$\\quad \\quad \\sqrt{7}=\[2;(1,1,1,4)]$, period = 4
|
||||
|
||||
$\\quad \\quad \\sqrt{8}=\[2;(1,4)]$, period = 2
|
||||
|
||||
$\\quad \\quad \\sqrt{10}=\[3;(6)]$, period = 1
|
||||
|
||||
$\\quad \\quad \\sqrt{11}=\[3;(3,6)]$, period = 2
|
||||
|
||||
$\\quad \\quad \\sqrt{12}=\[3;(2,6)]$, period = 2
|
||||
|
||||
$\\quad \\quad \\sqrt{13}=\[3;(1,1,1,1,6)]$, period = 5
|
||||
|
||||
Exactly four continued fractions, for $N \\le 13$, have an odd period.
|
||||
|
||||
How many continued fractions for $N \\le n$ have an odd period?
|
||||
|
||||
# --hints--
|
||||
|
||||
`oddPeriodSqrts(13)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof oddPeriodSqrts(13) === 'number');
|
||||
```
|
||||
|
||||
`oddPeriodSqrts(500)` should return `83`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(oddPeriodSqrts(500), 83);
|
||||
```
|
||||
|
||||
`oddPeriodSqrts(1000)` should return `152`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(oddPeriodSqrts(1000), 152);
|
||||
```
|
||||
|
||||
`oddPeriodSqrts(5000)` should return `690`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(oddPeriodSqrts(5000), 690);
|
||||
```
|
||||
|
||||
`oddPeriodSqrts(10000)` should return `1322`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(oddPeriodSqrts(10000), 1322);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function oddPeriodSqrts(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
oddPeriodSqrts(13);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function oddPeriodSqrts(n) {
|
||||
// Based on https://www.mathblog.dk/project-euler-continued-fractions-odd-period/
|
||||
function getPeriod(num) {
|
||||
let period = 0;
|
||||
let m = 0;
|
||||
let d = 1;
|
||||
let a = Math.floor(Math.sqrt(num));
|
||||
const a0 = a;
|
||||
while (2 * a0 !== a) {
|
||||
m = d * a - m;
|
||||
d = Math.floor((num - m ** 2) / d);
|
||||
a = Math.floor((Math.sqrt(num) + m) / d);
|
||||
period++;
|
||||
}
|
||||
return period;
|
||||
}
|
||||
|
||||
function isPerfectSquare(num) {
|
||||
return Number.isInteger(Math.sqrt(num));
|
||||
}
|
||||
|
||||
let counter = 0;
|
||||
for (let i = 2; i <= n; i++) {
|
||||
if (!isPerfectSquare(i)) {
|
||||
if (getPeriod(i) % 2 !== 0) {
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,117 @@
|
||||
---
|
||||
id: 5900f3ad1000cf542c50fec0
|
||||
title: 'Problem 65: Convergents of e'
|
||||
challengeType: 1
|
||||
forumTopicId: 302177
|
||||
dashedName: problem-65-convergents-of-e
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The square root of 2 can be written as an infinite continued fraction.
|
||||
|
||||
$\\sqrt{2} = 1 + \\dfrac{1}{2 + \\dfrac{1}{2 + \\dfrac{1}{2 + \\dfrac{1}{2 + ...}}}}$
|
||||
|
||||
The infinite continued fraction can be written, $\\sqrt{2} = \[1; (2)]$ indicates that 2 repeats *ad infinitum*. In a similar way, $\\sqrt{23} = \[4; (1, 3, 1, 8)]$. It turns out that the sequence of partial values of continued fractions for square roots provide the best rational approximations. Let us consider the convergents for $\\sqrt{2}$.
|
||||
|
||||
$1 + \\dfrac{1}{2} = \\dfrac{3}{2}\\\\ 1 + \\dfrac{1}{2 + \\dfrac{1}{2}} = \\dfrac{7}{5}\\\\ 1 + \\dfrac{1}{2 + \\dfrac{1}{2 + \\dfrac{1}{2}}} = \\dfrac{17}{12}\\\\ 1 + \\dfrac{1}{2 + \\dfrac{1}{2 + \\dfrac{1}{2 + \\dfrac{1}{2}}}} = \\dfrac{41}{29}$
|
||||
|
||||
Hence the sequence of the first ten convergents for $\\sqrt{2}$ are:
|
||||
|
||||
$1, \\dfrac{3}{2}, \\dfrac{7}{5}, \\dfrac{17}{12}, \\dfrac{41}{29}, \\dfrac{99}{70}, \\dfrac{239}{169}, \\dfrac{577}{408}, \\dfrac{1393}{985}, \\dfrac{3363}{2378}, ...$
|
||||
|
||||
What is most surprising is that the important mathematical constant, $e = \[2; 1, 2, 1, 1, 4, 1, 1, 6, 1, ... , 1, 2k, 1, ...]$. The first ten terms in the sequence of convergents for `e` are:
|
||||
|
||||
$2, 3, \\dfrac{8}{3}, \\dfrac{11}{4}, \\dfrac{19}{7}, \\dfrac{87}{32}, \\dfrac{106}{39}, \\dfrac{193}{71}, \\dfrac{1264}{465}, \\dfrac{1457}{536}, ...$
|
||||
|
||||
The sum of digits in the numerator of the 10<sup>th</sup> convergent is $1 + 4 + 5 + 7 = 17$.
|
||||
|
||||
Find the sum of digits in the numerator of the `n`<sup>th</sup> convergent of the continued fraction for `e`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`convergentsOfE(10)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof convergentsOfE(10) === 'number');
|
||||
```
|
||||
|
||||
`convergentsOfE(10)` should return `17`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(convergentsOfE(10), 17);
|
||||
```
|
||||
|
||||
`convergentsOfE(30)` should return `53`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(convergentsOfE(30), 53);
|
||||
```
|
||||
|
||||
`convergentsOfE(50)` should return `91`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(convergentsOfE(50), 91);
|
||||
```
|
||||
|
||||
`convergentsOfE(70)` should return `169`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(convergentsOfE(70), 169);
|
||||
```
|
||||
|
||||
`convergentsOfE(100)` should return `272`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(convergentsOfE(100), 272);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function convergentsOfE(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
convergentsOfE(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function convergentsOfE(n) {
|
||||
function sumDigits(num) {
|
||||
let sum = 0n;
|
||||
while (num > 0) {
|
||||
sum += num % 10n;
|
||||
num = num / 10n;
|
||||
}
|
||||
return parseInt(sum);
|
||||
}
|
||||
|
||||
// BigInt is needed for high convergents
|
||||
let convergents = [
|
||||
[2n, 1n],
|
||||
[3n, 1n]
|
||||
];
|
||||
const multipliers = [1n, 1n, 2n];
|
||||
for (let i = 2; i < n; i++) {
|
||||
const [secondLastConvergent, lastConvergent] = convergents;
|
||||
const [secondLastNumerator, secondLastDenominator] = secondLastConvergent;
|
||||
const [lastNumerator, lastDenominator] = lastConvergent;
|
||||
const curMultiplier = multipliers[i % 3];
|
||||
|
||||
const numerator = secondLastNumerator + curMultiplier * lastNumerator;
|
||||
const denominator = secondLastDenominator + curMultiplier * lastDenominator;
|
||||
|
||||
convergents = [lastConvergent, [numerator, denominator]]
|
||||
if (i % 3 === 2) {
|
||||
multipliers[2] += 2n;
|
||||
}
|
||||
}
|
||||
return sumDigits(convergents[1][0]);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,129 @@
|
||||
---
|
||||
id: 5900f3ae1000cf542c50fec1
|
||||
title: 'Problem 66: Diophantine equation'
|
||||
challengeType: 1
|
||||
forumTopicId: 302178
|
||||
dashedName: problem-66-diophantine-equation
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Consider quadratic Diophantine equations of the form:
|
||||
|
||||
<div style='text-align: center;'>x<sup>2</sup> – Dy<sup>2</sup> = 1</div>
|
||||
|
||||
For example, when D=13, the minimal solution in x is 649<sup>2</sup> – 13×180<sup>2</sup> = 1.
|
||||
|
||||
It can be assumed that there are no solutions in positive integers when D is square.
|
||||
|
||||
By finding minimal solutions in x for D = {2, 3, 5, 6, 7}, we obtain the following:
|
||||
|
||||
<div style='margin-left: 2em;'>
|
||||
3<sup>2</sup> – 2×2<sup>2</sup> = 1<br>
|
||||
2<sup>2</sup> – 3×1<sup>2</sup> = 1<br>
|
||||
<strong><span style='color: red;'>9</span></strong><sup>2</sup> – 5×4<sup>2</sup> = 1<br>
|
||||
5<sup>2</sup> – 6×2<sup>2</sup> = 1<br>
|
||||
8<sup>2</sup> – 7×3<sup>2</sup> = 1<br>
|
||||
</div>
|
||||
|
||||
Hence, by considering minimal solutions in `x` for D ≤ 7, the largest `x` is obtained when D=5.
|
||||
|
||||
Find the value of D ≤ `n` in minimal solutions of `x` for which the largest value of `x` is obtained.
|
||||
|
||||
# --hints--
|
||||
|
||||
`diophantineEquation(7)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof diophantineEquation(7) === 'number');
|
||||
```
|
||||
|
||||
`diophantineEquation(7)` should return `5`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(diophantineEquation(7), 5);
|
||||
```
|
||||
|
||||
`diophantineEquation(100)` should return `61`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(diophantineEquation(100), 61);
|
||||
```
|
||||
|
||||
`diophantineEquation(409)` should return `409`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(diophantineEquation(409), 409);
|
||||
```
|
||||
|
||||
`diophantineEquation(500)` should return `421`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(diophantineEquation(500), 421);
|
||||
```
|
||||
|
||||
`diophantineEquation(1000)` should return `661`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(diophantineEquation(1000), 661);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function diophantineEquation(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
diophantineEquation(7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function diophantineEquation(n) {
|
||||
// Based on https://www.mathblog.dk/project-euler-66-diophantine-equation/
|
||||
function isSolution(D, numerator, denominator) {
|
||||
return numerator * numerator - BigInt(D) * denominator * denominator === 1n;
|
||||
}
|
||||
|
||||
let result = 0;
|
||||
let biggestX = 0;
|
||||
|
||||
for (let D = 2; D <= n; D++) {
|
||||
let boundary = Math.floor(Math.sqrt(D));
|
||||
if (boundary ** 2 === D) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let m = 0n;
|
||||
let d = 1n;
|
||||
let a = BigInt(boundary);
|
||||
|
||||
let [numerator, prevNumerator] = [a, 1n];
|
||||
|
||||
let [denominator, prevDenominator] = [1n, 0n];
|
||||
|
||||
while (!isSolution(D, numerator, denominator)) {
|
||||
m = d * a - m;
|
||||
d = (BigInt(D) - m * m) / d;
|
||||
a = (BigInt(boundary) + m) / d;
|
||||
|
||||
[numerator, prevNumerator] = [a * numerator + prevNumerator, numerator];
|
||||
[denominator, prevDenominator] = [
|
||||
a * denominator + prevDenominator,
|
||||
denominator
|
||||
];
|
||||
}
|
||||
|
||||
if (numerator > biggestX) {
|
||||
biggestX = numerator;
|
||||
result = D;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 5900f3b01000cf542c50fec3
|
||||
title: 'Problem 68: Magic 5-gon ring'
|
||||
challengeType: 1
|
||||
forumTopicId: 302180
|
||||
dashedName: problem-68-magic-5-gon-ring
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.
|
||||
|
||||
<img class="img-responsive center-block" alt="a completed example of a 3-gon ring" src="https://cdn-media-1.freecodecamp.org/project-euler/3-gon-ring.png" style="background-color: white; padding: 10px;" />
|
||||
|
||||
Working **clockwise**, and starting from the group of three with the numerically lowest external node (4,3,2 in this example), each solution can be described uniquely. For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3.
|
||||
|
||||
It is possible to complete the ring with four different totals: 9, 10, 11, and 12. There are eight solutions in total.
|
||||
|
||||
<div style='text-align: center;'>
|
||||
|
||||
| <div style='width: 100px;'>Total</div> | <div style='width: 250px;'>Solution Set</div> |
|
||||
| -------------------------------------- | --------------------------------------------- |
|
||||
| 9 | 4,2,3; 5,3,1; 6,1,2 |
|
||||
| 9 | 4,3,2; 6,2,1; 5,1,3 |
|
||||
| 10 | 2,3,5; 4,5,1; 6,1,3 |
|
||||
| 10 | 2,5,3; 6,3,1; 4,1,5 |
|
||||
| 11 | 1,4,6; 3,6,2; 5,2,4 |
|
||||
| 11 | 1,6,4; 5,4,2; 3,2,6 |
|
||||
| 12 | 1,5,6; 2,6,4; 3,4,5 |
|
||||
| 12 | 1,6,5; 3,5,4; 2,4,6 |
|
||||
|
||||
</div>
|
||||
|
||||
By concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513.
|
||||
|
||||
Using the numbers 1 to 10, and depending on arrangements, it is possible to form 16- and 17-digit strings. What is the maximum **16-digit** string for a "magic" 5-gon ring?
|
||||
|
||||
<img class="img-responsive center-block" alt="a blank diagram of a 5-gon ring" src="https://cdn-media-1.freecodecamp.org/project-euler/5-gon-ring.png" style="background-color: white; padding: 10px;" />
|
||||
|
||||
# --hints--
|
||||
|
||||
`magic5GonRing()` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof magic5GonRing() === 'number');
|
||||
```
|
||||
|
||||
`magic5GonRing()` should return 6531031914842725.
|
||||
|
||||
```js
|
||||
assert.strictEqual(magic5GonRing(), 6531031914842725);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function magic5GonRing() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
magic5GonRing();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
@@ -0,0 +1,110 @@
|
||||
---
|
||||
id: 5900f3b11000cf542c50fec4
|
||||
title: 'Problem 69: Totient maximum'
|
||||
challengeType: 1
|
||||
forumTopicId: 302181
|
||||
dashedName: problem-69-totient-maximum
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Euler's Totient function, ${\phi}(n)$ (sometimes called the phi function), is used to determine the number of numbers less than `n` which are relatively prime to `n`. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, ${\phi}(9) = 6$.
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
|
||||
| $n$ | $\text{Relatively Prime}$ | $\displaystyle{\phi}(n)$ | $\displaystyle\frac{n}{{\phi}(n)}$ |
|
||||
| --- | ------------------------- | ------------------------ | ---------------------------------- |
|
||||
| 2 | 1 | 1 | 2 |
|
||||
| 3 | 1,2 | 2 | 1.5 |
|
||||
| 4 | 1,3 | 2 | 2 |
|
||||
| 5 | 1,2,3,4 | 4 | 1.25 |
|
||||
| 6 | 1,5 | 2 | 3 |
|
||||
| 7 | 1,2,3,4,5,6 | 6 | 1.1666... |
|
||||
| 8 | 1,3,5,7 | 4 | 2 |
|
||||
| 9 | 1,2,4,5,7,8 | 6 | 1.5 |
|
||||
| 10 | 1,3,7,9 | 4 | 2.5 |
|
||||
|
||||
</div>
|
||||
|
||||
It can be seen that `n` = 6 produces a maximum $\displaystyle\frac{n}{{\phi}(n)}$ for `n` ≤ 10.
|
||||
|
||||
Find the value of `n` ≤ `limit` for which $\displaystyle\frac{n}{{\phi(n)}}$ is a maximum.
|
||||
|
||||
# --hints--
|
||||
|
||||
`totientMaximum(10)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof totientMaximum(10) === 'number');
|
||||
```
|
||||
|
||||
`totientMaximum(10)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(totientMaximum(10), 6);
|
||||
```
|
||||
|
||||
`totientMaximum(10000)` should return `2310`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(totientMaximum(10000), 2310);
|
||||
```
|
||||
|
||||
`totientMaximum(500000)` should return `30030`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(totientMaximum(500000), 30030);
|
||||
```
|
||||
|
||||
`totientMaximum(1000000)` should return `510510`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(totientMaximum(1000000), 510510);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function totientMaximum(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
totientMaximum(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function totientMaximum(limit) {
|
||||
function getSievePrimes(max) {
|
||||
const primesMap = new Array(max).fill(true);
|
||||
primesMap[0] = false;
|
||||
primesMap[1] = false;
|
||||
const primes = [];
|
||||
for (let i = 2; i < max; i = i + 2) {
|
||||
if (primesMap[i]) {
|
||||
primes.push(i);
|
||||
for (let j = i * i; j < max; j = j + i) {
|
||||
primesMap[j] = false;
|
||||
}
|
||||
}
|
||||
if (i === 2) {
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
const MAX_PRIME = 50;
|
||||
const primes = getSievePrimes(MAX_PRIME);
|
||||
let result = 1;
|
||||
|
||||
for (let i = 0; result * primes[i] < limit; i++) {
|
||||
result *= primes[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 5900f3731000cf542c50fe86
|
||||
title: 'Problem 7: 10001st prime'
|
||||
challengeType: 1
|
||||
forumTopicId: 302182
|
||||
dashedName: problem-7-10001st-prime
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
|
||||
|
||||
What is the `n`th prime number?
|
||||
|
||||
# --hints--
|
||||
|
||||
`nthPrime(6)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof nthPrime(6) === 'number');
|
||||
```
|
||||
|
||||
`nthPrime(6)` should return 13.
|
||||
|
||||
```js
|
||||
assert.strictEqual(nthPrime(6), 13);
|
||||
```
|
||||
|
||||
`nthPrime(10)` should return 29.
|
||||
|
||||
```js
|
||||
assert.strictEqual(nthPrime(10), 29);
|
||||
```
|
||||
|
||||
`nthPrime(100)` should return 541.
|
||||
|
||||
```js
|
||||
assert.strictEqual(nthPrime(100), 541);
|
||||
```
|
||||
|
||||
`nthPrime(1000)` should return 7919.
|
||||
|
||||
```js
|
||||
assert.strictEqual(nthPrime(1000), 7919);
|
||||
```
|
||||
|
||||
`nthPrime(10001)` should return 104743.
|
||||
|
||||
```js
|
||||
assert.strictEqual(nthPrime(10001), 104743);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function nthPrime(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
nthPrime(10001);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const nthPrime = n => {
|
||||
let pN = 2;
|
||||
let step = 0;
|
||||
while (step < n) {
|
||||
let isPrime = true;
|
||||
let rootN = Math.sqrt(pN);
|
||||
for (let i = 2; i <= rootN; i++) {
|
||||
if (!(pN % i)) {
|
||||
isPrime = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
isPrime ? step++ : '';
|
||||
pN++;
|
||||
}
|
||||
return pN - 1;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,118 @@
|
||||
---
|
||||
id: 5900f3b21000cf542c50fec5
|
||||
title: 'Problem 70: Totient permutation'
|
||||
challengeType: 1
|
||||
forumTopicId: 302183
|
||||
dashedName: problem-70-totient-permutation
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Euler's Totient function, ${\phi}(n)$ (sometimes called the phi function), is used to determine the number of positive numbers less than or equal to `n` which are relatively prime to `n`. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, ${\phi}(9) = 6$. The number 1 is considered to be relatively prime to every positive number, so ${\phi}(1) = 1$.
|
||||
|
||||
Interestingly, ${\phi}(87109) = 79180$, and it can be seen that 87109 is a permutation of 79180.
|
||||
|
||||
Find the value of `n`, 1 < `n` < `limit`, for which ${\phi}(n)$ is a permutation of `n` and the ratio $\displaystyle\frac{n}{{\phi}(n)}$ produces a minimum.
|
||||
|
||||
# --hints--
|
||||
|
||||
`totientPermutation(10000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof totientPermutation(10000) === 'number');
|
||||
```
|
||||
|
||||
`totientPermutation(10000)` should return `4435`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(totientPermutation(10000), 4435);
|
||||
```
|
||||
|
||||
`totientPermutation(100000)` should return `75841`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(totientPermutation(100000), 75841);
|
||||
```
|
||||
|
||||
`totientPermutation(500000)` should return `474883`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(totientPermutation(500000), 474883);
|
||||
```
|
||||
|
||||
`totientPermutation(10000000)` should return `8319823`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(totientPermutation(10000000), 8319823);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function totientPermutation(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
totientPermutation(10000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function totientPermutation(limit) {
|
||||
function getSievePrimes(max) {
|
||||
const primes = [];
|
||||
const primesMap = new Array(max).fill(true);
|
||||
primesMap[0] = false;
|
||||
primesMap[1] = false;
|
||||
|
||||
for (let i = 2; i < max; i += 2) {
|
||||
if (primesMap[i]) {
|
||||
primes.push(i);
|
||||
for (let j = i * i; j < max; j += i) {
|
||||
primesMap[j] = false;
|
||||
}
|
||||
}
|
||||
if (i === 2) {
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
function sortDigits(number) {
|
||||
return number.toString().split('').sort().join('');
|
||||
}
|
||||
|
||||
function isPermutation(numberA, numberB) {
|
||||
return sortDigits(numberA) === sortDigits(numberB);
|
||||
}
|
||||
|
||||
const MAX_PRIME = 4000;
|
||||
const primes = getSievePrimes(MAX_PRIME);
|
||||
|
||||
let nValue = 1;
|
||||
let minRatio = Infinity;
|
||||
|
||||
for (let i = 1; i < primes.length; i++) {
|
||||
for (let j = i + 1; j < primes.length; j++) {
|
||||
const num = primes[i] * primes[j];
|
||||
if (num > limit) {
|
||||
break;
|
||||
}
|
||||
|
||||
const phi = (primes[i] - 1) * (primes[j] - 1);
|
||||
const ratio = num / phi;
|
||||
|
||||
if (minRatio > ratio && isPermutation(num, phi)) {
|
||||
nValue = num;
|
||||
minRatio = ratio;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nValue;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,95 @@
|
||||
---
|
||||
id: 5900f3b31000cf542c50fec6
|
||||
title: 'Problem 71: Ordered fractions'
|
||||
challengeType: 1
|
||||
forumTopicId: 302184
|
||||
dashedName: problem-71-ordered-fractions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Consider the fraction, $\frac{n}{d}$, where `n` and `d` are positive integers. If `n` < `d` and highest common factor, ${{HCF}(n, d)} = 1$, it is called a reduced proper fraction.
|
||||
|
||||
If we list the set of reduced proper fractions for `d` ≤ 8 in ascending order of size, we get:
|
||||
|
||||
$$\frac{1}{8}, \frac{1}{7}, \frac{1}{6}, \frac{1}{5}, \frac{1}{4}, \frac{2}{7}, \frac{1}{3}, \frac{3}{8}, \frac{\textbf2}{\textbf5}, \frac{3}{7}, \frac{1}{2}, \frac{4}{7}, \frac{3}{5}, \frac{5}{8}, \frac{2}{3}, \frac{5}{7}, \frac{3}{4}, \frac{4}{5}, \frac{5}{6}, \frac{6}{7}, \frac{7}{8}$$
|
||||
|
||||
It can be seen that $\frac{2}{5}$ is the fraction immediately to the left of $\frac{3}{7}$.
|
||||
|
||||
By listing the set of reduced proper fractions for `d` ≤ `limit` in ascending order of size, find the numerator of the fraction immediately to the left of $\frac{3}{7}$.
|
||||
|
||||
# --hints--
|
||||
|
||||
`orderedFractions(8)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof orderedFractions(8) === 'number');
|
||||
```
|
||||
|
||||
`orderedFractions(8)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(orderedFractions(8), 2);
|
||||
```
|
||||
|
||||
`orderedFractions(10)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(orderedFractions(10), 2);
|
||||
```
|
||||
|
||||
`orderedFractions(9994)` should return `4283`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(orderedFractions(9994), 4283);
|
||||
```
|
||||
|
||||
`orderedFractions(500000)` should return `214283`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(orderedFractions(500000), 214283);
|
||||
```
|
||||
|
||||
`orderedFractions(1000000)` should return `428570`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(orderedFractions(1000000), 428570);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function orderedFractions(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
orderedFractions(8);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function orderedFractions(limit) {
|
||||
const fractions = [];
|
||||
const fractionValues = {};
|
||||
const highBoundary = 3 / 7;
|
||||
let lowBoundary = 2 / 7;
|
||||
|
||||
for (let denominator = limit; denominator > 2; denominator--) {
|
||||
let numerator = Math.floor((3 * denominator - 1) / 7);
|
||||
let value = numerator / denominator;
|
||||
if (value > highBoundary || value < lowBoundary) {
|
||||
continue;
|
||||
}
|
||||
fractionValues[value] = [numerator, denominator];
|
||||
fractions.push(value);
|
||||
lowBoundary = value;
|
||||
}
|
||||
|
||||
fractions.sort();
|
||||
return fractionValues[fractions[fractions.length - 1]][0];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,90 @@
|
||||
---
|
||||
id: 5900f3b41000cf542c50fec7
|
||||
title: 'Problem 72: Counting fractions'
|
||||
challengeType: 1
|
||||
forumTopicId: 302185
|
||||
dashedName: problem-72-counting-fractions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Consider the fraction, $\frac{n}{d}$, where `n` and `d` are positive integers. If `n` < `d` and highest common factor, ${HCF}(n, d) = 1$, it is called a reduced proper fraction.
|
||||
|
||||
If we list the set of reduced proper fractions for `d` ≤ 8 in ascending order of size, we get:
|
||||
|
||||
$$\frac{1}{8}, \frac{1}{7}, \frac{1}{6}, \frac{1}{5}, \frac{1}{4}, \frac{2}{7}, \frac{1}{3}, \frac{3}{8}, \frac{2}{5}, \frac{3}{7}, \frac{1}{2}, \frac{4}{7}, \frac{3}{5}, \frac{5}{8}, \frac{2}{3}, \frac{5}{7}, \frac{3}{4}, \frac{4}{5}, \frac{5}{6}, \frac{6}{7}, \frac{7}{8}$$
|
||||
|
||||
It can be seen that there are `21` elements in this set.
|
||||
|
||||
How many elements would be contained in the set of reduced proper fractions for `d` ≤ `limit`?
|
||||
|
||||
# --hints--
|
||||
|
||||
`countingFractions(8)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof countingFractions(8) === 'number');
|
||||
```
|
||||
|
||||
`countingFractions(8)` should return `21`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingFractions(8), 21);
|
||||
```
|
||||
|
||||
`countingFractions(20000)` should return `121590395`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingFractions(20000), 121590395);
|
||||
```
|
||||
|
||||
`countingFractions(500000)` should return `75991039675`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingFractions(500000), 75991039675);
|
||||
```
|
||||
|
||||
`countingFractions(1000000)` should return `303963552391`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingFractions(1000000), 303963552391);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function countingFractions(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
countingFractions(8);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function countingFractions(limit) {
|
||||
const phi = {};
|
||||
let count = 0;
|
||||
|
||||
for (let i = 2; i <= limit; i++) {
|
||||
if (!phi[i]) {
|
||||
phi[i] = i;
|
||||
}
|
||||
if (phi[i] === i) {
|
||||
for (let j = i; j <= limit; j += i) {
|
||||
if (!phi[j]) {
|
||||
phi[j] = j;
|
||||
}
|
||||
phi[j] = (phi[j] / i) * (i - 1);
|
||||
}
|
||||
}
|
||||
count += phi[i];
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 5900f3b61000cf542c50fec8
|
||||
title: 'Problem 73: Counting fractions in a range'
|
||||
challengeType: 1
|
||||
forumTopicId: 302186
|
||||
dashedName: problem-73-counting-fractions-in-a-range
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Consider the fraction, $\frac{n}{d}$, where `n` and `d` are positive integers. If `n` < `d` and highest common factor, ${HCF}(n, d) = 1$, it is called a reduced proper fraction.
|
||||
|
||||
If we list the set of reduced proper fractions for `d` ≤ 8 in ascending order of size, we get:
|
||||
|
||||
$$\frac{1}{8}, \frac{1}{7}, \frac{1}{6}, \frac{1}{5}, \frac{1}{4}, \frac{2}{7}, \frac{1}{3}, \mathbf{\frac{3}{8}, \frac{2}{5}, \frac{3}{7}}, \frac{1}{2}, \frac{4}{7}, \frac{3}{5}, \frac{5}{8}, \frac{2}{3}, \frac{5}{7}, \frac{3}{4}, \frac{4}{5}, \frac{5}{6}, \frac{6}{7}, \frac{7}{8}$$
|
||||
|
||||
It can be seen that there are `3` fractions between $\frac{1}{3}$ and $\frac{1}{2}$.
|
||||
|
||||
How many fractions lie between $\frac{1}{3}$ and $\frac{1}{2}$ in the sorted set of reduced proper fractions for `d` ≤ `limit`?
|
||||
|
||||
# --hints--
|
||||
|
||||
`countingFractionsInARange(8)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof countingFractionsInARange(8) === 'number');
|
||||
```
|
||||
|
||||
`countingFractionsInARange(8)` should return `3`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingFractionsInARange(8), 3);
|
||||
```
|
||||
|
||||
`countingFractionsInARange(1000)` should return `50695`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingFractionsInARange(1000), 50695);
|
||||
```
|
||||
|
||||
`countingFractionsInARange(6000)` should return `1823861`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingFractionsInARange(6000), 1823861);
|
||||
```
|
||||
|
||||
`countingFractionsInARange(12000)` should return `7295372`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingFractionsInARange(12000), 7295372);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function countingFractionsInARange(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
countingFractionsInARange(8);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function countingFractionsInARange(limit) {
|
||||
let result = 0;
|
||||
const stack = [[3, 2]];
|
||||
while (stack.length > 0) {
|
||||
const [startDenominator, endDenominator] = stack.pop();
|
||||
const curDenominator = startDenominator + endDenominator;
|
||||
if (curDenominator <= limit) {
|
||||
result++;
|
||||
stack.push([startDenominator, curDenominator]);
|
||||
stack.push([curDenominator, endDenominator]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,127 @@
|
||||
---
|
||||
id: 5900f3b61000cf542c50fec9
|
||||
title: 'Problem 74: Digit factorial chains'
|
||||
challengeType: 1
|
||||
forumTopicId: 302187
|
||||
dashedName: problem-74-digit-factorial-chains
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145:
|
||||
|
||||
$$1! + 4! + 5! = 1 + 24 + 120 = 145$$
|
||||
|
||||
Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it turns out that there are only three such loops that exist:
|
||||
|
||||
$$\begin{align} &169 → 363601 → 1454 → 169\\\\
|
||||
&871 → 45361 → 871\\\\ &872 → 45362 → 872\\\\
|
||||
\end{align}$$
|
||||
|
||||
It is not difficult to prove that EVERY starting number will eventually get stuck in a loop. For example,
|
||||
|
||||
$$\begin{align} &69 → 363600 → 1454 → 169 → 363601\\ (→ 1454)\\\\
|
||||
&78 → 45360 → 871 → 45361\\ (→ 871)\\\\ &540 → 145\\ (→ 145)\\\\
|
||||
\end{align}$$
|
||||
|
||||
Starting with 69 produces a chain of five non-repeating terms, but the longest non-repeating chain with a starting number below one million is sixty terms.
|
||||
|
||||
How many chains, with a starting number below `n`, contain exactly sixty non-repeating terms?
|
||||
|
||||
# --hints--
|
||||
|
||||
`digitFactorialChains(2000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof digitFactorialChains(2000) === 'number');
|
||||
```
|
||||
|
||||
`digitFactorialChains(2000)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitFactorialChains(2000), 6);
|
||||
```
|
||||
|
||||
`digitFactorialChains(100000)` should return `42`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitFactorialChains(100000), 42);
|
||||
```
|
||||
|
||||
`digitFactorialChains(500000)` should return `282`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitFactorialChains(500000), 282);
|
||||
```
|
||||
|
||||
`digitFactorialChains(1000000)` should return `402`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(digitFactorialChains(1000000), 402);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function digitFactorialChains(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
digitFactorialChains(2000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function digitFactorialChains(n) {
|
||||
function sumDigitsFactorials(number) {
|
||||
let sum = 0;
|
||||
while (number > 0) {
|
||||
sum += factorials[number % 10];
|
||||
number = Math.floor(number / 10);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
const factorials = [1];
|
||||
for (let i = 1; i < 10; i++) {
|
||||
factorials.push(factorials[factorials.length - 1] * i);
|
||||
}
|
||||
|
||||
const sequences = {
|
||||
169: 3,
|
||||
871: 2,
|
||||
872: 2,
|
||||
1454: 3,
|
||||
45362: 2,
|
||||
45461: 2,
|
||||
3693601: 3
|
||||
};
|
||||
let result = 0;
|
||||
|
||||
for (let i = 2; i < n; i++) {
|
||||
let curNum = i;
|
||||
let chainLength = 0;
|
||||
const curSequence = [];
|
||||
while (curSequence.indexOf(curNum) === -1) {
|
||||
curSequence.push(curNum);
|
||||
curNum = sumDigitsFactorials(curNum);
|
||||
chainLength++;
|
||||
if (sequences.hasOwnProperty(curNum) > 0) {
|
||||
chainLength += sequences[curNum];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (chainLength === 60) {
|
||||
result++;
|
||||
}
|
||||
for (let j = 1; j < curSequence.length; j++) {
|
||||
sequences[curSequence[j]] = chainLength - j;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,117 @@
|
||||
---
|
||||
id: 5900f3b71000cf542c50feca
|
||||
title: 'Problem 75: Singular integer right triangles'
|
||||
challengeType: 1
|
||||
forumTopicId: 302188
|
||||
dashedName: problem-75-singular-integer-right-triangles
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
<strong>12 cm:</strong> (3,4,5)<br>
|
||||
<strong>24 cm:</strong> (6,8,10)<br>
|
||||
<strong>30 cm:</strong> (5,12,13)<br>
|
||||
<strong>36 cm:</strong> (9,12,15)<br>
|
||||
<strong>40 cm:</strong> (8,15,17)<br>
|
||||
<strong>48 cm:</strong> (12,16,20)<br><br>
|
||||
</div>
|
||||
|
||||
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
<strong>120 cm:</strong> (30,40,50), (20,48,52), (24,45,51)<br><br>
|
||||
</div>
|
||||
|
||||
Given that L is the length of the wire, for how many values of L ≤ `n` can exactly one, integer sided right angle, triangle be formed?
|
||||
|
||||
# --hints--
|
||||
|
||||
`singularIntRightTriangles(48)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof singularIntRightTriangles(48) === 'number');
|
||||
```
|
||||
|
||||
`singularIntRightTriangles(48)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(singularIntRightTriangles(48), 6);
|
||||
```
|
||||
|
||||
`singularIntRightTriangles(700000)` should return `75783`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(singularIntRightTriangles(700000), 75783);
|
||||
```
|
||||
|
||||
`singularIntRightTriangles(1000000)` should return `107876`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(singularIntRightTriangles(1000000), 107876);
|
||||
```
|
||||
|
||||
`singularIntRightTriangles(1500000)` should return `161667`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(singularIntRightTriangles(1500000), 161667);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function singularIntRightTriangles(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
singularIntRightTriangles(48);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function singularIntRightTriangles(limit) {
|
||||
function euclidFormula(m, n) {
|
||||
return [m ** 2 - n ** 2, 2 * m * n, m ** 2 + n ** 2];
|
||||
}
|
||||
|
||||
function gcd(numberA, numberB) {
|
||||
if (numberB === 0) {
|
||||
return numberA;
|
||||
}
|
||||
return gcd(numberB, numberA % numberB);
|
||||
}
|
||||
|
||||
function notBothOdd(numberA, numberB) {
|
||||
return (numberA + numberB) % 2 === 1;
|
||||
}
|
||||
|
||||
function areCoprime(numberA, numberB) {
|
||||
return gcd(numberA, numberB) === 1;
|
||||
}
|
||||
|
||||
const trianglesWithPerimeter = new Array(limit + 1).fill(0);
|
||||
const mLimit = Math.sqrt(limit / 2);
|
||||
|
||||
for (let m = 2; m < mLimit; m++) {
|
||||
for (let n = 1; n < m; n++) {
|
||||
if (notBothOdd(m, n) && areCoprime(m, n)) {
|
||||
const [sideA, sideB, sideC] = euclidFormula(m, n);
|
||||
const perimeter = sideA + sideB + sideC;
|
||||
let curPerimeter = perimeter;
|
||||
while (curPerimeter <= limit) {
|
||||
trianglesWithPerimeter[curPerimeter]++;
|
||||
curPerimeter += perimeter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return trianglesWithPerimeter.filter(trianglesCount => trianglesCount === 1)
|
||||
.length;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 5900f3b81000cf542c50fecb
|
||||
title: 'Problem 76: Counting summations'
|
||||
challengeType: 1
|
||||
forumTopicId: 302189
|
||||
dashedName: problem-76-counting-summations
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
It is possible to write five as a sum in exactly six different ways:
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
4 + 1<br>
|
||||
3 + 2<br>
|
||||
3 + 1 + 1<br>
|
||||
2 + 2 + 1<br>
|
||||
2 + 1 + 1 + 1<br>
|
||||
1 + 1 + 1 + 1 + 1<br><br>
|
||||
</div>
|
||||
|
||||
How many different ways can `n` be written as a sum of at least two positive integers?
|
||||
|
||||
# --hints--
|
||||
|
||||
`countingSummations(5)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof countingSummations(5) === 'number');
|
||||
```
|
||||
|
||||
`countingSummations(5)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingSummations(5), 6);
|
||||
```
|
||||
|
||||
`countingSummations(20)` should return `626`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingSummations(20), 626);
|
||||
```
|
||||
|
||||
`countingSummations(50)` should return `204225`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingSummations(50), 204225);
|
||||
```
|
||||
|
||||
`countingSummations(100)` should return `190569291`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingSummations(100), 190569291);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function countingSummations(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
countingSummations(5);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function countingSummations(n) {
|
||||
const combinations = new Array(n + 1).fill(0);
|
||||
combinations[0] = 1;
|
||||
|
||||
for (let i = 1; i < n; i++) {
|
||||
for (let j = i; j < n + 1; j++) {
|
||||
combinations[j] += combinations[j - i];
|
||||
}
|
||||
}
|
||||
return combinations[n];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,110 @@
|
||||
---
|
||||
id: 5900f3b91000cf542c50fecc
|
||||
title: 'Problem 77: Prime summations'
|
||||
challengeType: 1
|
||||
forumTopicId: 302190
|
||||
dashedName: problem-77-prime-summations
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
It is possible to write ten as the sum of primes in exactly five different ways:
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
7 + 3<br>
|
||||
5 + 5<br>
|
||||
5 + 3 + 2<br>
|
||||
3 + 3 + 2 + 2<br>
|
||||
2 + 2 + 2 + 2 + 2<br><br>
|
||||
</div>
|
||||
|
||||
What is the first value which can be written as the sum of primes in over `n` ways?
|
||||
|
||||
# --hints--
|
||||
|
||||
`primeSummations(5)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof primeSummations(5) === 'number');
|
||||
```
|
||||
|
||||
`primeSummations(5)` should return `11`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeSummations(5), 11);
|
||||
```
|
||||
|
||||
`primeSummations(100)` should return `31`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeSummations(100), 31);
|
||||
```
|
||||
|
||||
`primeSummations(1000)` should return `53`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeSummations(1000), 53);
|
||||
```
|
||||
|
||||
`primeSummations(5000)` should return `71`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primeSummations(5000), 71);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function primeSummations(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
primeSummations(5);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function primeSummations(n) {
|
||||
function getSievePrimes(max) {
|
||||
const primesMap = new Array(max).fill(true);
|
||||
primesMap[0] = false;
|
||||
primesMap[1] = false;
|
||||
const primes = [];
|
||||
|
||||
for (let i = 2; i < max; i += 2) {
|
||||
if (primesMap[i]) {
|
||||
primes.push(i);
|
||||
for (let j = i * i; j < max; j += i) {
|
||||
primesMap[j] = false;
|
||||
}
|
||||
}
|
||||
if (i === 2) {
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
const MAX_NUMBER = 100;
|
||||
const primes = getSievePrimes(MAX_NUMBER);
|
||||
|
||||
for (let curNumber = 2; curNumber < MAX_NUMBER; curNumber++) {
|
||||
const combinations = new Array(curNumber + 1).fill(0);
|
||||
combinations[0] = 1;
|
||||
for (let i = 0; i < primes.length; i++) {
|
||||
for (let j = primes[i]; j <= curNumber; j++) {
|
||||
combinations[j] += combinations[j - primes[i]];
|
||||
}
|
||||
}
|
||||
if (combinations[curNumber] > n) {
|
||||
return curNumber;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 5900f3ba1000cf542c50fecd
|
||||
title: 'Problem 78: Coin partitions'
|
||||
challengeType: 1
|
||||
forumTopicId: 302191
|
||||
dashedName: problem-78-coin-partitions
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Let ${p}(n)$ represent the number of different ways in which `n` coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so ${p}(5) = 7$.
|
||||
|
||||
<div style='text-align: center;'>
|
||||
|
||||
| Coin piles |
|
||||
| ----------------- |
|
||||
| OOOOO |
|
||||
| OOOO O |
|
||||
| OOO OO |
|
||||
| OOO O O |
|
||||
| OO OO O |
|
||||
| OO O O O |
|
||||
| O O O O O |
|
||||
|
||||
</div><br>
|
||||
|
||||
Find the least value of `n` for which ${p}(n)$ is divisible by `divisor`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`coinPartitions(7)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof coinPartitions(7) === 'number');
|
||||
```
|
||||
|
||||
`coinPartitions(7)` should return `5`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(coinPartitions(7), 5);
|
||||
```
|
||||
|
||||
`coinPartitions(10000)` should return `599`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(coinPartitions(10000), 599);
|
||||
```
|
||||
|
||||
`coinPartitions(100000)` should return `11224`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(coinPartitions(100000), 11224);
|
||||
```
|
||||
|
||||
`coinPartitions(1000000)` should return `55374`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(coinPartitions(1000000), 55374);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function coinPartitions(divisor) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
coinPartitions(7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function coinPartitions(divisor) {
|
||||
const partitions = [1];
|
||||
|
||||
let n = 0;
|
||||
while (partitions[n] !== 0) {
|
||||
n++;
|
||||
partitions.push(0);
|
||||
|
||||
let i = 0;
|
||||
let pentagonal = 1;
|
||||
while (pentagonal <= n) {
|
||||
const sign = i % 4 > 1 ? -1 : 1;
|
||||
partitions[n] += sign * partitions[n - pentagonal];
|
||||
partitions[n] = partitions[n] % divisor;
|
||||
|
||||
i++;
|
||||
|
||||
let k = Math.floor(i / 2) + 1;
|
||||
if (i % 2 !== 0) {
|
||||
k *= -1;
|
||||
}
|
||||
pentagonal = Math.floor((k * (3 * k - 1)) / 2);
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,115 @@
|
||||
---
|
||||
id: 5900f3bb1000cf542c50fece
|
||||
title: 'Problem 79: Passcode derivation'
|
||||
challengeType: 1
|
||||
forumTopicId: 302192
|
||||
dashedName: problem-79-passcode-derivation
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was `531278`, they may ask for the 2nd, 3rd, and 5th characters; the expected reply would be: `317`.
|
||||
|
||||
The arrays, `keylog1`, `keylog2`, and `keylog3`, contains fifty successful login attempts.
|
||||
|
||||
Given that the three characters are always asked for in order, analyze the array so as to determine the shortest possible secret passcode of unknown length.
|
||||
|
||||
# --hints--
|
||||
|
||||
`passcodeDerivation(keylog1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof passcodeDerivation(_keylog1) === 'number');
|
||||
```
|
||||
|
||||
`passcodeDerivation(keylog1)` should return `531278`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(passcodeDerivation(_keylog1), 531278);
|
||||
```
|
||||
|
||||
`passcodeDerivation(keylog2)` should return `1230567`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(passcodeDerivation(_keylog2), 1230567);
|
||||
```
|
||||
|
||||
`passcodeDerivation(keylog3)` should return `73162890`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(passcodeDerivation(_keylog3), 73162890);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
const _keylog1 = [
|
||||
127,327,178,528,537,538,532,328,127,178,537,127,317,328,512,278,328,327,178,327,578,317,527,178,128,328,517,312,531,128,128,317,527,312,328,532,512,518,317,127,537,528,537,527,327,278,532,128, 318,517
|
||||
];
|
||||
const _keylog2 = [
|
||||
305,367,256,123,357,120,125,307,236,256,356,267,357,256,356,136,257,107,126,567,567,105,120,237,367,120,367,135,207,167,367,367,307,125,120,130,367,230,106,356,126,106,130,123,307,127,306,167,136,126
|
||||
];
|
||||
const _keylog3 = [
|
||||
319,680,180,690,129,620,762,689,762,318,368,710,720,710,629,168,160,689,716,731,736,729,316,729,729,710,769,290,719,680,318,389,162,289,162,718,729,319,790,680,890,362,319,760,316,729,380,319,728,716,
|
||||
];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function passcodeDerivation(arr) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
const keylog1 = [
|
||||
319,680,180,690,129,620,762,689,762,318,368,710,720,710,629,168,160,689,716,731,736,729,316,729,729,710,769,290,719,680,318,389,162,289,162,718,729,319,790,680,890,362,319,760,316,729,380,319,728,716,
|
||||
];
|
||||
|
||||
passcodeDerivation(keylog1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function passcodeDerivation(arr) {
|
||||
const numbersInPasscode = [];
|
||||
const relativePositions = new Array(10)
|
||||
.fill()
|
||||
.map(() => new Array(10).fill(0));
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
const curAttempt = arr[i]
|
||||
.toString()
|
||||
.split('')
|
||||
.map(key => parseInt(key, 10));
|
||||
for (let j = 0; j < curAttempt.length; j++) {
|
||||
if (numbersInPasscode.indexOf(curAttempt[j]) === -1) {
|
||||
numbersInPasscode.push(curAttempt[j]);
|
||||
}
|
||||
for (let k = j + 1; k < curAttempt.length; k++) {
|
||||
relativePositions[curAttempt[j]][curAttempt[k]] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ranks = {};
|
||||
for (let i = 0; i < numbersInPasscode.length; i++) {
|
||||
const curNumber = numbersInPasscode[i];
|
||||
ranks[curNumber] = relativePositions[curNumber].filter(
|
||||
count => count > 0
|
||||
).length;
|
||||
}
|
||||
|
||||
const passcode = numbersInPasscode
|
||||
.sort((i, j) => ranks[i] - ranks[j])
|
||||
.reverse()
|
||||
.join('');
|
||||
|
||||
return parseInt(passcode, 10);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,92 @@
|
||||
---
|
||||
id: 5900f3741000cf542c50fe87
|
||||
title: 'Problem 8: Largest product in a series'
|
||||
challengeType: 1
|
||||
forumTopicId: 302193
|
||||
dashedName: problem-8-largest-product-in-a-series
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
|
||||
|
||||
<div style='text-align: center;'>73167176531330624919225119674426574742355349194934</div>
|
||||
<div style='text-align: center;'>96983520312774506326239578318016984801869478851843</div>
|
||||
<div style='text-align: center;'>85861560789112949495459501737958331952853208805511</div>
|
||||
<div style='text-align: center;'>12540698747158523863050715693290963295227443043557</div>
|
||||
<div style='text-align: center;'>66896648950445244523161731856403098711121722383113</div>
|
||||
<div style='text-align: center;'>62229893423380308135336276614282806444486645238749</div>
|
||||
<div style='text-align: center;'>30358907296290491560440772390713810515859307960866</div>
|
||||
<div style='text-align: center;'>70172427121883998797908792274921901699720888093776</div>
|
||||
<div style='text-align: center;'>65727333001053367881220235421809751254540594752243</div>
|
||||
<div style='text-align: center;'>52584907711670556013604839586446706324415722155397</div>
|
||||
<div style='text-align: center;'>53697817977846174064955149290862569321978468622482</div>
|
||||
<div style='text-align: center;'>83972241375657056057490261407972968652414535100474</div>
|
||||
<div style='text-align: center;'>82166370484403199890008895243450658541227588666881</div>
|
||||
<div style='text-align: center;'>16427171479924442928230863465674813919123162824586</div>
|
||||
<div style='text-align: center;'>17866458359124566529476545682848912883142607690042</div>
|
||||
<div style='text-align: center;'>24219022671055626321111109370544217506941658960408</div>
|
||||
<div style='text-align: center;'>07198403850962455444362981230987879927244284909188</div>
|
||||
<div style='text-align: center;'>84580156166097919133875499200524063689912560717606</div>
|
||||
<div style='text-align: center;'>05886116467109405077541002256983155200055935729725</div>
|
||||
<div style='text-align: center;'>71636269561882670428252483600823257530420752963450</div>
|
||||
|
||||
Find the `n` adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
||||
|
||||
# --hints--
|
||||
|
||||
`largestProductinaSeries(4)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof largestProductinaSeries(4) === 'number');
|
||||
```
|
||||
|
||||
`largestProductinaSeries(4)` should return 5832.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestProductinaSeries(4), 5832);
|
||||
```
|
||||
|
||||
`largestProductinaSeries(13)` should return 23514624000.
|
||||
|
||||
```js
|
||||
assert.strictEqual(largestProductinaSeries(13), 23514624000);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function largestProductinaSeries(n) {
|
||||
|
||||
let thousandDigits = [7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4,9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3,8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1,1,2,5,4,0,6,9,8,7,4,7,1,5,8,5,2,3,8,6,3,0,5,0,7,1,5,6,9,3,2,9,0,9,6,3,2,9,5,2,2,7,4,4,3,0,4,3,5,5,7,6,6,8,9,6,6,4,8,9,5,0,4,4,5,2,4,4,5,2,3,1,6,1,7,3,1,8,5,6,4,0,3,0,9,8,7,1,1,1,2,1,7,2,2,3,8,3,1,1,3,6,2,2,2,9,8,9,3,4,2,3,3,8,0,3,0,8,1,3,5,3,3,6,2,7,6,6,1,4,2,8,2,8,0,6,4,4,4,4,8,6,6,4,5,2,3,8,7,4,9,3,0,3,5,8,9,0,7,2,9,6,2,9,0,4,9,1,5,6,0,4,4,0,7,7,2,3,9,0,7,1,3,8,1,0,5,1,5,8,5,9,3,0,7,9,6,0,8,6,6,7,0,1,7,2,4,2,7,1,2,1,8,8,3,9,9,8,7,9,7,9,0,8,7,9,2,2,7,4,9,2,1,9,0,1,6,9,9,7,2,0,8,8,8,0,9,3,7,7,6,6,5,7,2,7,3,3,3,0,0,1,0,5,3,3,6,7,8,8,1,2,2,0,2,3,5,4,2,1,8,0,9,7,5,1,2,5,4,5,4,0,5,9,4,7,5,2,2,4,3,5,2,5,8,4,9,0,7,7,1,1,6,7,0,5,5,6,0,1,3,6,0,4,8,3,9,5,8,6,4,4,6,7,0,6,3,2,4,4,1,5,7,2,2,1,5,5,3,9,7,5,3,6,9,7,8,1,7,9,7,7,8,4,6,1,7,4,0,6,4,9,5,5,1,4,9,2,9,0,8,6,2,5,6,9,3,2,1,9,7,8,4,6,8,6,2,2,4,8,2,8,3,9,7,2,2,4,1,3,7,5,6,5,7,0,5,6,0,5,7,4,9,0,2,6,1,4,0,7,9,7,2,9,6,8,6,5,2,4,1,4,5,3,5,1,0,0,4,7,4,8,2,1,6,6,3,7,0,4,8,4,4,0,3,1,9,9,8,9,0,0,0,8,8,9,5,2,4,3,4,5,0,6,5,8,5,4,1,2,2,7,5,8,8,6,6,6,8,8,1,1,6,4,2,7,1,7,1,4,7,9,9,2,4,4,4,2,9,2,8,2,3,0,8,6,3,4,6,5,6,7,4,8,1,3,9,1,9,1,2,3,1,6,2,8,2,4,5,8,6,1,7,8,6,6,4,5,8,3,5,9,1,2,4,5,6,6,5,2,9,4,7,6,5,4,5,6,8,2,8,4,8,9,1,2,8,8,3,1,4,2,6,0,7,6,9,0,0,4,2,2,4,2,1,9,0,2,2,6,7,1,0,5,5,6,2,6,3,2,1,1,1,1,1,0,9,3,7,0,5,4,4,2,1,7,5,0,6,9,4,1,6,5,8,9,6,0,4,0,8,0,7,1,9,8,4,0,3,8,5,0,9,6,2,4,5,5,4,4,4,3,6,2,9,8,1,2,3,0,9,8,7,8,7,9,9,2,7,2,4,4,2,8,4,9,0,9,1,8,8,8,4,5,8,0,1,5,6,1,6,6,0,9,7,9,1,9,1,3,3,8,7,5,4,9,9,2,0,0,5,2,4,0,6,3,6,8,9,9,1,2,5,6,0,7,1,7,6,0,6,0,5,8,8,6,1,1,6,4,6,7,1,0,9,4,0,5,0,7,7,5,4,1,0,0,2,2,5,6,9,8,3,1,5,5,2,0,0,0,5,5,9,3,5,7,2,9,7,2,5,7,1,6,3,6,2,6,9,5,6,1,8,8,2,6,7,0,4,2,8,2,5,2,4,8,3,6,0,0,8,2,3,2,5,7,5,3,0,4,2,0,7,5,2,9,6,3,4,5,0];
|
||||
return true;
|
||||
}
|
||||
|
||||
largestProductinaSeries(13);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const largestProductinaSeries = (number)=>{
|
||||
let thousandDigits = [7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4,9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3,8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1,1,2,5,4,0,6,9,8,7,4,7,1,5,8,5,2,3,8,6,3,0,5,0,7,1,5,6,9,3,2,9,0,9,6,3,2,9,5,2,2,7,4,4,3,0,4,3,5,5,7,6,6,8,9,6,6,4,8,9,5,0,4,4,5,2,4,4,5,2,3,1,6,1,7,3,1,8,5,6,4,0,3,0,9,8,7,1,1,1,2,1,7,2,2,3,8,3,1,1,3,6,2,2,2,9,8,9,3,4,2,3,3,8,0,3,0,8,1,3,5,3,3,6,2,7,6,6,1,4,2,8,2,8,0,6,4,4,4,4,8,6,6,4,5,2,3,8,7,4,9,3,0,3,5,8,9,0,7,2,9,6,2,9,0,4,9,1,5,6,0,4,4,0,7,7,2,3,9,0,7,1,3,8,1,0,5,1,5,8,5,9,3,0,7,9,6,0,8,6,6,7,0,1,7,2,4,2,7,1,2,1,8,8,3,9,9,8,7,9,7,9,0,8,7,9,2,2,7,4,9,2,1,9,0,1,6,9,9,7,2,0,8,8,8,0,9,3,7,7,6,6,5,7,2,7,3,3,3,0,0,1,0,5,3,3,6,7,8,8,1,2,2,0,2,3,5,4,2,1,8,0,9,7,5,1,2,5,4,5,4,0,5,9,4,7,5,2,2,4,3,5,2,5,8,4,9,0,7,7,1,1,6,7,0,5,5,6,0,1,3,6,0,4,8,3,9,5,8,6,4,4,6,7,0,6,3,2,4,4,1,5,7,2,2,1,5,5,3,9,7,5,3,6,9,7,8,1,7,9,7,7,8,4,6,1,7,4,0,6,4,9,5,5,1,4,9,2,9,0,8,6,2,5,6,9,3,2,1,9,7,8,4,6,8,6,2,2,4,8,2,8,3,9,7,2,2,4,1,3,7,5,6,5,7,0,5,6,0,5,7,4,9,0,2,6,1,4,0,7,9,7,2,9,6,8,6,5,2,4,1,4,5,3,5,1,0,0,4,7,4,8,2,1,6,6,3,7,0,4,8,4,4,0,3,1,9,9,8,9,0,0,0,8,8,9,5,2,4,3,4,5,0,6,5,8,5,4,1,2,2,7,5,8,8,6,6,6,8,8,1,1,6,4,2,7,1,7,1,4,7,9,9,2,4,4,4,2,9,2,8,2,3,0,8,6,3,4,6,5,6,7,4,8,1,3,9,1,9,1,2,3,1,6,2,8,2,4,5,8,6,1,7,8,6,6,4,5,8,3,5,9,1,2,4,5,6,6,5,2,9,4,7,6,5,4,5,6,8,2,8,4,8,9,1,2,8,8,3,1,4,2,6,0,7,6,9,0,0,4,2,2,4,2,1,9,0,2,2,6,7,1,0,5,5,6,2,6,3,2,1,1,1,1,1,0,9,3,7,0,5,4,4,2,1,7,5,0,6,9,4,1,6,5,8,9,6,0,4,0,8,0,7,1,9,8,4,0,3,8,5,0,9,6,2,4,5,5,4,4,4,3,6,2,9,8,1,2,3,0,9,8,7,8,7,9,9,2,7,2,4,4,2,8,4,9,0,9,1,8,8,8,4,5,8,0,1,5,6,1,6,6,0,9,7,9,1,9,1,3,3,8,7,5,4,9,9,2,0,0,5,2,4,0,6,3,6,8,9,9,1,2,5,6,0,7,1,7,6,0,6,0,5,8,8,6,1,1,6,4,6,7,1,0,9,4,0,5,0,7,7,5,4,1,0,0,2,2,5,6,9,8,3,1,5,5,2,0,0,0,5,5,9,3,5,7,2,9,7,2,5,7,1,6,3,6,2,6,9,5,6,1,8,8,2,6,7,0,4,2,8,2,5,2,4,8,3,6,0,0,8,2,3,2,5,7,5,3,0,4,2,0,7,5,2,9,6,3,4,5,0];
|
||||
let numberOfDigits = thousandDigits.length;
|
||||
let currentIndex = 0;
|
||||
let productOfAdjDigits = [];
|
||||
|
||||
while(currentIndex<=(numberOfDigits-number)){
|
||||
let currentAdj = thousandDigits.slice(currentIndex,currentIndex+number);
|
||||
let isAdjDigits = false;
|
||||
|
||||
productOfAdjDigits.push(currentAdj.reduce((prev,cur)=>{
|
||||
return prev*cur;
|
||||
}));
|
||||
|
||||
currentIndex++;
|
||||
}
|
||||
|
||||
return Math.max(...productOfAdjDigits);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 5900f3bc1000cf542c50fecf
|
||||
title: 'Problem 80: Square root digital expansion'
|
||||
challengeType: 1
|
||||
forumTopicId: 302194
|
||||
dashedName: problem-80-square-root-digital-expansion
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.
|
||||
|
||||
The square root of two is `1.41421356237309504880...`, and the digital sum of the first one hundred decimal digits is `475`.
|
||||
|
||||
For the first `n` natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sqrtDigitalExpansion(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof sqrtDigitalExpansion(2) === 'number');
|
||||
```
|
||||
|
||||
`sqrtDigitalExpansion(2)` should return `475`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sqrtDigitalExpansion(2), 475);
|
||||
```
|
||||
|
||||
`sqrtDigitalExpansion(50)` should return `19543`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sqrtDigitalExpansion(50), 19543);
|
||||
```
|
||||
|
||||
`sqrtDigitalExpansion(100)` should return `40886`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sqrtDigitalExpansion(100), 40886);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sqrtDigitalExpansion(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
sqrtDigitalExpansion(2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sqrtDigitalExpansion(n) {
|
||||
function sumDigits(number) {
|
||||
let sum = 0;
|
||||
while (number > 0n) {
|
||||
let digit = number % 10n;
|
||||
sum += parseInt(digit, 10);
|
||||
number = number / 10n;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
function power(numberA, numberB) {
|
||||
let result = 1n;
|
||||
for (let b = 0; b < numberB; b++) {
|
||||
result = result * BigInt(numberA);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Based on http://www.afjarvis.staff.shef.ac.uk/maths/jarvisspec02.pdf
|
||||
function expandSquareRoot(number, numDigits) {
|
||||
let a = 5n * BigInt(number);
|
||||
let b = 5n;
|
||||
const boundaryWithNeededDigits = power(10, numDigits + 1);
|
||||
|
||||
while (b < boundaryWithNeededDigits) {
|
||||
if (a >= b) {
|
||||
a = a - b;
|
||||
b = b + 10n;
|
||||
} else {
|
||||
a = a * 100n;
|
||||
b = (b / 10n) * 100n + 5n;
|
||||
}
|
||||
}
|
||||
return b / 100n;
|
||||
}
|
||||
|
||||
let result = 0;
|
||||
let nextPerfectRoot = 1;
|
||||
const requiredDigits = 100;
|
||||
for (let i = 1; i <= n; i++) {
|
||||
if (nextPerfectRoot ** 2 === i) {
|
||||
nextPerfectRoot++;
|
||||
continue;
|
||||
}
|
||||
result += sumDigits(expandSquareRoot(i, requiredDigits));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,185 @@
|
||||
---
|
||||
id: 5900f3bd1000cf542c50fed0
|
||||
title: 'Problem 81: Path sum: two ways'
|
||||
challengeType: 1
|
||||
forumTopicId: 302195
|
||||
dashedName: problem-81-path-sum-two-ways
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by **only moving to the right and down**, is indicated in bold red and is equal to `2427`.
|
||||
|
||||
$$\begin{pmatrix} \color{red}{131} & 673 & 234 & 103 & 18\\\\
|
||||
\color{red}{201} & \color{red}{96} & \color{red}{342} & 965 & 150\\\\ 630 & 803 & \color{red}{746} & \color{red}{422} & 111\\\\
|
||||
537 & 699 & 497 & \color{red}{121} & 956\\\\ 805 & 732 & 524 & \color{red}{37} & \color{red}{331} \end{pmatrix}$$
|
||||
|
||||
Find the minimal path sum from the top left to the bottom right by only moving right and down in `matrix`, a 2D array representing a matrix. The maximum matrix size used in the tests will be 80 by 80.
|
||||
|
||||
# --hints--
|
||||
|
||||
`pathSumTwoWays(testMatrix1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof pathSumTwoWays(_testMatrix1) === 'number');
|
||||
```
|
||||
|
||||
`pathSumTwoWays(testMatrix1)` should return `2427`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pathSumTwoWays(_testMatrix1), 2427);
|
||||
```
|
||||
|
||||
`pathSumTwoWays(testMatrix2)` should return `427337`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pathSumTwoWays(_testMatrix2), 427337);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
const _testMatrix1 = [
|
||||
[131, 673, 234, 103, 18],
|
||||
[201, 96, 342, 965, 150],
|
||||
[630, 803, 746, 422, 111],
|
||||
[537, 699, 497, 121, 956],
|
||||
[805, 732, 524, 37, 331]
|
||||
];
|
||||
const _testMatrix2 = [
|
||||
[4445,2697,5115,718,2209,2212,654,4348,3079,6821,7668,3276,8874,4190,3785,2752,9473,7817,9137,496,7338,3434,7152,4355,4552,7917,7827,2460,2350,691,3514,5880,3145,7633,7199,3783,5066,7487,3285,1084,8985,760,872,8609,8051,1134,9536,5750,9716,9371,7619,5617,275,9721,2997,2698,1887,8825,6372,3014,2113,7122,7050,6775,5948,2758,1219,3539,348,7989,2735,9862,1263,8089,6401,9462,3168,2758,3748,5870],
|
||||
[1096,20,1318,7586,5167,2642,1443,5741,7621,7030,5526,4244,2348,4641,9827,2448,6918,5883,3737,300,7116,6531,567,5997,3971,6623,820,6148,3287,1874,7981,8424,7672,7575,6797,6717,1078,5008,4051,8795,5820,346,1851,6463,2117,6058,3407,8211,117,4822,1317,4377,4434,5925,8341,4800,1175,4173,690,8978,7470,1295,3799,8724,3509,9849,618,3320,7068,9633,2384,7175,544,6583,1908,9983,481,4187,9353,9377],
|
||||
[9607,7385,521,6084,1364,8983,7623,1585,6935,8551,2574,8267,4781,3834,2764,2084,2669,4656,9343,7709,2203,9328,8004,6192,5856,3555,2260,5118,6504,1839,9227,1259,9451,1388,7909,5733,6968,8519,9973,1663,5315,7571,3035,4325,4283,2304,6438,3815,9213,9806,9536,196,5542,6907,2475,1159,5820,9075,9470,2179,9248,1828,4592,9167,3713,4640,47,3637,309,7344,6955,346,378,9044,8635,7466,5036,9515,6385,9230],
|
||||
[7206,3114,7760,1094,6150,5182,7358,7387,4497,955,101,1478,7777,6966,7010,8417,6453,4955,3496,107,449,8271,131,2948,6185,784,5937,8001,6104,8282,4165,3642,710,2390,575,715,3089,6964,4217,192,5949,7006,715,3328,1152,66,8044,4319,1735,146,4818,5456,6451,4113,1063,4781,6799,602,1504,6245,6550,1417,1343,2363,3785,5448,4545,9371,5420,5068,4613,4882,4241,5043,7873,8042,8434,3939,9256,2187],
|
||||
[3620,8024,577,9997,7377,7682,1314,1158,6282,6310,1896,2509,5436,1732,9480,706,496,101,6232,7375,2207,2306,110,6772,3433,2878,8140,5933,8688,1399,2210,7332,6172,6403,7333,4044,2291,1790,2446,7390,8698,5723,3678,7104,1825,2040,140,3982,4905,4160,2200,5041,2512,1488,2268,1175,7588,8321,8078,7312,977,5257,8465,5068,3453,3096,1651,7906,253,9250,6021,8791,8109,6651,3412,345,4778,5152,4883,7505],
|
||||
[1074,5438,9008,2679,5397,5429,2652,3403,770,9188,4248,2493,4361,8327,9587,707,9525,5913,93,1899,328,2876,3604,673,8576,6908,7659,2544,3359,3883,5273,6587,3065,1749,3223,604,9925,6941,2823,8767,7039,3290,3214,1787,7904,3421,7137,9560,8451,2669,9219,6332,1576,5477,6755,8348,4164,4307,2984,4012,6629,1044,2874,6541,4942,903,1404,9125,5160,8836,4345,2581,460,8438,1538,5507,668,3352,2678,6942],
|
||||
[4295,1176,5596,1521,3061,9868,7037,7129,8933,6659,5947,5063,3653,9447,9245,2679,767,714,116,8558,163,3927,8779,158,5093,2447,5782,3967,1716,931,7772,8164,1117,9244,5783,7776,3846,8862,6014,2330,6947,1777,3112,6008,3491,1906,5952,314,4602,8994,5919,9214,3995,5026,7688,6809,5003,3128,2509,7477,110,8971,3982,8539,2980,4689,6343,5411,2992,5270,5247,9260,2269,7474,1042,7162,5206,1232,4556,4757],
|
||||
[510,3556,5377,1406,5721,4946,2635,7847,4251,8293,8281,6351,4912,287,2870,3380,3948,5322,3840,4738,9563,1906,6298,3234,8959,1562,6297,8835,7861,239,6618,1322,2553,2213,5053,5446,4402,6500,5182,8585,6900,5756,9661,903,5186,7687,5998,7997,8081,8955,4835,6069,2621,1581,732,9564,1082,1853,5442,1342,520,1737,3703,5321,4793,2776,1508,1647,9101,2499,6891,4336,7012,3329,3212,1442,9993,3988,4930,7706],
|
||||
[9444,3401,5891,9716,1228,7107,109,3563,2700,6161,5039,4992,2242,8541,7372,2067,1294,3058,1306,320,8881,5756,9326,411,8650,8824,5495,8282,8397,2000,1228,7817,2099,6473,3571,5994,4447,1299,5991,543,7874,2297,1651,101,2093,3463,9189,6872,6118,872,1008,1779,2805,9084,4048,2123,5877,55,3075,1737,9459,4535,6453,3644,108,5982,4437,5213,1340,6967,9943,5815,669,8074,1838,6979,9132,9315,715,5048],
|
||||
[3327,4030,7177,6336,9933,5296,2621,4785,2755,4832,2512,2118,2244,4407,2170,499,7532,9742,5051,7687,970,6924,3527,4694,5145,1306,2165,5940,2425,8910,3513,1909,6983,346,6377,4304,9330,7203,6605,3709,3346,970,369,9737,5811,4427,9939,3693,8436,5566,1977,3728,2399,3985,8303,2492,5366,9802,9193,7296,1033,5060,9144,2766,1151,7629,5169,5995,58,7619,7565,4208,1713,6279,3209,4908,9224,7409,1325,8540],
|
||||
[6882,1265,1775,3648,4690,959,5837,4520,5394,1378,9485,1360,4018,578,9174,2932,9890,3696,116,1723,1178,9355,7063,1594,1918,8574,7594,7942,1547,6166,7888,354,6932,4651,1010,7759,6905,661,7689,6092,9292,3845,9605,8443,443,8275,5163,7720,7265,6356,7779,1798,1754,5225,6661,1180,8024,5666,88,9153,1840,3508,1193,4445,2648,3538,6243,6375,8107,5902,5423,2520,1122,5015,6113,8859,9370,966,8673,2442],
|
||||
[7338,3423,4723,6533,848,8041,7921,8277,4094,5368,7252,8852,9166,2250,2801,6125,8093,5738,4038,9808,7359,9494,601,9116,4946,2702,5573,2921,9862,1462,1269,2410,4171,2709,7508,6241,7522,615,2407,8200,4189,5492,5649,7353,2590,5203,4274,710,7329,9063,956,8371,3722,4253,4785,1194,4828,4717,4548,940,983,2575,4511,2938,1827,2027,2700,1236,841,5760,1680,6260,2373,3851,1841,4968,1172,5179,7175,3509],
|
||||
[4420,1327,3560,2376,6260,2988,9537,4064,4829,8872,9598,3228,1792,7118,9962,9336,4368,9189,6857,1829,9863,6287,7303,7769,2707,8257,2391,2009,3975,4993,3068,9835,3427,341,8412,2134,4034,8511,6421,3041,9012,2983,7289,100,1355,7904,9186,6920,5856,2008,6545,8331,3655,5011,839,8041,9255,6524,3862,8788,62,7455,3513,5003,8413,3918,2076,7960,6108,3638,6999,3436,1441,4858,4181,1866,8731,7745,3744,1000],
|
||||
[356,8296,8325,1058,1277,4743,3850,2388,6079,6462,2815,5620,8495,5378,75,4324,3441,9870,1113,165,1544,1179,2834,562,6176,2313,6836,8839,2986,9454,5199,6888,1927,5866,8760,320,1792,8296,7898,6121,7241,5886,5814,2815,8336,1576,4314,3109,2572,6011,2086,9061,9403,3947,5487,9731,7281,3159,1819,1334,3181,5844,5114,9898,4634,2531,4412,6430,4262,8482,4546,4555,6804,2607,9421,686,8649,8860,7794,6672],
|
||||
[9870,152,1558,4963,8750,4754,6521,6256,8818,5208,5691,9659,8377,9725,5050,5343,2539,6101,1844,9700,7750,8114,5357,3001,8830,4438,199,9545,8496,43,2078,327,9397,106,6090,8181,8646,6414,7499,5450,4850,6273,5014,4131,7639,3913,6571,8534,9703,4391,7618,445,1320,5,1894,6771,7383,9191,4708,9706,6939,7937,8726,9382,5216,3685,2247,9029,8154,1738,9984,2626,9438,4167,6351,5060,29,1218,1239,4785],
|
||||
[192,5213,8297,8974,4032,6966,5717,1179,6523,4679,9513,1481,3041,5355,9303,9154,1389,8702,6589,7818,6336,3539,5538,3094,6646,6702,6266,2759,4608,4452,617,9406,8064,6379,444,5602,4950,1810,8391,1536,316,8714,1178,5182,5863,5110,5372,4954,1978,2971,5680,4863,2255,4630,5723,2168,538,1692,1319,7540,440,6430,6266,7712,7385,5702,620,641,3136,7350,1478,3155,2820,9109,6261,1122,4470,14,8493,2095],
|
||||
[1046,4301,6082,474,4974,7822,2102,5161,5172,6946,8074,9716,6586,9962,9749,5015,2217,995,5388,4402,7652,6399,6539,1349,8101,3677,1328,9612,7922,2879,231,5887,2655,508,4357,4964,3554,5930,6236,7384,4614,280,3093,9600,2110,7863,2631,6626,6620,68,1311,7198,7561,1768,5139,1431,221,230,2940,968,5283,6517,2146,1646,869,9402,7068,8645,7058,1765,9690,4152,2926,9504,2939,7504,6074,2944,6470,7859],
|
||||
[4659,736,4951,9344,1927,6271,8837,8711,3241,6579,7660,5499,5616,3743,5801,4682,9748,8796,779,1833,4549,8138,4026,775,4170,2432,4174,3741,7540,8017,2833,4027,396,811,2871,1150,9809,2719,9199,8504,1224,540,2051,3519,7982,7367,2761,308,3358,6505,2050,4836,5090,7864,805,2566,2409,6876,3361,8622,5572,5895,3280,441,7893,8105,1634,2929,274,3926,7786,6123,8233,9921,2674,5340,1445,203,4585,3837],
|
||||
[5759,338,7444,7968,7742,3755,1591,4839,1705,650,7061,2461,9230,9391,9373,2413,1213,431,7801,4994,2380,2703,6161,6878,8331,2538,6093,1275,5065,5062,2839,582,1014,8109,3525,1544,1569,8622,7944,2905,6120,1564,1839,5570,7579,1318,2677,5257,4418,5601,7935,7656,5192,1864,5886,6083,5580,6202,8869,1636,7907,4759,9082,5854,3185,7631,6854,5872,5632,5280,1431,2077,9717,7431,4256,8261,9680,4487,4752,4286],
|
||||
[1571,1428,8599,1230,7772,4221,8523,9049,4042,8726,7567,6736,9033,2104,4879,4967,6334,6716,3994,1269,8995,6539,3610,7667,6560,6065,874,848,4597,1711,7161,4811,6734,5723,6356,6026,9183,2586,5636,1092,7779,7923,8747,6887,7505,9909,1792,3233,4526,3176,1508,8043,720,5212,6046,4988,709,5277,8256,3642,1391,5803,1468,2145,3970,6301,7767,2359,8487,9771,8785,7520,856,1605,8972,2402,2386,991,1383,5963],
|
||||
[1822,4824,5957,6511,9868,4113,301,9353,6228,2881,2966,6956,9124,9574,9233,1601,7340,973,9396,540,4747,8590,9535,3650,7333,7583,4806,3593,2738,8157,5215,8472,2284,9473,3906,6982,5505,6053,7936,6074,7179,6688,1564,1103,6860,5839,2022,8490,910,7551,7805,881,7024,1855,9448,4790,1274,3672,2810,774,7623,4223,4850,6071,9975,4935,1915,9771,6690,3846,517,463,7624,4511,614,6394,3661,7409,1395,8127],
|
||||
[8738,3850,9555,3695,4383,2378,87,6256,6740,7682,9546,4255,6105,2000,1851,4073,8957,9022,6547,5189,2487,303,9602,7833,1628,4163,6678,3144,8589,7096,8913,5823,4890,7679,1212,9294,5884,2972,3012,3359,7794,7428,1579,4350,7246,4301,7779,7790,3294,9547,4367,3549,1958,8237,6758,3497,3250,3456,6318,1663,708,7714,6143,6890,3428,6853,9334,7992,591,6449,9786,1412,8500,722,5468,1371,108,3939,4199,2535],
|
||||
[7047,4323,1934,5163,4166,461,3544,2767,6554,203,6098,2265,9078,2075,4644,6641,8412,9183,487,101,7566,5622,1975,5726,2920,5374,7779,5631,3753,3725,2672,3621,4280,1162,5812,345,8173,9785,1525,955,5603,2215,2580,5261,2765,2990,5979,389,3907,2484,1232,5933,5871,3304,1138,1616,5114,9199,5072,7442,7245,6472,4760,6359,9053,7876,2564,9404,3043,9026,2261,3374,4460,7306,2326,966,828,3274,1712,3446],
|
||||
[3975,4565,8131,5800,4570,2306,8838,4392,9147,11,3911,7118,9645,4994,2028,6062,5431,2279,8752,2658,7836,994,7316,5336,7185,3289,1898,9689,2331,5737,3403,1124,2679,3241,7748,16,2724,5441,6640,9368,9081,5618,858,4969,17,2103,6035,8043,7475,2181,939,415,1617,8500,8253,2155,7843,7974,7859,1746,6336,3193,2617,8736,4079,6324,6645,8891,9396,5522,6103,1857,8979,3835,2475,1310,7422,610,8345,7615],
|
||||
[9248,5397,5686,2988,3446,4359,6634,9141,497,9176,6773,7448,1907,8454,916,1596,2241,1626,1384,2741,3649,5362,8791,7170,2903,2475,5325,6451,924,3328,522,90,4813,9737,9557,691,2388,1383,4021,1609,9206,4707,5200,7107,8104,4333,9860,5013,1224,6959,8527,1877,4545,7772,6268,621,4915,9349,5970,706,9583,3071,4127,780,8231,3017,9114,3836,7503,2383,1977,4870,8035,2379,9704,1037,3992,3642,1016,4303],
|
||||
[5093,138,4639,6609,1146,5565,95,7521,9077,2272,974,4388,2465,2650,722,4998,3567,3047,921,2736,7855,173,2065,4238,1048,5,6847,9548,8632,9194,5942,4777,7910,8971,6279,7253,2516,1555,1833,3184,9453,9053,6897,7808,8629,4877,1871,8055,4881,7639,1537,7701,2508,7564,5845,5023,2304,5396,3193,2955,1088,3801,6203,1748,3737,1276,13,4120,7715,8552,3047,2921,106,7508,304,1280,7140,2567,9135,5266],
|
||||
[6237,4607,7527,9047,522,7371,4883,2540,5867,6366,5301,1570,421,276,3361,527,6637,4861,2401,7522,5808,9371,5298,2045,5096,5447,7755,5115,7060,8529,4078,1943,1697,1764,5453,7085,960,2405,739,2100,5800,728,9737,5704,5693,1431,8979,6428,673,7540,6,7773,5857,6823,150,5869,8486,684,5816,9626,7451,5579,8260,3397,5322,6920,1879,2127,2884,5478,4977,9016,6165,6292,3062,5671,5968,78,4619,4763],
|
||||
[9905,7127,9390,5185,6923,3721,9164,9705,4341,1031,1046,5127,7376,6528,3248,4941,1178,7889,3364,4486,5358,9402,9158,8600,1025,874,1839,1783,309,9030,1843,845,8398,1433,7118,70,8071,2877,3904,8866,6722,4299,10,1929,5897,4188,600,1889,3325,2485,6473,4474,7444,6992,4846,6166,4441,2283,2629,4352,7775,1101,2214,9985,215,8270,9750,2740,8361,7103,5930,8664,9690,8302,9267,344,2077,1372,1880,9550],
|
||||
[5825,8517,7769,2405,8204,1060,3603,7025,478,8334,1997,3692,7433,9101,7294,7498,9415,5452,3850,3508,6857,9213,6807,4412,7310,854,5384,686,4978,892,8651,3241,2743,3801,3813,8588,6701,4416,6990,6490,3197,6838,6503,114,8343,5844,8646,8694,65,791,5979,2687,2621,2019,8097,1423,3644,9764,4921,3266,3662,5561,2476,8271,8138,6147,1168,3340,1998,9874,6572,9873,6659,5609,2711,3931,9567,4143,7833,8887],
|
||||
[6223,2099,2700,589,4716,8333,1362,5007,2753,2848,4441,8397,7192,8191,4916,9955,6076,3370,6396,6971,3156,248,3911,2488,4930,2458,7183,5455,170,6809,6417,3390,1956,7188,577,7526,2203,968,8164,479,8699,7915,507,6393,4632,1597,7534,3604,618,3280,6061,9793,9238,8347,568,9645,2070,5198,6482,5000,9212,6655,5961,7513,1323,3872,6170,3812,4146,2736,67,3151,5548,2781,9679,7564,5043,8587,1893,4531],
|
||||
[5826,3690,6724,2121,9308,6986,8106,6659,2142,1642,7170,2877,5757,6494,8026,6571,8387,9961,6043,9758,9607,6450,8631,8334,7359,5256,8523,2225,7487,1977,9555,8048,5763,2414,4948,4265,2427,8978,8088,8841,9208,9601,5810,9398,8866,9138,4176,5875,7212,3272,6759,5678,7649,4922,5422,1343,8197,3154,3600,687,1028,4579,2084,9467,4492,7262,7296,6538,7657,7134,2077,1505,7332,6890,8964,4879,7603,7400,5973,739],
|
||||
[1861,1613,4879,1884,7334,966,2000,7489,2123,4287,1472,3263,4726,9203,1040,4103,6075,6049,330,9253,4062,4268,1635,9960,577,1320,3195,9628,1030,4092,4979,6474,6393,2799,6967,8687,7724,7392,9927,2085,3200,6466,8702,265,7646,8665,7986,7266,4574,6587,612,2724,704,3191,8323,9523,3002,704,5064,3960,8209,2027,2758,8393,4875,4641,9584,6401,7883,7014,768,443,5490,7506,1852,2005,8850,5776,4487,4269],
|
||||
[4052,6687,4705,7260,6645,6715,3706,5504,8672,2853,1136,8187,8203,4016,871,1809,1366,4952,9294,5339,6872,2645,6083,7874,3056,5218,7485,8796,7401,3348,2103,426,8572,4163,9171,3176,948,7654,9344,3217,1650,5580,7971,2622,76,2874,880,2034,9929,1546,2659,5811,3754,7096,7436,9694,9960,7415,2164,953,2360,4194,2397,1047,2196,6827,575,784,2675,8821,6802,7972,5996,6699,2134,7577,2887,1412,4349,4380],
|
||||
[4629,2234,6240,8132,7592,3181,6389,1214,266,1910,2451,8784,2790,1127,6932,1447,8986,2492,5476,397,889,3027,7641,5083,5776,4022,185,3364,5701,2442,2840,4160,9525,4828,6602,2614,7447,3711,4505,7745,8034,6514,4907,2605,7753,6958,7270,6936,3006,8968,439,2326,4652,3085,3425,9863,5049,5361,8688,297,7580,8777,7916,6687,8683,7141,306,9569,2384,1500,3346,4601,7329,9040,6097,2727,6314,4501,4974,2829],
|
||||
[8316,4072,2025,6884,3027,1808,5714,7624,7880,8528,4205,8686,7587,3230,1139,7273,6163,6986,3914,9309,1464,9359,4474,7095,2212,7302,2583,9462,7532,6567,1606,4436,8981,5612,6796,4385,5076,2007,6072,3678,8331,1338,3299,8845,4783,8613,4071,1232,6028,2176,3990,2148,3748,103,9453,538,6745,9110,926,3125,473,5970,8728,7072,9062,1404,1317,5139,9862,6496,6062,3338,464,1600,2532,1088,8232,7739,8274,3873],
|
||||
[2341,523,7096,8397,8301,6541,9844,244,4993,2280,7689,4025,4196,5522,7904,6048,2623,9258,2149,9461,6448,8087,7245,1917,8340,7127,8466,5725,6996,3421,5313,512,9164,9837,9794,8369,4185,1488,7210,1524,1016,4620,9435,2478,7765,8035,697,6677,3724,6988,5853,7662,3895,9593,1185,4727,6025,5734,7665,3070,138,8469,6748,6459,561,7935,8646,2378,462,7755,3115,9690,8877,3946,2728,8793,244,6323,8666,4271],
|
||||
[6430,2406,8994,56,1267,3826,9443,7079,7579,5232,6691,3435,6718,5698,4144,7028,592,2627,217,734,6194,8156,9118,58,2640,8069,4127,3285,694,3197,3377,4143,4802,3324,8134,6953,7625,3598,3584,4289,7065,3434,2106,7132,5802,7920,9060,7531,3321,1725,1067,3751,444,5503,6785,7937,6365,4803,198,6266,8177,1470,6390,1606,2904,7555,9834,8667,2033,1723,5167,1666,8546,8152,473,4475,6451,7947,3062,3281],
|
||||
[2810,3042,7759,1741,2275,2609,7676,8640,4117,1958,7500,8048,1757,3954,9270,1971,4796,2912,660,5511,3553,1012,5757,4525,6084,7198,8352,5775,7726,8591,7710,9589,3122,4392,6856,5016,749,2285,3356,7482,9956,7348,2599,8944,495,3462,3578,551,4543,7207,7169,7796,1247,4278,6916,8176,3742,8385,2310,1345,8692,2667,4568,1770,8319,3585,4920,3890,4928,7343,5385,9772,7947,8786,2056,9266,3454,2807,877,2660],
|
||||
[6206,8252,5928,5837,4177,4333,207,7934,5581,9526,8906,1498,8411,2984,5198,5134,2464,8435,8514,8674,3876,599,5327,826,2152,4084,2433,9327,9697,4800,2728,3608,3849,3861,3498,9943,1407,3991,7191,9110,5666,8434,4704,6545,5944,2357,1163,4995,9619,6754,4200,9682,6654,4862,4744,5953,6632,1054,293,9439,8286,2255,696,8709,1533,1844,6441,430,1999,6063,9431,7018,8057,2920,6266,6799,356,3597,4024,6665],
|
||||
[3847,6356,8541,7225,2325,2946,5199,469,5450,7508,2197,9915,8284,7983,6341,3276,3321,16,1321,7608,5015,3362,8491,6968,6818,797,156,2575,706,9516,5344,5457,9210,5051,8099,1617,9951,7663,8253,9683,2670,1261,4710,1068,8753,4799,1228,2621,3275,6188,4699,1791,9518,8701,5932,4275,6011,9877,2933,4182,6059,2930,6687,6682,9771,654,9437,3169,8596,1827,5471,8909,2352,123,4394,3208,8756,5513,6917,2056],
|
||||
[5458,8173,3138,3290,4570,4892,3317,4251,9699,7973,1163,1935,5477,6648,9614,5655,9592,975,9118,2194,7322,8248,8413,3462,8560,1907,7810,6650,7355,2939,4973,6894,3933,3784,3200,2419,9234,4747,2208,2207,1945,2899,1407,6145,8023,3484,5688,7686,2737,3828,3704,9004,5190,9740,8643,8650,5358,4426,1522,1707,3613,9887,6956,2447,2762,833,1449,9489,2573,1080,4167,3456,6809,2466,227,7125,2759,6250,6472,8089],
|
||||
[3266,7025,9756,3914,1265,9116,7723,9788,6805,5493,2092,8688,6592,9173,4431,4028,6007,7131,4446,4815,3648,6701,759,3312,8355,4485,4187,5188,8746,7759,3528,2177,5243,8379,3838,7233,4607,9187,7216,2190,6967,2920,6082,7910,5354,3609,8958,6949,7731,494,8753,8707,1523,4426,3543,7085,647,6771,9847,646,5049,824,8417,5260,2730,5702,2513,9275,4279,2767,8684,1165,9903,4518,55,9682,8963,6005,2102,6523],
|
||||
[1998,8731,936,1479,5259,7064,4085,91,7745,7136,3773,3810,730,8255,2705,2653,9790,6807,2342,355,9344,2668,3690,2028,9679,8102,574,4318,6481,9175,5423,8062,2867,9657,7553,3442,3920,7430,3945,7639,3714,3392,2525,4995,4850,2867,7951,9667,486,9506,9888,781,8866,1702,3795,90,356,1483,4200,2131,6969,5931,486,6880,4404,1084,5169,4910,6567,8335,4686,5043,2614,3352,2667,4513,6472,7471,5720,1616],
|
||||
[8878,1613,1716,868,1906,2681,564,665,5995,2474,7496,3432,9491,9087,8850,8287,669,823,347,6194,2264,2592,7871,7616,8508,4827,760,2676,4660,4881,7572,3811,9032,939,4384,929,7525,8419,5556,9063,662,8887,7026,8534,3111,1454,2082,7598,5726,6687,9647,7608,73,3014,5063,670,5461,5631,3367,9796,8475,7908,5073,1565,5008,5295,4457,1274,4788,1728,338,600,8415,8535,9351,7750,6887,5845,1741,125],
|
||||
[3637,6489,9634,9464,9055,2413,7824,9517,7532,3577,7050,6186,6980,9365,9782,191,870,2497,8498,2218,2757,5420,6468,586,3320,9230,1034,1393,9886,5072,9391,1178,8464,8042,6869,2075,8275,3601,7715,9470,8786,6475,8373,2159,9237,2066,3264,5000,679,355,3069,4073,494,2308,5512,4334,9438,8786,8637,9774,1169,1949,6594,6072,4270,9158,7916,5752,6794,9391,6301,5842,3285,2141,3898,8027,4310,8821,7079,1307],
|
||||
[8497,6681,4732,7151,7060,5204,9030,7157,833,5014,8723,3207,9796,9286,4913,119,5118,7650,9335,809,3675,2597,5144,3945,5090,8384,187,4102,1260,2445,2792,4422,8389,9290,50,1765,1521,6921,8586,4368,1565,5727,7855,2003,4834,9897,5911,8630,5070,1330,7692,7557,7980,6028,5805,9090,8265,3019,3802,698,9149,5748,1965,9658,4417,5994,5584,8226,2937,272,5743,1278,5698,8736,2595,6475,5342,6596,1149,6920],
|
||||
[8188,8009,9546,6310,8772,2500,9846,6592,6872,3857,1307,8125,7042,1544,6159,2330,643,4604,7899,6848,371,8067,2062,3200,7295,1857,9505,6936,384,2193,2190,301,8535,5503,1462,7380,5114,4824,8833,1763,4974,8711,9262,6698,3999,2645,6937,7747,1128,2933,3556,7943,2885,3122,9105,5447,418,2899,5148,3699,9021,9501,597,4084,175,1621,1,1079,6067,5812,4326,9914,6633,5394,4233,6728,9084,1864,5863,1225],
|
||||
[9935,8793,9117,1825,9542,8246,8437,3331,9128,9675,6086,7075,319,1334,7932,3583,7167,4178,1726,7720,695,8277,7887,6359,5912,1719,2780,8529,1359,2013,4498,8072,1129,9998,1147,8804,9405,6255,1619,2165,7491,1,8882,7378,3337,503,5758,4109,3577,985,3200,7615,8058,5032,1080,6410,6873,5496,1466,2412,9885,5904,4406,3605,8770,4361,6205,9193,1537,9959,214,7260,9566,1685,100,4920,7138,9819,5637,976],
|
||||
[3466,9854,985,1078,7222,8888,5466,5379,3578,4540,6853,8690,3728,6351,7147,3134,6921,9692,857,3307,4998,2172,5783,3931,9417,2541,6299,13,787,2099,9131,9494,896,8600,1643,8419,7248,2660,2609,8579,91,6663,5506,7675,1947,6165,4286,1972,9645,3805,1663,1456,8853,5705,9889,7489,1107,383,4044,2969,3343,152,7805,4980,9929,5033,1737,9953,7197,9158,4071,1324,473,9676,3984,9680,3606,8160,7384,5432],
|
||||
[1005,4512,5186,3953,2164,3372,4097,3247,8697,3022,9896,4101,3871,6791,3219,2742,4630,6967,7829,5991,6134,1197,1414,8923,8787,1394,8852,5019,7768,5147,8004,8825,5062,9625,7988,1110,3992,7984,9966,6516,6251,8270,421,3723,1432,4830,6935,8095,9059,2214,6483,6846,3120,1587,6201,6691,9096,9627,6671,4002,3495,9939,7708,7465,5879,6959,6634,3241,3401,2355,9061,2611,7830,3941,2177,2146,5089,7079,519,6351],
|
||||
[7280,8586,4261,2831,7217,3141,9994,9940,5462,2189,4005,6942,9848,5350,8060,6665,7519,4324,7684,657,9453,9296,2944,6843,7499,7847,1728,9681,3906,6353,5529,2822,3355,3897,7724,4257,7489,8672,4356,3983,1948,6892,7415,4153,5893,4190,621,1736,4045,9532,7701,3671,1211,1622,3176,4524,9317,7800,5638,6644,6943,5463,3531,2821,1347,5958,3436,1438,2999,994,850,4131,2616,1549,3465,5946,690,9273,6954,7991],
|
||||
[9517,399,3249,2596,7736,2142,1322,968,7350,1614,468,3346,3265,7222,6086,1661,5317,2582,7959,4685,2807,2917,1037,5698,1529,3972,8716,2634,3301,3412,8621,743,8001,4734,888,7744,8092,3671,8941,1487,5658,7099,2781,99,1932,4443,4756,4652,9328,1581,7855,4312,5976,7255,6480,3996,2748,1973,9731,4530,2790,9417,7186,5303,3557,351,7182,9428,1342,9020,7599,1392,8304,2070,9138,7215,2008,9937,1106,7110],
|
||||
[7444,769,9688,632,1571,6820,8743,4338,337,3366,3073,1946,8219,104,4210,6986,249,5061,8693,7960,6546,1004,8857,5997,9352,4338,6105,5008,2556,6518,6694,4345,3727,7956,20,3954,8652,4424,9387,2035,8358,5962,5304,5194,8650,8282,1256,1103,2138,6679,1985,3653,2770,2433,4278,615,2863,1715,242,3790,2636,6998,3088,1671,2239,957,5411,4595,6282,2881,9974,2401,875,7574,2987,4587,3147,6766,9885,2965],
|
||||
[3287,3016,3619,6818,9073,6120,5423,557,2900,2015,8111,3873,1314,4189,1846,4399,7041,7583,2427,2864,3525,5002,2069,748,1948,6015,2684,438,770,8367,1663,7887,7759,1885,157,7770,4520,4878,3857,1137,3525,3050,6276,5569,7649,904,4533,7843,2199,5648,7628,9075,9441,3600,7231,2388,5640,9096,958,3058,584,5899,8150,1181,9616,1098,8162,6819,8171,1519,1140,7665,8801,2632,1299,9192,707,9955,2710,7314],
|
||||
[1772,2963,7578,3541,3095,1488,7026,2634,6015,4633,4370,2762,1650,2174,909,8158,2922,8467,4198,4280,9092,8856,8835,5457,2790,8574,9742,5054,9547,4156,7940,8126,9824,7340,8840,6574,3547,1477,3014,6798,7134,435,9484,9859,3031,4,1502,4133,1738,1807,4825,463,6343,9701,8506,9822,9555,8688,8168,3467,3234,6318,1787,5591,419,6593,7974,8486,9861,6381,6758,194,3061,4315,2863,4665,3789,2201,1492,4416],
|
||||
[126,8927,6608,5682,8986,6867,1715,6076,3159,788,3140,4744,830,9253,5812,5021,7616,8534,1546,9590,1101,9012,9821,8132,7857,4086,1069,7491,2988,1579,2442,4321,2149,7642,6108,250,6086,3167,24,9528,7663,2685,1220,9196,1397,5776,1577,1730,5481,977,6115,199,6326,2183,3767,5928,5586,7561,663,8649,9688,949,5913,9160,1870,5764,9887,4477,6703,1413,4995,5494,7131,2192,8969,7138,3997,8697,646,1028],
|
||||
[8074,1731,8245,624,4601,8706,155,8891,309,2552,8208,8452,2954,3124,3469,4246,3352,1105,4509,8677,9901,4416,8191,9283,5625,7120,2952,8881,7693,830,4580,8228,9459,8611,4499,1179,4988,1394,550,2336,6089,6872,269,7213,1848,917,6672,4890,656,1478,6536,3165,4743,4990,1176,6211,7207,5284,9730,4738,1549,4986,4942,8645,3698,9429,1439,2175,6549,3058,6513,1574,6988,8333,3406,5245,5431,7140,7085,6407],
|
||||
[7845,4694,2530,8249,290,5948,5509,1588,5940,4495,5866,5021,4626,3979,3296,7589,4854,1998,5627,3926,8346,6512,9608,1918,7070,4747,4182,2858,2766,4606,6269,4107,8982,8568,9053,4244,5604,102,2756,727,5887,2566,7922,44,5986,621,1202,374,6988,4130,3627,6744,9443,4568,1398,8679,397,3928,9159,367,2917,6127,5788,3304,8129,911,2669,1463,9749,264,4478,8940,1109,7309,2462,117,4692,7724,225,2312],
|
||||
[4164,3637,2000,941,8903,39,3443,7172,1031,3687,4901,8082,4945,4515,7204,9310,9349,9535,9940,218,1788,9245,2237,1541,5670,6538,6047,5553,9807,8101,1925,8714,445,8332,7309,6830,5786,5736,7306,2710,3034,1838,7969,6318,7912,2584,2080,7437,6705,2254,7428,820,782,9861,7596,3842,3631,8063,5240,6666,394,4565,7865,4895,9890,6028,6117,4724,9156,4473,4552,602,470,6191,4927,5387,884,3146,1978,3000],
|
||||
[4258,6880,1696,3582,5793,4923,2119,1155,9056,9698,6603,3768,5514,9927,9609,6166,6566,4536,4985,4934,8076,9062,6741,6163,7399,4562,2337,5600,2919,9012,8459,1308,6072,1225,9306,8818,5886,7243,7365,8792,6007,9256,6699,7171,4230,7002,8720,7839,4533,1671,478,7774,1607,2317,5437,4705,7886,4760,6760,7271,3081,2997,3088,7675,6208,3101,6821,6840,122,9633,4900,2067,8546,4549,2091,7188,5605,8599,6758,5229],
|
||||
[7854,5243,9155,3556,8812,7047,2202,1541,5993,4600,4760,713,434,7911,7426,7414,8729,322,803,7960,7563,4908,6285,6291,736,3389,9339,4132,8701,7534,5287,3646,592,3065,7582,2592,8755,6068,8597,1982,5782,1894,2900,6236,4039,6569,3037,5837,7698,700,7815,2491,7272,5878,3083,6778,6639,3589,5010,8313,2581,6617,5869,8402,6808,2951,2321,5195,497,2190,6187,1342,1316,4453,7740,4154,2959,1781,1482,8256],
|
||||
[7178,2046,4419,744,8312,5356,6855,8839,319,2962,5662,47,6307,8662,68,4813,567,2712,9931,1678,3101,8227,6533,4933,6656,92,5846,4780,6256,6361,4323,9985,1231,2175,7178,3034,9744,6155,9165,7787,5836,9318,7860,9644,8941,6480,9443,8188,5928,161,6979,2352,5628,6991,1198,8067,5867,6620,3778,8426,2994,3122,3124,6335,3918,8897,2655,9670,634,1088,1576,8935,7255,474,8166,7417,9547,2886,5560,3842],
|
||||
[6957,3111,26,7530,7143,1295,1744,6057,3009,1854,8098,5405,2234,4874,9447,2620,9303,27,7410,969,40,2966,5648,7596,8637,4238,3143,3679,7187,690,9980,7085,7714,9373,5632,7526,6707,3951,9734,4216,2146,3602,5371,6029,3039,4433,4855,4151,1449,3376,8009,7240,7027,4602,2947,9081,4045,8424,9352,8742,923,2705,4266,3232,2264,6761,363,2651,3383,7770,6730,7856,7340,9679,2158,610,4471,4608,910,6241],
|
||||
[4417,6756,1013,8797,658,8809,5032,8703,7541,846,3357,2920,9817,1745,9980,7593,4667,3087,779,3218,6233,5568,4296,2289,2654,7898,5021,9461,5593,8214,9173,4203,2271,7980,2983,5952,9992,8399,3468,1776,3188,9314,1720,6523,2933,621,8685,5483,8986,6163,3444,9539,4320,155,3992,2828,2150,6071,524,2895,5468,8063,1210,3348,9071,4862,483,9017,4097,6186,9815,3610,5048,1644,1003,9865,9332,2145,1944,2213],
|
||||
[9284,3803,4920,1927,6706,4344,7383,4786,9890,2010,5228,1224,3158,6967,8580,8990,8883,5213,76,8306,2031,4980,5639,9519,7184,5645,7769,3259,8077,9130,1317,3096,9624,3818,1770,695,2454,947,6029,3474,9938,3527,5696,4760,7724,7738,2848,6442,5767,6845,8323,4131,2859,7595,2500,4815,3660,9130,8580,7016,8231,4391,8369,3444,4069,4021,556,6154,627,2778,1496,4206,6356,8434,8491,3816,8231,3190,5575,1015],
|
||||
[3787,7572,1788,6803,5641,6844,1961,4811,8535,9914,9999,1450,8857,738,4662,8569,6679,2225,7839,8618,286,2648,5342,2294,3205,4546,176,8705,3741,6134,8324,8021,7004,5205,7032,6637,9442,5539,5584,4819,5874,5807,8589,6871,9016,983,1758,3786,1519,6241,185,8398,495,3370,9133,3051,4549,9674,7311,9738,3316,9383,2658,2776,9481,7558,619,3943,3324,6491,4933,153,9738,4623,912,3595,7771,7939,1219,4405],
|
||||
[2650,3883,4154,5809,315,7756,4430,1788,4451,1631,6461,7230,6017,5751,138,588,5282,2442,9110,9035,6349,2515,1570,6122,4192,4174,3530,1933,4186,4420,4609,5739,4135,2963,6308,1161,8809,8619,2796,3819,6971,8228,4188,1492,909,8048,2328,6772,8467,7671,9068,2226,7579,6422,7056,8042,3296,2272,3006,2196,7320,3238,3490,3102,37,1293,3212,4767,5041,8773,5794,4456,6174,7279,7054,2835,7053,9088,790,6640],
|
||||
[3101,1057,7057,3826,6077,1025,2955,1224,1114,6729,5902,4698,6239,7203,9423,1804,4417,6686,1426,6941,8071,1029,4985,9010,6122,6597,1622,1574,3513,1684,7086,5505,3244,411,9638,4150,907,9135,829,981,1707,5359,8781,9751,5,9131,3973,7159,1340,6955,7514,7993,6964,8198,1933,2797,877,3993,4453,8020,9349,8646,2779,8679,2961,3547,3374,3510,1129,3568,2241,2625,9138,5974,8206,7669,7678,1833,8700,4480],
|
||||
[4865,9912,8038,8238,782,3095,8199,1127,4501,7280,2112,2487,3626,2790,9432,1475,6312,8277,4827,2218,5806,7132,8752,1468,7471,6386,739,8762,8323,8120,5169,9078,9058,3370,9560,7987,8585,8531,5347,9312,1058,4271,1159,5286,5404,6925,8606,9204,7361,2415,560,586,4002,2644,1927,2824,768,4409,2942,3345,1002,808,4941,6267,7979,5140,8643,7553,9438,7320,4938,2666,4609,2778,8158,6730,3748,3867,1866,7181],
|
||||
[171,3771,7134,8927,4778,2913,3326,2004,3089,7853,1378,1729,4777,2706,9578,1360,5693,3036,1851,7248,2403,2273,8536,6501,9216,613,9671,7131,7719,6425,773,717,8803,160,1114,7554,7197,753,4513,4322,8499,4533,2609,4226,8710,6627,644,9666,6260,4870,5744,7385,6542,6203,7703,6130,8944,5589,2262,6803,6381,7414,6888,5123,7320,9392,9061,6780,322,8975,7050,5089,1061,2260,3199,1150,1865,5386,9699,6501],
|
||||
[3744,8454,6885,8277,919,1923,4001,6864,7854,5519,2491,6057,8794,9645,1776,5714,9786,9281,7538,6916,3215,395,2501,9618,4835,8846,9708,2813,3303,1794,8309,7176,2206,1602,1838,236,4593,2245,8993,4017,10,8215,6921,5206,4023,5932,6997,7801,262,7640,3107,8275,4938,7822,2425,3223,3886,2105,8700,9526,2088,8662,8034,7004,5710,2124,7164,3574,6630,9980,4242,2901,9471,1491,2117,4562,1130,9086,4117,6698],
|
||||
[2810,2280,2331,1170,4554,4071,8387,1215,2274,9848,6738,1604,7281,8805,439,1298,8318,7834,9426,8603,6092,7944,1309,8828,303,3157,4638,4439,9175,1921,4695,7716,1494,1015,1772,5913,1127,1952,1950,8905,4064,9890,385,9357,7945,5035,7082,5369,4093,6546,5187,5637,2041,8946,1758,7111,6566,1027,1049,5148,7224,7248,296,6169,375,1656,7993,2816,3717,4279,4675,1609,3317,42,6201,3100,3144,163,9530,4531],
|
||||
[7096,6070,1009,4988,3538,5801,7149,3063,2324,2912,7911,7002,4338,7880,2481,7368,3516,2016,7556,2193,1388,3865,8125,4637,4096,8114,750,3144,1938,7002,9343,4095,1392,4220,3455,6969,9647,1321,9048,1996,1640,6626,1788,314,9578,6630,2813,6626,4981,9908,7024,4355,3201,3521,3864,3303,464,1923,595,9801,3391,8366,8084,9374,1041,8807,9085,1892,9431,8317,9016,9221,8574,9981,9240,5395,2009,6310,2854,9255],
|
||||
[8830,3145,2960,9615,8220,6061,3452,2918,6481,9278,2297,3385,6565,7066,7316,5682,107,7646,4466,68,1952,9603,8615,54,7191,791,6833,2560,693,9733,4168,570,9127,9537,1925,8287,5508,4297,8452,8795,6213,7994,2420,4208,524,5915,8602,8330,2651,8547,6156,1812,6271,7991,9407,9804,1553,6866,1128,2119,4691,9711,8315,5879,9935,6900,482,682,4126,1041,428,6247,3720,5882,7526,2582,4327,7725,3503,2631],
|
||||
[2738,9323,721,7434,1453,6294,2957,3786,5722,6019,8685,4386,3066,9057,6860,499,5315,3045,5194,7111,3137,9104,941,586,3066,755,4177,8819,7040,5309,3583,3897,4428,7788,4721,7249,6559,7324,825,7311,3760,6064,6070,9672,4882,584,1365,9739,9331,5783,2624,7889,1604,1303,1555,7125,8312,425,8936,3233,7724,1480,403,7440,1784,1754,4721,1569,652,3893,4574,5692,9730,4813,9844,8291,9199,7101,3391,8914],
|
||||
[6044,2928,9332,3328,8588,447,3830,1176,3523,2705,8365,6136,5442,9049,5526,8575,8869,9031,7280,706,2794,8814,5767,4241,7696,78,6570,556,5083,1426,4502,3336,9518,2292,1885,3740,3153,9348,9331,8051,2759,5407,9028,7840,9255,831,515,2612,9747,7435,8964,4971,2048,4900,5967,8271,1719,9670,2810,6777,1594,6367,6259,8316,3815,1689,6840,9437,4361,822,9619,3065,83,6344,7486,8657,8228,9635,6932,4864],
|
||||
[8478,4777,6334,4678,7476,4963,6735,3096,5860,1405,5127,7269,7793,4738,227,9168,2996,8928,765,733,1276,7677,6258,1528,9558,3329,302,8901,1422,8277,6340,645,9125,8869,5952,141,8141,1816,9635,4025,4184,3093,83,2344,2747,9352,7966,1206,1126,1826,218,7939,2957,2729,810,8752,5247,4174,4038,8884,7899,9567,301,5265,5752,7524,4381,1669,3106,8270,6228,6373,754,2547,4240,2313,5514,3022,1040,9738],
|
||||
[2265,8192,1763,1369,8469,8789,4836,52,1212,6690,5257,8918,6723,6319,378,4039,2421,8555,8184,9577,1432,7139,8078,5452,9628,7579,4161,7490,5159,8559,1011,81,478,5840,1964,1334,6875,8670,9900,739,1514,8692,522,9316,6955,1345,8132,2277,3193,9773,3923,4177,2183,1236,6747,6575,4874,6003,6409,8187,745,8776,9440,7543,9825,2582,7381,8147,7236,5185,7564,6125,218,7991,6394,391,7659,7456,5128,5294],
|
||||
[2132,8992,8160,5782,4420,3371,3798,5054,552,5631,7546,4716,1332,6486,7892,7441,4370,6231,4579,2121,8615,1145,9391,1524,1385,2400,9437,2454,7896,7467,2928,8400,3299,4025,7458,4703,7206,6358,792,6200,725,4275,4136,7390,5984,4502,7929,5085,8176,4600,119,3568,76,9363,6943,2248,9077,9731,6213,5817,6729,4190,3092,6910,759,2682,8380,1254,9604,3011,9291,5329,9453,9746,2739,6522,3765,5634,1113,5789],
|
||||
[5304,5499,564,2801,679,2653,1783,3608,7359,7797,3284,796,3222,437,7185,6135,8571,2778,7488,5746,678,6140,861,7750,803,9859,9918,2425,3734,2698,9005,4864,9818,6743,2475,132,9486,3825,5472,919,292,4411,7213,7699,6435,9019,6769,1388,802,2124,1345,8493,9487,8558,7061,8777,8833,2427,2238,5409,4957,8503,3171,7622,5779,6145,2417,5873,5563,5693,9574,9491,1937,7384,4563,6842,5432,2751,3406,7981]
|
||||
];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pathSumTwoWays(matrix) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
const testMatrix1 = [
|
||||
[131, 673, 234, 103, 18],
|
||||
[201, 96, 342, 965, 150],
|
||||
[630, 803, 746, 422, 111],
|
||||
[537, 699, 497, 121, 956],
|
||||
[805, 732, 524, 37, 331]
|
||||
];
|
||||
|
||||
pathSumTwoWays(testMatrix1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pathSumTwoWays(matrix) {
|
||||
function makeMinimalMove(row, column) {
|
||||
if (resultMatrix[row][column + 1] < resultMatrix[row + 1][column]) {
|
||||
return resultMatrix[row][column + 1];
|
||||
}
|
||||
return resultMatrix[row + 1][column];
|
||||
}
|
||||
|
||||
const size = matrix.length;
|
||||
const resultMatrix = [];
|
||||
for (let i = 0; i < size; i++) {
|
||||
resultMatrix.push([...matrix[i]])
|
||||
}
|
||||
|
||||
for (let i = size - 2; i >= 0; i--) {
|
||||
resultMatrix[size - 1][i] += resultMatrix[size - 1][i + 1];
|
||||
resultMatrix[i][size - 1] += resultMatrix[i + 1][size - 1];
|
||||
}
|
||||
|
||||
for (let row = size - 2; row >= 0; row--) {
|
||||
for (let column = size - 2; column >= 0; column--) {
|
||||
resultMatrix[row][column] += makeMinimalMove(row, column);
|
||||
}
|
||||
}
|
||||
return resultMatrix[0][0];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,195 @@
|
||||
---
|
||||
id: 5900f3be1000cf542c50fed1
|
||||
title: 'Problem 82: Path sum: three ways'
|
||||
challengeType: 1
|
||||
forumTopicId: 302196
|
||||
dashedName: problem-82-path-sum-three-ways
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
**Note:** This problem is a more challenging version of Problem 81.
|
||||
|
||||
The minimal path sum in the 5 by 5 matrix below, by starting in any cell in the left column and finishing in any cell in the right column, and only moving up, down, and right, is indicated in red and bold; the sum is equal to `994`.
|
||||
|
||||
$$\begin{pmatrix} 131 & 673 & \color{red}{234} & \color{red}{103} & \color{red}{18}\\\\
|
||||
\color{red}{201} & \color{red}{96} & \color{red}{342} & 965 & 150\\\\ 630 & 803 & 746 & 422 & 111\\\\
|
||||
537 & 699 & 497 & 121 & 956\\\\ 805 & 732 & 524 & 37 & 331 \end{pmatrix}$$
|
||||
|
||||
Find the minimal path sum from the left column to the right column in `matrix`, a 2D array representing a matrix. The maximum matrix size used in tests will be 80 by 80.
|
||||
|
||||
# --hints--
|
||||
|
||||
`pathSumThreeWays(testMatrix1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof pathSumThreeWays(_testMatrix1) === 'number');
|
||||
```
|
||||
|
||||
`pathSumThreeWays(testMatrix1)` should return `994`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pathSumThreeWays(_testMatrix1), 994);
|
||||
```
|
||||
|
||||
`pathSumThreeWays(testMatrix2)` should return `260324`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pathSumThreeWays(_testMatrix2), 260324);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
const _testMatrix1 = [
|
||||
[131, 673, 234, 103, 18],
|
||||
[201, 96, 342, 965, 150],
|
||||
[630, 803, 746, 422, 111],
|
||||
[537, 699, 497, 121, 956],
|
||||
[805, 732, 524, 37, 331]
|
||||
];
|
||||
const _testMatrix2 = [
|
||||
[4445,2697,5115,718,2209,2212,654,4348,3079,6821,7668,3276,8874,4190,3785,2752,9473,7817,9137,496,7338,3434,7152,4355,4552,7917,7827,2460,2350,691,3514,5880,3145,7633,7199,3783,5066,7487,3285,1084,8985,760,872,8609,8051,1134,9536,5750,9716,9371,7619,5617,275,9721,2997,2698,1887,8825,6372,3014,2113,7122,7050,6775,5948,2758,1219,3539,348,7989,2735,9862,1263,8089,6401,9462,3168,2758,3748,5870],
|
||||
[1096,20,1318,7586,5167,2642,1443,5741,7621,7030,5526,4244,2348,4641,9827,2448,6918,5883,3737,300,7116,6531,567,5997,3971,6623,820,6148,3287,1874,7981,8424,7672,7575,6797,6717,1078,5008,4051,8795,5820,346,1851,6463,2117,6058,3407,8211,117,4822,1317,4377,4434,5925,8341,4800,1175,4173,690,8978,7470,1295,3799,8724,3509,9849,618,3320,7068,9633,2384,7175,544,6583,1908,9983,481,4187,9353,9377],
|
||||
[9607,7385,521,6084,1364,8983,7623,1585,6935,8551,2574,8267,4781,3834,2764,2084,2669,4656,9343,7709,2203,9328,8004,6192,5856,3555,2260,5118,6504,1839,9227,1259,9451,1388,7909,5733,6968,8519,9973,1663,5315,7571,3035,4325,4283,2304,6438,3815,9213,9806,9536,196,5542,6907,2475,1159,5820,9075,9470,2179,9248,1828,4592,9167,3713,4640,47,3637,309,7344,6955,346,378,9044,8635,7466,5036,9515,6385,9230],
|
||||
[7206,3114,7760,1094,6150,5182,7358,7387,4497,955,101,1478,7777,6966,7010,8417,6453,4955,3496,107,449,8271,131,2948,6185,784,5937,8001,6104,8282,4165,3642,710,2390,575,715,3089,6964,4217,192,5949,7006,715,3328,1152,66,8044,4319,1735,146,4818,5456,6451,4113,1063,4781,6799,602,1504,6245,6550,1417,1343,2363,3785,5448,4545,9371,5420,5068,4613,4882,4241,5043,7873,8042,8434,3939,9256,2187],
|
||||
[3620,8024,577,9997,7377,7682,1314,1158,6282,6310,1896,2509,5436,1732,9480,706,496,101,6232,7375,2207,2306,110,6772,3433,2878,8140,5933,8688,1399,2210,7332,6172,6403,7333,4044,2291,1790,2446,7390,8698,5723,3678,7104,1825,2040,140,3982,4905,4160,2200,5041,2512,1488,2268,1175,7588,8321,8078,7312,977,5257,8465,5068,3453,3096,1651,7906,253,9250,6021,8791,8109,6651,3412,345,4778,5152,4883,7505],
|
||||
[1074,5438,9008,2679,5397,5429,2652,3403,770,9188,4248,2493,4361,8327,9587,707,9525,5913,93,1899,328,2876,3604,673,8576,6908,7659,2544,3359,3883,5273,6587,3065,1749,3223,604,9925,6941,2823,8767,7039,3290,3214,1787,7904,3421,7137,9560,8451,2669,9219,6332,1576,5477,6755,8348,4164,4307,2984,4012,6629,1044,2874,6541,4942,903,1404,9125,5160,8836,4345,2581,460,8438,1538,5507,668,3352,2678,6942],
|
||||
[4295,1176,5596,1521,3061,9868,7037,7129,8933,6659,5947,5063,3653,9447,9245,2679,767,714,116,8558,163,3927,8779,158,5093,2447,5782,3967,1716,931,7772,8164,1117,9244,5783,7776,3846,8862,6014,2330,6947,1777,3112,6008,3491,1906,5952,314,4602,8994,5919,9214,3995,5026,7688,6809,5003,3128,2509,7477,110,8971,3982,8539,2980,4689,6343,5411,2992,5270,5247,9260,2269,7474,1042,7162,5206,1232,4556,4757],
|
||||
[510,3556,5377,1406,5721,4946,2635,7847,4251,8293,8281,6351,4912,287,2870,3380,3948,5322,3840,4738,9563,1906,6298,3234,8959,1562,6297,8835,7861,239,6618,1322,2553,2213,5053,5446,4402,6500,5182,8585,6900,5756,9661,903,5186,7687,5998,7997,8081,8955,4835,6069,2621,1581,732,9564,1082,1853,5442,1342,520,1737,3703,5321,4793,2776,1508,1647,9101,2499,6891,4336,7012,3329,3212,1442,9993,3988,4930,7706],
|
||||
[9444,3401,5891,9716,1228,7107,109,3563,2700,6161,5039,4992,2242,8541,7372,2067,1294,3058,1306,320,8881,5756,9326,411,8650,8824,5495,8282,8397,2000,1228,7817,2099,6473,3571,5994,4447,1299,5991,543,7874,2297,1651,101,2093,3463,9189,6872,6118,872,1008,1779,2805,9084,4048,2123,5877,55,3075,1737,9459,4535,6453,3644,108,5982,4437,5213,1340,6967,9943,5815,669,8074,1838,6979,9132,9315,715,5048],
|
||||
[3327,4030,7177,6336,9933,5296,2621,4785,2755,4832,2512,2118,2244,4407,2170,499,7532,9742,5051,7687,970,6924,3527,4694,5145,1306,2165,5940,2425,8910,3513,1909,6983,346,6377,4304,9330,7203,6605,3709,3346,970,369,9737,5811,4427,9939,3693,8436,5566,1977,3728,2399,3985,8303,2492,5366,9802,9193,7296,1033,5060,9144,2766,1151,7629,5169,5995,58,7619,7565,4208,1713,6279,3209,4908,9224,7409,1325,8540],
|
||||
[6882,1265,1775,3648,4690,959,5837,4520,5394,1378,9485,1360,4018,578,9174,2932,9890,3696,116,1723,1178,9355,7063,1594,1918,8574,7594,7942,1547,6166,7888,354,6932,4651,1010,7759,6905,661,7689,6092,9292,3845,9605,8443,443,8275,5163,7720,7265,6356,7779,1798,1754,5225,6661,1180,8024,5666,88,9153,1840,3508,1193,4445,2648,3538,6243,6375,8107,5902,5423,2520,1122,5015,6113,8859,9370,966,8673,2442],
|
||||
[7338,3423,4723,6533,848,8041,7921,8277,4094,5368,7252,8852,9166,2250,2801,6125,8093,5738,4038,9808,7359,9494,601,9116,4946,2702,5573,2921,9862,1462,1269,2410,4171,2709,7508,6241,7522,615,2407,8200,4189,5492,5649,7353,2590,5203,4274,710,7329,9063,956,8371,3722,4253,4785,1194,4828,4717,4548,940,983,2575,4511,2938,1827,2027,2700,1236,841,5760,1680,6260,2373,3851,1841,4968,1172,5179,7175,3509],
|
||||
[4420,1327,3560,2376,6260,2988,9537,4064,4829,8872,9598,3228,1792,7118,9962,9336,4368,9189,6857,1829,9863,6287,7303,7769,2707,8257,2391,2009,3975,4993,3068,9835,3427,341,8412,2134,4034,8511,6421,3041,9012,2983,7289,100,1355,7904,9186,6920,5856,2008,6545,8331,3655,5011,839,8041,9255,6524,3862,8788,62,7455,3513,5003,8413,3918,2076,7960,6108,3638,6999,3436,1441,4858,4181,1866,8731,7745,3744,1000],
|
||||
[356,8296,8325,1058,1277,4743,3850,2388,6079,6462,2815,5620,8495,5378,75,4324,3441,9870,1113,165,1544,1179,2834,562,6176,2313,6836,8839,2986,9454,5199,6888,1927,5866,8760,320,1792,8296,7898,6121,7241,5886,5814,2815,8336,1576,4314,3109,2572,6011,2086,9061,9403,3947,5487,9731,7281,3159,1819,1334,3181,5844,5114,9898,4634,2531,4412,6430,4262,8482,4546,4555,6804,2607,9421,686,8649,8860,7794,6672],
|
||||
[9870,152,1558,4963,8750,4754,6521,6256,8818,5208,5691,9659,8377,9725,5050,5343,2539,6101,1844,9700,7750,8114,5357,3001,8830,4438,199,9545,8496,43,2078,327,9397,106,6090,8181,8646,6414,7499,5450,4850,6273,5014,4131,7639,3913,6571,8534,9703,4391,7618,445,1320,5,1894,6771,7383,9191,4708,9706,6939,7937,8726,9382,5216,3685,2247,9029,8154,1738,9984,2626,9438,4167,6351,5060,29,1218,1239,4785],
|
||||
[192,5213,8297,8974,4032,6966,5717,1179,6523,4679,9513,1481,3041,5355,9303,9154,1389,8702,6589,7818,6336,3539,5538,3094,6646,6702,6266,2759,4608,4452,617,9406,8064,6379,444,5602,4950,1810,8391,1536,316,8714,1178,5182,5863,5110,5372,4954,1978,2971,5680,4863,2255,4630,5723,2168,538,1692,1319,7540,440,6430,6266,7712,7385,5702,620,641,3136,7350,1478,3155,2820,9109,6261,1122,4470,14,8493,2095],
|
||||
[1046,4301,6082,474,4974,7822,2102,5161,5172,6946,8074,9716,6586,9962,9749,5015,2217,995,5388,4402,7652,6399,6539,1349,8101,3677,1328,9612,7922,2879,231,5887,2655,508,4357,4964,3554,5930,6236,7384,4614,280,3093,9600,2110,7863,2631,6626,6620,68,1311,7198,7561,1768,5139,1431,221,230,2940,968,5283,6517,2146,1646,869,9402,7068,8645,7058,1765,9690,4152,2926,9504,2939,7504,6074,2944,6470,7859],
|
||||
[4659,736,4951,9344,1927,6271,8837,8711,3241,6579,7660,5499,5616,3743,5801,4682,9748,8796,779,1833,4549,8138,4026,775,4170,2432,4174,3741,7540,8017,2833,4027,396,811,2871,1150,9809,2719,9199,8504,1224,540,2051,3519,7982,7367,2761,308,3358,6505,2050,4836,5090,7864,805,2566,2409,6876,3361,8622,5572,5895,3280,441,7893,8105,1634,2929,274,3926,7786,6123,8233,9921,2674,5340,1445,203,4585,3837],
|
||||
[5759,338,7444,7968,7742,3755,1591,4839,1705,650,7061,2461,9230,9391,9373,2413,1213,431,7801,4994,2380,2703,6161,6878,8331,2538,6093,1275,5065,5062,2839,582,1014,8109,3525,1544,1569,8622,7944,2905,6120,1564,1839,5570,7579,1318,2677,5257,4418,5601,7935,7656,5192,1864,5886,6083,5580,6202,8869,1636,7907,4759,9082,5854,3185,7631,6854,5872,5632,5280,1431,2077,9717,7431,4256,8261,9680,4487,4752,4286],
|
||||
[1571,1428,8599,1230,7772,4221,8523,9049,4042,8726,7567,6736,9033,2104,4879,4967,6334,6716,3994,1269,8995,6539,3610,7667,6560,6065,874,848,4597,1711,7161,4811,6734,5723,6356,6026,9183,2586,5636,1092,7779,7923,8747,6887,7505,9909,1792,3233,4526,3176,1508,8043,720,5212,6046,4988,709,5277,8256,3642,1391,5803,1468,2145,3970,6301,7767,2359,8487,9771,8785,7520,856,1605,8972,2402,2386,991,1383,5963],
|
||||
[1822,4824,5957,6511,9868,4113,301,9353,6228,2881,2966,6956,9124,9574,9233,1601,7340,973,9396,540,4747,8590,9535,3650,7333,7583,4806,3593,2738,8157,5215,8472,2284,9473,3906,6982,5505,6053,7936,6074,7179,6688,1564,1103,6860,5839,2022,8490,910,7551,7805,881,7024,1855,9448,4790,1274,3672,2810,774,7623,4223,4850,6071,9975,4935,1915,9771,6690,3846,517,463,7624,4511,614,6394,3661,7409,1395,8127],
|
||||
[8738,3850,9555,3695,4383,2378,87,6256,6740,7682,9546,4255,6105,2000,1851,4073,8957,9022,6547,5189,2487,303,9602,7833,1628,4163,6678,3144,8589,7096,8913,5823,4890,7679,1212,9294,5884,2972,3012,3359,7794,7428,1579,4350,7246,4301,7779,7790,3294,9547,4367,3549,1958,8237,6758,3497,3250,3456,6318,1663,708,7714,6143,6890,3428,6853,9334,7992,591,6449,9786,1412,8500,722,5468,1371,108,3939,4199,2535],
|
||||
[7047,4323,1934,5163,4166,461,3544,2767,6554,203,6098,2265,9078,2075,4644,6641,8412,9183,487,101,7566,5622,1975,5726,2920,5374,7779,5631,3753,3725,2672,3621,4280,1162,5812,345,8173,9785,1525,955,5603,2215,2580,5261,2765,2990,5979,389,3907,2484,1232,5933,5871,3304,1138,1616,5114,9199,5072,7442,7245,6472,4760,6359,9053,7876,2564,9404,3043,9026,2261,3374,4460,7306,2326,966,828,3274,1712,3446],
|
||||
[3975,4565,8131,5800,4570,2306,8838,4392,9147,11,3911,7118,9645,4994,2028,6062,5431,2279,8752,2658,7836,994,7316,5336,7185,3289,1898,9689,2331,5737,3403,1124,2679,3241,7748,16,2724,5441,6640,9368,9081,5618,858,4969,17,2103,6035,8043,7475,2181,939,415,1617,8500,8253,2155,7843,7974,7859,1746,6336,3193,2617,8736,4079,6324,6645,8891,9396,5522,6103,1857,8979,3835,2475,1310,7422,610,8345,7615],
|
||||
[9248,5397,5686,2988,3446,4359,6634,9141,497,9176,6773,7448,1907,8454,916,1596,2241,1626,1384,2741,3649,5362,8791,7170,2903,2475,5325,6451,924,3328,522,90,4813,9737,9557,691,2388,1383,4021,1609,9206,4707,5200,7107,8104,4333,9860,5013,1224,6959,8527,1877,4545,7772,6268,621,4915,9349,5970,706,9583,3071,4127,780,8231,3017,9114,3836,7503,2383,1977,4870,8035,2379,9704,1037,3992,3642,1016,4303],
|
||||
[5093,138,4639,6609,1146,5565,95,7521,9077,2272,974,4388,2465,2650,722,4998,3567,3047,921,2736,7855,173,2065,4238,1048,5,6847,9548,8632,9194,5942,4777,7910,8971,6279,7253,2516,1555,1833,3184,9453,9053,6897,7808,8629,4877,1871,8055,4881,7639,1537,7701,2508,7564,5845,5023,2304,5396,3193,2955,1088,3801,6203,1748,3737,1276,13,4120,7715,8552,3047,2921,106,7508,304,1280,7140,2567,9135,5266],
|
||||
[6237,4607,7527,9047,522,7371,4883,2540,5867,6366,5301,1570,421,276,3361,527,6637,4861,2401,7522,5808,9371,5298,2045,5096,5447,7755,5115,7060,8529,4078,1943,1697,1764,5453,7085,960,2405,739,2100,5800,728,9737,5704,5693,1431,8979,6428,673,7540,6,7773,5857,6823,150,5869,8486,684,5816,9626,7451,5579,8260,3397,5322,6920,1879,2127,2884,5478,4977,9016,6165,6292,3062,5671,5968,78,4619,4763],
|
||||
[9905,7127,9390,5185,6923,3721,9164,9705,4341,1031,1046,5127,7376,6528,3248,4941,1178,7889,3364,4486,5358,9402,9158,8600,1025,874,1839,1783,309,9030,1843,845,8398,1433,7118,70,8071,2877,3904,8866,6722,4299,10,1929,5897,4188,600,1889,3325,2485,6473,4474,7444,6992,4846,6166,4441,2283,2629,4352,7775,1101,2214,9985,215,8270,9750,2740,8361,7103,5930,8664,9690,8302,9267,344,2077,1372,1880,9550],
|
||||
[5825,8517,7769,2405,8204,1060,3603,7025,478,8334,1997,3692,7433,9101,7294,7498,9415,5452,3850,3508,6857,9213,6807,4412,7310,854,5384,686,4978,892,8651,3241,2743,3801,3813,8588,6701,4416,6990,6490,3197,6838,6503,114,8343,5844,8646,8694,65,791,5979,2687,2621,2019,8097,1423,3644,9764,4921,3266,3662,5561,2476,8271,8138,6147,1168,3340,1998,9874,6572,9873,6659,5609,2711,3931,9567,4143,7833,8887],
|
||||
[6223,2099,2700,589,4716,8333,1362,5007,2753,2848,4441,8397,7192,8191,4916,9955,6076,3370,6396,6971,3156,248,3911,2488,4930,2458,7183,5455,170,6809,6417,3390,1956,7188,577,7526,2203,968,8164,479,8699,7915,507,6393,4632,1597,7534,3604,618,3280,6061,9793,9238,8347,568,9645,2070,5198,6482,5000,9212,6655,5961,7513,1323,3872,6170,3812,4146,2736,67,3151,5548,2781,9679,7564,5043,8587,1893,4531],
|
||||
[5826,3690,6724,2121,9308,6986,8106,6659,2142,1642,7170,2877,5757,6494,8026,6571,8387,9961,6043,9758,9607,6450,8631,8334,7359,5256,8523,2225,7487,1977,9555,8048,5763,2414,4948,4265,2427,8978,8088,8841,9208,9601,5810,9398,8866,9138,4176,5875,7212,3272,6759,5678,7649,4922,5422,1343,8197,3154,3600,687,1028,4579,2084,9467,4492,7262,7296,6538,7657,7134,2077,1505,7332,6890,8964,4879,7603,7400,5973,739],
|
||||
[1861,1613,4879,1884,7334,966,2000,7489,2123,4287,1472,3263,4726,9203,1040,4103,6075,6049,330,9253,4062,4268,1635,9960,577,1320,3195,9628,1030,4092,4979,6474,6393,2799,6967,8687,7724,7392,9927,2085,3200,6466,8702,265,7646,8665,7986,7266,4574,6587,612,2724,704,3191,8323,9523,3002,704,5064,3960,8209,2027,2758,8393,4875,4641,9584,6401,7883,7014,768,443,5490,7506,1852,2005,8850,5776,4487,4269],
|
||||
[4052,6687,4705,7260,6645,6715,3706,5504,8672,2853,1136,8187,8203,4016,871,1809,1366,4952,9294,5339,6872,2645,6083,7874,3056,5218,7485,8796,7401,3348,2103,426,8572,4163,9171,3176,948,7654,9344,3217,1650,5580,7971,2622,76,2874,880,2034,9929,1546,2659,5811,3754,7096,7436,9694,9960,7415,2164,953,2360,4194,2397,1047,2196,6827,575,784,2675,8821,6802,7972,5996,6699,2134,7577,2887,1412,4349,4380],
|
||||
[4629,2234,6240,8132,7592,3181,6389,1214,266,1910,2451,8784,2790,1127,6932,1447,8986,2492,5476,397,889,3027,7641,5083,5776,4022,185,3364,5701,2442,2840,4160,9525,4828,6602,2614,7447,3711,4505,7745,8034,6514,4907,2605,7753,6958,7270,6936,3006,8968,439,2326,4652,3085,3425,9863,5049,5361,8688,297,7580,8777,7916,6687,8683,7141,306,9569,2384,1500,3346,4601,7329,9040,6097,2727,6314,4501,4974,2829],
|
||||
[8316,4072,2025,6884,3027,1808,5714,7624,7880,8528,4205,8686,7587,3230,1139,7273,6163,6986,3914,9309,1464,9359,4474,7095,2212,7302,2583,9462,7532,6567,1606,4436,8981,5612,6796,4385,5076,2007,6072,3678,8331,1338,3299,8845,4783,8613,4071,1232,6028,2176,3990,2148,3748,103,9453,538,6745,9110,926,3125,473,5970,8728,7072,9062,1404,1317,5139,9862,6496,6062,3338,464,1600,2532,1088,8232,7739,8274,3873],
|
||||
[2341,523,7096,8397,8301,6541,9844,244,4993,2280,7689,4025,4196,5522,7904,6048,2623,9258,2149,9461,6448,8087,7245,1917,8340,7127,8466,5725,6996,3421,5313,512,9164,9837,9794,8369,4185,1488,7210,1524,1016,4620,9435,2478,7765,8035,697,6677,3724,6988,5853,7662,3895,9593,1185,4727,6025,5734,7665,3070,138,8469,6748,6459,561,7935,8646,2378,462,7755,3115,9690,8877,3946,2728,8793,244,6323,8666,4271],
|
||||
[6430,2406,8994,56,1267,3826,9443,7079,7579,5232,6691,3435,6718,5698,4144,7028,592,2627,217,734,6194,8156,9118,58,2640,8069,4127,3285,694,3197,3377,4143,4802,3324,8134,6953,7625,3598,3584,4289,7065,3434,2106,7132,5802,7920,9060,7531,3321,1725,1067,3751,444,5503,6785,7937,6365,4803,198,6266,8177,1470,6390,1606,2904,7555,9834,8667,2033,1723,5167,1666,8546,8152,473,4475,6451,7947,3062,3281],
|
||||
[2810,3042,7759,1741,2275,2609,7676,8640,4117,1958,7500,8048,1757,3954,9270,1971,4796,2912,660,5511,3553,1012,5757,4525,6084,7198,8352,5775,7726,8591,7710,9589,3122,4392,6856,5016,749,2285,3356,7482,9956,7348,2599,8944,495,3462,3578,551,4543,7207,7169,7796,1247,4278,6916,8176,3742,8385,2310,1345,8692,2667,4568,1770,8319,3585,4920,3890,4928,7343,5385,9772,7947,8786,2056,9266,3454,2807,877,2660],
|
||||
[6206,8252,5928,5837,4177,4333,207,7934,5581,9526,8906,1498,8411,2984,5198,5134,2464,8435,8514,8674,3876,599,5327,826,2152,4084,2433,9327,9697,4800,2728,3608,3849,3861,3498,9943,1407,3991,7191,9110,5666,8434,4704,6545,5944,2357,1163,4995,9619,6754,4200,9682,6654,4862,4744,5953,6632,1054,293,9439,8286,2255,696,8709,1533,1844,6441,430,1999,6063,9431,7018,8057,2920,6266,6799,356,3597,4024,6665],
|
||||
[3847,6356,8541,7225,2325,2946,5199,469,5450,7508,2197,9915,8284,7983,6341,3276,3321,16,1321,7608,5015,3362,8491,6968,6818,797,156,2575,706,9516,5344,5457,9210,5051,8099,1617,9951,7663,8253,9683,2670,1261,4710,1068,8753,4799,1228,2621,3275,6188,4699,1791,9518,8701,5932,4275,6011,9877,2933,4182,6059,2930,6687,6682,9771,654,9437,3169,8596,1827,5471,8909,2352,123,4394,3208,8756,5513,6917,2056],
|
||||
[5458,8173,3138,3290,4570,4892,3317,4251,9699,7973,1163,1935,5477,6648,9614,5655,9592,975,9118,2194,7322,8248,8413,3462,8560,1907,7810,6650,7355,2939,4973,6894,3933,3784,3200,2419,9234,4747,2208,2207,1945,2899,1407,6145,8023,3484,5688,7686,2737,3828,3704,9004,5190,9740,8643,8650,5358,4426,1522,1707,3613,9887,6956,2447,2762,833,1449,9489,2573,1080,4167,3456,6809,2466,227,7125,2759,6250,6472,8089],
|
||||
[3266,7025,9756,3914,1265,9116,7723,9788,6805,5493,2092,8688,6592,9173,4431,4028,6007,7131,4446,4815,3648,6701,759,3312,8355,4485,4187,5188,8746,7759,3528,2177,5243,8379,3838,7233,4607,9187,7216,2190,6967,2920,6082,7910,5354,3609,8958,6949,7731,494,8753,8707,1523,4426,3543,7085,647,6771,9847,646,5049,824,8417,5260,2730,5702,2513,9275,4279,2767,8684,1165,9903,4518,55,9682,8963,6005,2102,6523],
|
||||
[1998,8731,936,1479,5259,7064,4085,91,7745,7136,3773,3810,730,8255,2705,2653,9790,6807,2342,355,9344,2668,3690,2028,9679,8102,574,4318,6481,9175,5423,8062,2867,9657,7553,3442,3920,7430,3945,7639,3714,3392,2525,4995,4850,2867,7951,9667,486,9506,9888,781,8866,1702,3795,90,356,1483,4200,2131,6969,5931,486,6880,4404,1084,5169,4910,6567,8335,4686,5043,2614,3352,2667,4513,6472,7471,5720,1616],
|
||||
[8878,1613,1716,868,1906,2681,564,665,5995,2474,7496,3432,9491,9087,8850,8287,669,823,347,6194,2264,2592,7871,7616,8508,4827,760,2676,4660,4881,7572,3811,9032,939,4384,929,7525,8419,5556,9063,662,8887,7026,8534,3111,1454,2082,7598,5726,6687,9647,7608,73,3014,5063,670,5461,5631,3367,9796,8475,7908,5073,1565,5008,5295,4457,1274,4788,1728,338,600,8415,8535,9351,7750,6887,5845,1741,125],
|
||||
[3637,6489,9634,9464,9055,2413,7824,9517,7532,3577,7050,6186,6980,9365,9782,191,870,2497,8498,2218,2757,5420,6468,586,3320,9230,1034,1393,9886,5072,9391,1178,8464,8042,6869,2075,8275,3601,7715,9470,8786,6475,8373,2159,9237,2066,3264,5000,679,355,3069,4073,494,2308,5512,4334,9438,8786,8637,9774,1169,1949,6594,6072,4270,9158,7916,5752,6794,9391,6301,5842,3285,2141,3898,8027,4310,8821,7079,1307],
|
||||
[8497,6681,4732,7151,7060,5204,9030,7157,833,5014,8723,3207,9796,9286,4913,119,5118,7650,9335,809,3675,2597,5144,3945,5090,8384,187,4102,1260,2445,2792,4422,8389,9290,50,1765,1521,6921,8586,4368,1565,5727,7855,2003,4834,9897,5911,8630,5070,1330,7692,7557,7980,6028,5805,9090,8265,3019,3802,698,9149,5748,1965,9658,4417,5994,5584,8226,2937,272,5743,1278,5698,8736,2595,6475,5342,6596,1149,6920],
|
||||
[8188,8009,9546,6310,8772,2500,9846,6592,6872,3857,1307,8125,7042,1544,6159,2330,643,4604,7899,6848,371,8067,2062,3200,7295,1857,9505,6936,384,2193,2190,301,8535,5503,1462,7380,5114,4824,8833,1763,4974,8711,9262,6698,3999,2645,6937,7747,1128,2933,3556,7943,2885,3122,9105,5447,418,2899,5148,3699,9021,9501,597,4084,175,1621,1,1079,6067,5812,4326,9914,6633,5394,4233,6728,9084,1864,5863,1225],
|
||||
[9935,8793,9117,1825,9542,8246,8437,3331,9128,9675,6086,7075,319,1334,7932,3583,7167,4178,1726,7720,695,8277,7887,6359,5912,1719,2780,8529,1359,2013,4498,8072,1129,9998,1147,8804,9405,6255,1619,2165,7491,1,8882,7378,3337,503,5758,4109,3577,985,3200,7615,8058,5032,1080,6410,6873,5496,1466,2412,9885,5904,4406,3605,8770,4361,6205,9193,1537,9959,214,7260,9566,1685,100,4920,7138,9819,5637,976],
|
||||
[3466,9854,985,1078,7222,8888,5466,5379,3578,4540,6853,8690,3728,6351,7147,3134,6921,9692,857,3307,4998,2172,5783,3931,9417,2541,6299,13,787,2099,9131,9494,896,8600,1643,8419,7248,2660,2609,8579,91,6663,5506,7675,1947,6165,4286,1972,9645,3805,1663,1456,8853,5705,9889,7489,1107,383,4044,2969,3343,152,7805,4980,9929,5033,1737,9953,7197,9158,4071,1324,473,9676,3984,9680,3606,8160,7384,5432],
|
||||
[1005,4512,5186,3953,2164,3372,4097,3247,8697,3022,9896,4101,3871,6791,3219,2742,4630,6967,7829,5991,6134,1197,1414,8923,8787,1394,8852,5019,7768,5147,8004,8825,5062,9625,7988,1110,3992,7984,9966,6516,6251,8270,421,3723,1432,4830,6935,8095,9059,2214,6483,6846,3120,1587,6201,6691,9096,9627,6671,4002,3495,9939,7708,7465,5879,6959,6634,3241,3401,2355,9061,2611,7830,3941,2177,2146,5089,7079,519,6351],
|
||||
[7280,8586,4261,2831,7217,3141,9994,9940,5462,2189,4005,6942,9848,5350,8060,6665,7519,4324,7684,657,9453,9296,2944,6843,7499,7847,1728,9681,3906,6353,5529,2822,3355,3897,7724,4257,7489,8672,4356,3983,1948,6892,7415,4153,5893,4190,621,1736,4045,9532,7701,3671,1211,1622,3176,4524,9317,7800,5638,6644,6943,5463,3531,2821,1347,5958,3436,1438,2999,994,850,4131,2616,1549,3465,5946,690,9273,6954,7991],
|
||||
[9517,399,3249,2596,7736,2142,1322,968,7350,1614,468,3346,3265,7222,6086,1661,5317,2582,7959,4685,2807,2917,1037,5698,1529,3972,8716,2634,3301,3412,8621,743,8001,4734,888,7744,8092,3671,8941,1487,5658,7099,2781,99,1932,4443,4756,4652,9328,1581,7855,4312,5976,7255,6480,3996,2748,1973,9731,4530,2790,9417,7186,5303,3557,351,7182,9428,1342,9020,7599,1392,8304,2070,9138,7215,2008,9937,1106,7110],
|
||||
[7444,769,9688,632,1571,6820,8743,4338,337,3366,3073,1946,8219,104,4210,6986,249,5061,8693,7960,6546,1004,8857,5997,9352,4338,6105,5008,2556,6518,6694,4345,3727,7956,20,3954,8652,4424,9387,2035,8358,5962,5304,5194,8650,8282,1256,1103,2138,6679,1985,3653,2770,2433,4278,615,2863,1715,242,3790,2636,6998,3088,1671,2239,957,5411,4595,6282,2881,9974,2401,875,7574,2987,4587,3147,6766,9885,2965],
|
||||
[3287,3016,3619,6818,9073,6120,5423,557,2900,2015,8111,3873,1314,4189,1846,4399,7041,7583,2427,2864,3525,5002,2069,748,1948,6015,2684,438,770,8367,1663,7887,7759,1885,157,7770,4520,4878,3857,1137,3525,3050,6276,5569,7649,904,4533,7843,2199,5648,7628,9075,9441,3600,7231,2388,5640,9096,958,3058,584,5899,8150,1181,9616,1098,8162,6819,8171,1519,1140,7665,8801,2632,1299,9192,707,9955,2710,7314],
|
||||
[1772,2963,7578,3541,3095,1488,7026,2634,6015,4633,4370,2762,1650,2174,909,8158,2922,8467,4198,4280,9092,8856,8835,5457,2790,8574,9742,5054,9547,4156,7940,8126,9824,7340,8840,6574,3547,1477,3014,6798,7134,435,9484,9859,3031,4,1502,4133,1738,1807,4825,463,6343,9701,8506,9822,9555,8688,8168,3467,3234,6318,1787,5591,419,6593,7974,8486,9861,6381,6758,194,3061,4315,2863,4665,3789,2201,1492,4416],
|
||||
[126,8927,6608,5682,8986,6867,1715,6076,3159,788,3140,4744,830,9253,5812,5021,7616,8534,1546,9590,1101,9012,9821,8132,7857,4086,1069,7491,2988,1579,2442,4321,2149,7642,6108,250,6086,3167,24,9528,7663,2685,1220,9196,1397,5776,1577,1730,5481,977,6115,199,6326,2183,3767,5928,5586,7561,663,8649,9688,949,5913,9160,1870,5764,9887,4477,6703,1413,4995,5494,7131,2192,8969,7138,3997,8697,646,1028],
|
||||
[8074,1731,8245,624,4601,8706,155,8891,309,2552,8208,8452,2954,3124,3469,4246,3352,1105,4509,8677,9901,4416,8191,9283,5625,7120,2952,8881,7693,830,4580,8228,9459,8611,4499,1179,4988,1394,550,2336,6089,6872,269,7213,1848,917,6672,4890,656,1478,6536,3165,4743,4990,1176,6211,7207,5284,9730,4738,1549,4986,4942,8645,3698,9429,1439,2175,6549,3058,6513,1574,6988,8333,3406,5245,5431,7140,7085,6407],
|
||||
[7845,4694,2530,8249,290,5948,5509,1588,5940,4495,5866,5021,4626,3979,3296,7589,4854,1998,5627,3926,8346,6512,9608,1918,7070,4747,4182,2858,2766,4606,6269,4107,8982,8568,9053,4244,5604,102,2756,727,5887,2566,7922,44,5986,621,1202,374,6988,4130,3627,6744,9443,4568,1398,8679,397,3928,9159,367,2917,6127,5788,3304,8129,911,2669,1463,9749,264,4478,8940,1109,7309,2462,117,4692,7724,225,2312],
|
||||
[4164,3637,2000,941,8903,39,3443,7172,1031,3687,4901,8082,4945,4515,7204,9310,9349,9535,9940,218,1788,9245,2237,1541,5670,6538,6047,5553,9807,8101,1925,8714,445,8332,7309,6830,5786,5736,7306,2710,3034,1838,7969,6318,7912,2584,2080,7437,6705,2254,7428,820,782,9861,7596,3842,3631,8063,5240,6666,394,4565,7865,4895,9890,6028,6117,4724,9156,4473,4552,602,470,6191,4927,5387,884,3146,1978,3000],
|
||||
[4258,6880,1696,3582,5793,4923,2119,1155,9056,9698,6603,3768,5514,9927,9609,6166,6566,4536,4985,4934,8076,9062,6741,6163,7399,4562,2337,5600,2919,9012,8459,1308,6072,1225,9306,8818,5886,7243,7365,8792,6007,9256,6699,7171,4230,7002,8720,7839,4533,1671,478,7774,1607,2317,5437,4705,7886,4760,6760,7271,3081,2997,3088,7675,6208,3101,6821,6840,122,9633,4900,2067,8546,4549,2091,7188,5605,8599,6758,5229],
|
||||
[7854,5243,9155,3556,8812,7047,2202,1541,5993,4600,4760,713,434,7911,7426,7414,8729,322,803,7960,7563,4908,6285,6291,736,3389,9339,4132,8701,7534,5287,3646,592,3065,7582,2592,8755,6068,8597,1982,5782,1894,2900,6236,4039,6569,3037,5837,7698,700,7815,2491,7272,5878,3083,6778,6639,3589,5010,8313,2581,6617,5869,8402,6808,2951,2321,5195,497,2190,6187,1342,1316,4453,7740,4154,2959,1781,1482,8256],
|
||||
[7178,2046,4419,744,8312,5356,6855,8839,319,2962,5662,47,6307,8662,68,4813,567,2712,9931,1678,3101,8227,6533,4933,6656,92,5846,4780,6256,6361,4323,9985,1231,2175,7178,3034,9744,6155,9165,7787,5836,9318,7860,9644,8941,6480,9443,8188,5928,161,6979,2352,5628,6991,1198,8067,5867,6620,3778,8426,2994,3122,3124,6335,3918,8897,2655,9670,634,1088,1576,8935,7255,474,8166,7417,9547,2886,5560,3842],
|
||||
[6957,3111,26,7530,7143,1295,1744,6057,3009,1854,8098,5405,2234,4874,9447,2620,9303,27,7410,969,40,2966,5648,7596,8637,4238,3143,3679,7187,690,9980,7085,7714,9373,5632,7526,6707,3951,9734,4216,2146,3602,5371,6029,3039,4433,4855,4151,1449,3376,8009,7240,7027,4602,2947,9081,4045,8424,9352,8742,923,2705,4266,3232,2264,6761,363,2651,3383,7770,6730,7856,7340,9679,2158,610,4471,4608,910,6241],
|
||||
[4417,6756,1013,8797,658,8809,5032,8703,7541,846,3357,2920,9817,1745,9980,7593,4667,3087,779,3218,6233,5568,4296,2289,2654,7898,5021,9461,5593,8214,9173,4203,2271,7980,2983,5952,9992,8399,3468,1776,3188,9314,1720,6523,2933,621,8685,5483,8986,6163,3444,9539,4320,155,3992,2828,2150,6071,524,2895,5468,8063,1210,3348,9071,4862,483,9017,4097,6186,9815,3610,5048,1644,1003,9865,9332,2145,1944,2213],
|
||||
[9284,3803,4920,1927,6706,4344,7383,4786,9890,2010,5228,1224,3158,6967,8580,8990,8883,5213,76,8306,2031,4980,5639,9519,7184,5645,7769,3259,8077,9130,1317,3096,9624,3818,1770,695,2454,947,6029,3474,9938,3527,5696,4760,7724,7738,2848,6442,5767,6845,8323,4131,2859,7595,2500,4815,3660,9130,8580,7016,8231,4391,8369,3444,4069,4021,556,6154,627,2778,1496,4206,6356,8434,8491,3816,8231,3190,5575,1015],
|
||||
[3787,7572,1788,6803,5641,6844,1961,4811,8535,9914,9999,1450,8857,738,4662,8569,6679,2225,7839,8618,286,2648,5342,2294,3205,4546,176,8705,3741,6134,8324,8021,7004,5205,7032,6637,9442,5539,5584,4819,5874,5807,8589,6871,9016,983,1758,3786,1519,6241,185,8398,495,3370,9133,3051,4549,9674,7311,9738,3316,9383,2658,2776,9481,7558,619,3943,3324,6491,4933,153,9738,4623,912,3595,7771,7939,1219,4405],
|
||||
[2650,3883,4154,5809,315,7756,4430,1788,4451,1631,6461,7230,6017,5751,138,588,5282,2442,9110,9035,6349,2515,1570,6122,4192,4174,3530,1933,4186,4420,4609,5739,4135,2963,6308,1161,8809,8619,2796,3819,6971,8228,4188,1492,909,8048,2328,6772,8467,7671,9068,2226,7579,6422,7056,8042,3296,2272,3006,2196,7320,3238,3490,3102,37,1293,3212,4767,5041,8773,5794,4456,6174,7279,7054,2835,7053,9088,790,6640],
|
||||
[3101,1057,7057,3826,6077,1025,2955,1224,1114,6729,5902,4698,6239,7203,9423,1804,4417,6686,1426,6941,8071,1029,4985,9010,6122,6597,1622,1574,3513,1684,7086,5505,3244,411,9638,4150,907,9135,829,981,1707,5359,8781,9751,5,9131,3973,7159,1340,6955,7514,7993,6964,8198,1933,2797,877,3993,4453,8020,9349,8646,2779,8679,2961,3547,3374,3510,1129,3568,2241,2625,9138,5974,8206,7669,7678,1833,8700,4480],
|
||||
[4865,9912,8038,8238,782,3095,8199,1127,4501,7280,2112,2487,3626,2790,9432,1475,6312,8277,4827,2218,5806,7132,8752,1468,7471,6386,739,8762,8323,8120,5169,9078,9058,3370,9560,7987,8585,8531,5347,9312,1058,4271,1159,5286,5404,6925,8606,9204,7361,2415,560,586,4002,2644,1927,2824,768,4409,2942,3345,1002,808,4941,6267,7979,5140,8643,7553,9438,7320,4938,2666,4609,2778,8158,6730,3748,3867,1866,7181],
|
||||
[171,3771,7134,8927,4778,2913,3326,2004,3089,7853,1378,1729,4777,2706,9578,1360,5693,3036,1851,7248,2403,2273,8536,6501,9216,613,9671,7131,7719,6425,773,717,8803,160,1114,7554,7197,753,4513,4322,8499,4533,2609,4226,8710,6627,644,9666,6260,4870,5744,7385,6542,6203,7703,6130,8944,5589,2262,6803,6381,7414,6888,5123,7320,9392,9061,6780,322,8975,7050,5089,1061,2260,3199,1150,1865,5386,9699,6501],
|
||||
[3744,8454,6885,8277,919,1923,4001,6864,7854,5519,2491,6057,8794,9645,1776,5714,9786,9281,7538,6916,3215,395,2501,9618,4835,8846,9708,2813,3303,1794,8309,7176,2206,1602,1838,236,4593,2245,8993,4017,10,8215,6921,5206,4023,5932,6997,7801,262,7640,3107,8275,4938,7822,2425,3223,3886,2105,8700,9526,2088,8662,8034,7004,5710,2124,7164,3574,6630,9980,4242,2901,9471,1491,2117,4562,1130,9086,4117,6698],
|
||||
[2810,2280,2331,1170,4554,4071,8387,1215,2274,9848,6738,1604,7281,8805,439,1298,8318,7834,9426,8603,6092,7944,1309,8828,303,3157,4638,4439,9175,1921,4695,7716,1494,1015,1772,5913,1127,1952,1950,8905,4064,9890,385,9357,7945,5035,7082,5369,4093,6546,5187,5637,2041,8946,1758,7111,6566,1027,1049,5148,7224,7248,296,6169,375,1656,7993,2816,3717,4279,4675,1609,3317,42,6201,3100,3144,163,9530,4531],
|
||||
[7096,6070,1009,4988,3538,5801,7149,3063,2324,2912,7911,7002,4338,7880,2481,7368,3516,2016,7556,2193,1388,3865,8125,4637,4096,8114,750,3144,1938,7002,9343,4095,1392,4220,3455,6969,9647,1321,9048,1996,1640,6626,1788,314,9578,6630,2813,6626,4981,9908,7024,4355,3201,3521,3864,3303,464,1923,595,9801,3391,8366,8084,9374,1041,8807,9085,1892,9431,8317,9016,9221,8574,9981,9240,5395,2009,6310,2854,9255],
|
||||
[8830,3145,2960,9615,8220,6061,3452,2918,6481,9278,2297,3385,6565,7066,7316,5682,107,7646,4466,68,1952,9603,8615,54,7191,791,6833,2560,693,9733,4168,570,9127,9537,1925,8287,5508,4297,8452,8795,6213,7994,2420,4208,524,5915,8602,8330,2651,8547,6156,1812,6271,7991,9407,9804,1553,6866,1128,2119,4691,9711,8315,5879,9935,6900,482,682,4126,1041,428,6247,3720,5882,7526,2582,4327,7725,3503,2631],
|
||||
[2738,9323,721,7434,1453,6294,2957,3786,5722,6019,8685,4386,3066,9057,6860,499,5315,3045,5194,7111,3137,9104,941,586,3066,755,4177,8819,7040,5309,3583,3897,4428,7788,4721,7249,6559,7324,825,7311,3760,6064,6070,9672,4882,584,1365,9739,9331,5783,2624,7889,1604,1303,1555,7125,8312,425,8936,3233,7724,1480,403,7440,1784,1754,4721,1569,652,3893,4574,5692,9730,4813,9844,8291,9199,7101,3391,8914],
|
||||
[6044,2928,9332,3328,8588,447,3830,1176,3523,2705,8365,6136,5442,9049,5526,8575,8869,9031,7280,706,2794,8814,5767,4241,7696,78,6570,556,5083,1426,4502,3336,9518,2292,1885,3740,3153,9348,9331,8051,2759,5407,9028,7840,9255,831,515,2612,9747,7435,8964,4971,2048,4900,5967,8271,1719,9670,2810,6777,1594,6367,6259,8316,3815,1689,6840,9437,4361,822,9619,3065,83,6344,7486,8657,8228,9635,6932,4864],
|
||||
[8478,4777,6334,4678,7476,4963,6735,3096,5860,1405,5127,7269,7793,4738,227,9168,2996,8928,765,733,1276,7677,6258,1528,9558,3329,302,8901,1422,8277,6340,645,9125,8869,5952,141,8141,1816,9635,4025,4184,3093,83,2344,2747,9352,7966,1206,1126,1826,218,7939,2957,2729,810,8752,5247,4174,4038,8884,7899,9567,301,5265,5752,7524,4381,1669,3106,8270,6228,6373,754,2547,4240,2313,5514,3022,1040,9738],
|
||||
[2265,8192,1763,1369,8469,8789,4836,52,1212,6690,5257,8918,6723,6319,378,4039,2421,8555,8184,9577,1432,7139,8078,5452,9628,7579,4161,7490,5159,8559,1011,81,478,5840,1964,1334,6875,8670,9900,739,1514,8692,522,9316,6955,1345,8132,2277,3193,9773,3923,4177,2183,1236,6747,6575,4874,6003,6409,8187,745,8776,9440,7543,9825,2582,7381,8147,7236,5185,7564,6125,218,7991,6394,391,7659,7456,5128,5294],
|
||||
[2132,8992,8160,5782,4420,3371,3798,5054,552,5631,7546,4716,1332,6486,7892,7441,4370,6231,4579,2121,8615,1145,9391,1524,1385,2400,9437,2454,7896,7467,2928,8400,3299,4025,7458,4703,7206,6358,792,6200,725,4275,4136,7390,5984,4502,7929,5085,8176,4600,119,3568,76,9363,6943,2248,9077,9731,6213,5817,6729,4190,3092,6910,759,2682,8380,1254,9604,3011,9291,5329,9453,9746,2739,6522,3765,5634,1113,5789],
|
||||
[5304,5499,564,2801,679,2653,1783,3608,7359,7797,3284,796,3222,437,7185,6135,8571,2778,7488,5746,678,6140,861,7750,803,9859,9918,2425,3734,2698,9005,4864,9818,6743,2475,132,9486,3825,5472,919,292,4411,7213,7699,6435,9019,6769,1388,802,2124,1345,8493,9487,8558,7061,8777,8833,2427,2238,5409,4957,8503,3171,7622,5779,6145,2417,5873,5563,5693,9574,9491,1937,7384,4563,6842,5432,2751,3406,7981]
|
||||
];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pathSumThreeWays(matrix) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
const testMatrix1 = [
|
||||
[131, 673, 234, 103, 18],
|
||||
[201, 96, 342, 965, 150],
|
||||
[630, 803, 746, 422, 111],
|
||||
[537, 699, 497, 121, 956],
|
||||
[805, 732, 524, 37, 331]
|
||||
];
|
||||
|
||||
pathSumThreeWays(testMatrix1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pathSumThreeWays(matrix) {
|
||||
function makeMinimumMoveFromUpOrRight(row, column) {
|
||||
const curValue = matrix[row][column];
|
||||
if (values[row - 1] > values[row]) {
|
||||
return values[row] + curValue;
|
||||
}
|
||||
return values[row - 1] + curValue;
|
||||
}
|
||||
function isGoingFromDownBetter(row, column) {
|
||||
return values[row] > values[row + 1] + matrix[row][column];
|
||||
}
|
||||
|
||||
const size = matrix.length;
|
||||
const values = [];
|
||||
for (let row = 0; row < size; row++) {
|
||||
values.push(matrix[row][size - 1]);
|
||||
}
|
||||
|
||||
for (let column = size - 2; column >= 0; column--) {
|
||||
values[0] += matrix[0][column];
|
||||
|
||||
for (let row = 1; row < size; row++) {
|
||||
values[row] = makeMinimumMoveFromUpOrRight(row, column);
|
||||
}
|
||||
|
||||
for (let row = size - 2; row >= 0; row--) {
|
||||
if (isGoingFromDownBetter(row, column)) {
|
||||
values[row] = values[row + 1] + matrix[row][column];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Math.min(...values);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,215 @@
|
||||
---
|
||||
id: 5900f3bf1000cf542c50fed2
|
||||
title: 'Problem 83: Path sum: four ways'
|
||||
challengeType: 1
|
||||
forumTopicId: 302197
|
||||
dashedName: problem-83-path-sum-four-ways
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
**Note:** This problem is a significantly more challenging version of Problem 81.
|
||||
|
||||
In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red and is equal to `2297`.
|
||||
|
||||
$$\begin{pmatrix} \color{red}{131} & 673 & \color{red}{234} & \color{red}{103} & \color{red}{18}\\\\
|
||||
\color{red}{201} & \color{red}{96} & \color{red}{342} & 965 & \color{red}{150}\\\\ 630 & 803 & 746 & \color{red}{422} & \color{red}{111}\\\\
|
||||
537 & 699 & 497 & \color{red}{121} & 956\\\\ 805 & 732 & 524 & \color{red}{37} & \color{red}{331} \end{pmatrix}$$
|
||||
|
||||
Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down in `matrix`, a 2D array representing a matrix. The maximum matrix size used in tests will be 80 by 80.
|
||||
|
||||
# --hints--
|
||||
|
||||
`pathSumFourWays(testMatrix1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof pathSumFourWays(_testMatrix1) === 'number');
|
||||
```
|
||||
|
||||
`pathSumFourWays(testMatrix1)` should return `2297`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pathSumFourWays(_testMatrix1), 2297);
|
||||
```
|
||||
|
||||
`pathSumFourWays(testMatrix2)` should return `425185`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(pathSumFourWays(_testMatrix2), 425185);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
const _testMatrix1 = [
|
||||
[131, 673, 234, 103, 18],
|
||||
[201, 96, 342, 965, 150],
|
||||
[630, 803, 746, 422, 111],
|
||||
[537, 699, 497, 121, 956],
|
||||
[805, 732, 524, 37, 331]
|
||||
];
|
||||
const _testMatrix2 = [
|
||||
[4445,2697,5115,718,2209,2212,654,4348,3079,6821,7668,3276,8874,4190,3785,2752,9473,7817,9137,496,7338,3434,7152,4355,4552,7917,7827,2460,2350,691,3514,5880,3145,7633,7199,3783,5066,7487,3285,1084,8985,760,872,8609,8051,1134,9536,5750,9716,9371,7619,5617,275,9721,2997,2698,1887,8825,6372,3014,2113,7122,7050,6775,5948,2758,1219,3539,348,7989,2735,9862,1263,8089,6401,9462,3168,2758,3748,5870],
|
||||
[1096,20,1318,7586,5167,2642,1443,5741,7621,7030,5526,4244,2348,4641,9827,2448,6918,5883,3737,300,7116,6531,567,5997,3971,6623,820,6148,3287,1874,7981,8424,7672,7575,6797,6717,1078,5008,4051,8795,5820,346,1851,6463,2117,6058,3407,8211,117,4822,1317,4377,4434,5925,8341,4800,1175,4173,690,8978,7470,1295,3799,8724,3509,9849,618,3320,7068,9633,2384,7175,544,6583,1908,9983,481,4187,9353,9377],
|
||||
[9607,7385,521,6084,1364,8983,7623,1585,6935,8551,2574,8267,4781,3834,2764,2084,2669,4656,9343,7709,2203,9328,8004,6192,5856,3555,2260,5118,6504,1839,9227,1259,9451,1388,7909,5733,6968,8519,9973,1663,5315,7571,3035,4325,4283,2304,6438,3815,9213,9806,9536,196,5542,6907,2475,1159,5820,9075,9470,2179,9248,1828,4592,9167,3713,4640,47,3637,309,7344,6955,346,378,9044,8635,7466,5036,9515,6385,9230],
|
||||
[7206,3114,7760,1094,6150,5182,7358,7387,4497,955,101,1478,7777,6966,7010,8417,6453,4955,3496,107,449,8271,131,2948,6185,784,5937,8001,6104,8282,4165,3642,710,2390,575,715,3089,6964,4217,192,5949,7006,715,3328,1152,66,8044,4319,1735,146,4818,5456,6451,4113,1063,4781,6799,602,1504,6245,6550,1417,1343,2363,3785,5448,4545,9371,5420,5068,4613,4882,4241,5043,7873,8042,8434,3939,9256,2187],
|
||||
[3620,8024,577,9997,7377,7682,1314,1158,6282,6310,1896,2509,5436,1732,9480,706,496,101,6232,7375,2207,2306,110,6772,3433,2878,8140,5933,8688,1399,2210,7332,6172,6403,7333,4044,2291,1790,2446,7390,8698,5723,3678,7104,1825,2040,140,3982,4905,4160,2200,5041,2512,1488,2268,1175,7588,8321,8078,7312,977,5257,8465,5068,3453,3096,1651,7906,253,9250,6021,8791,8109,6651,3412,345,4778,5152,4883,7505],
|
||||
[1074,5438,9008,2679,5397,5429,2652,3403,770,9188,4248,2493,4361,8327,9587,707,9525,5913,93,1899,328,2876,3604,673,8576,6908,7659,2544,3359,3883,5273,6587,3065,1749,3223,604,9925,6941,2823,8767,7039,3290,3214,1787,7904,3421,7137,9560,8451,2669,9219,6332,1576,5477,6755,8348,4164,4307,2984,4012,6629,1044,2874,6541,4942,903,1404,9125,5160,8836,4345,2581,460,8438,1538,5507,668,3352,2678,6942],
|
||||
[4295,1176,5596,1521,3061,9868,7037,7129,8933,6659,5947,5063,3653,9447,9245,2679,767,714,116,8558,163,3927,8779,158,5093,2447,5782,3967,1716,931,7772,8164,1117,9244,5783,7776,3846,8862,6014,2330,6947,1777,3112,6008,3491,1906,5952,314,4602,8994,5919,9214,3995,5026,7688,6809,5003,3128,2509,7477,110,8971,3982,8539,2980,4689,6343,5411,2992,5270,5247,9260,2269,7474,1042,7162,5206,1232,4556,4757],
|
||||
[510,3556,5377,1406,5721,4946,2635,7847,4251,8293,8281,6351,4912,287,2870,3380,3948,5322,3840,4738,9563,1906,6298,3234,8959,1562,6297,8835,7861,239,6618,1322,2553,2213,5053,5446,4402,6500,5182,8585,6900,5756,9661,903,5186,7687,5998,7997,8081,8955,4835,6069,2621,1581,732,9564,1082,1853,5442,1342,520,1737,3703,5321,4793,2776,1508,1647,9101,2499,6891,4336,7012,3329,3212,1442,9993,3988,4930,7706],
|
||||
[9444,3401,5891,9716,1228,7107,109,3563,2700,6161,5039,4992,2242,8541,7372,2067,1294,3058,1306,320,8881,5756,9326,411,8650,8824,5495,8282,8397,2000,1228,7817,2099,6473,3571,5994,4447,1299,5991,543,7874,2297,1651,101,2093,3463,9189,6872,6118,872,1008,1779,2805,9084,4048,2123,5877,55,3075,1737,9459,4535,6453,3644,108,5982,4437,5213,1340,6967,9943,5815,669,8074,1838,6979,9132,9315,715,5048],
|
||||
[3327,4030,7177,6336,9933,5296,2621,4785,2755,4832,2512,2118,2244,4407,2170,499,7532,9742,5051,7687,970,6924,3527,4694,5145,1306,2165,5940,2425,8910,3513,1909,6983,346,6377,4304,9330,7203,6605,3709,3346,970,369,9737,5811,4427,9939,3693,8436,5566,1977,3728,2399,3985,8303,2492,5366,9802,9193,7296,1033,5060,9144,2766,1151,7629,5169,5995,58,7619,7565,4208,1713,6279,3209,4908,9224,7409,1325,8540],
|
||||
[6882,1265,1775,3648,4690,959,5837,4520,5394,1378,9485,1360,4018,578,9174,2932,9890,3696,116,1723,1178,9355,7063,1594,1918,8574,7594,7942,1547,6166,7888,354,6932,4651,1010,7759,6905,661,7689,6092,9292,3845,9605,8443,443,8275,5163,7720,7265,6356,7779,1798,1754,5225,6661,1180,8024,5666,88,9153,1840,3508,1193,4445,2648,3538,6243,6375,8107,5902,5423,2520,1122,5015,6113,8859,9370,966,8673,2442],
|
||||
[7338,3423,4723,6533,848,8041,7921,8277,4094,5368,7252,8852,9166,2250,2801,6125,8093,5738,4038,9808,7359,9494,601,9116,4946,2702,5573,2921,9862,1462,1269,2410,4171,2709,7508,6241,7522,615,2407,8200,4189,5492,5649,7353,2590,5203,4274,710,7329,9063,956,8371,3722,4253,4785,1194,4828,4717,4548,940,983,2575,4511,2938,1827,2027,2700,1236,841,5760,1680,6260,2373,3851,1841,4968,1172,5179,7175,3509],
|
||||
[4420,1327,3560,2376,6260,2988,9537,4064,4829,8872,9598,3228,1792,7118,9962,9336,4368,9189,6857,1829,9863,6287,7303,7769,2707,8257,2391,2009,3975,4993,3068,9835,3427,341,8412,2134,4034,8511,6421,3041,9012,2983,7289,100,1355,7904,9186,6920,5856,2008,6545,8331,3655,5011,839,8041,9255,6524,3862,8788,62,7455,3513,5003,8413,3918,2076,7960,6108,3638,6999,3436,1441,4858,4181,1866,8731,7745,3744,1000],
|
||||
[356,8296,8325,1058,1277,4743,3850,2388,6079,6462,2815,5620,8495,5378,75,4324,3441,9870,1113,165,1544,1179,2834,562,6176,2313,6836,8839,2986,9454,5199,6888,1927,5866,8760,320,1792,8296,7898,6121,7241,5886,5814,2815,8336,1576,4314,3109,2572,6011,2086,9061,9403,3947,5487,9731,7281,3159,1819,1334,3181,5844,5114,9898,4634,2531,4412,6430,4262,8482,4546,4555,6804,2607,9421,686,8649,8860,7794,6672],
|
||||
[9870,152,1558,4963,8750,4754,6521,6256,8818,5208,5691,9659,8377,9725,5050,5343,2539,6101,1844,9700,7750,8114,5357,3001,8830,4438,199,9545,8496,43,2078,327,9397,106,6090,8181,8646,6414,7499,5450,4850,6273,5014,4131,7639,3913,6571,8534,9703,4391,7618,445,1320,5,1894,6771,7383,9191,4708,9706,6939,7937,8726,9382,5216,3685,2247,9029,8154,1738,9984,2626,9438,4167,6351,5060,29,1218,1239,4785],
|
||||
[192,5213,8297,8974,4032,6966,5717,1179,6523,4679,9513,1481,3041,5355,9303,9154,1389,8702,6589,7818,6336,3539,5538,3094,6646,6702,6266,2759,4608,4452,617,9406,8064,6379,444,5602,4950,1810,8391,1536,316,8714,1178,5182,5863,5110,5372,4954,1978,2971,5680,4863,2255,4630,5723,2168,538,1692,1319,7540,440,6430,6266,7712,7385,5702,620,641,3136,7350,1478,3155,2820,9109,6261,1122,4470,14,8493,2095],
|
||||
[1046,4301,6082,474,4974,7822,2102,5161,5172,6946,8074,9716,6586,9962,9749,5015,2217,995,5388,4402,7652,6399,6539,1349,8101,3677,1328,9612,7922,2879,231,5887,2655,508,4357,4964,3554,5930,6236,7384,4614,280,3093,9600,2110,7863,2631,6626,6620,68,1311,7198,7561,1768,5139,1431,221,230,2940,968,5283,6517,2146,1646,869,9402,7068,8645,7058,1765,9690,4152,2926,9504,2939,7504,6074,2944,6470,7859],
|
||||
[4659,736,4951,9344,1927,6271,8837,8711,3241,6579,7660,5499,5616,3743,5801,4682,9748,8796,779,1833,4549,8138,4026,775,4170,2432,4174,3741,7540,8017,2833,4027,396,811,2871,1150,9809,2719,9199,8504,1224,540,2051,3519,7982,7367,2761,308,3358,6505,2050,4836,5090,7864,805,2566,2409,6876,3361,8622,5572,5895,3280,441,7893,8105,1634,2929,274,3926,7786,6123,8233,9921,2674,5340,1445,203,4585,3837],
|
||||
[5759,338,7444,7968,7742,3755,1591,4839,1705,650,7061,2461,9230,9391,9373,2413,1213,431,7801,4994,2380,2703,6161,6878,8331,2538,6093,1275,5065,5062,2839,582,1014,8109,3525,1544,1569,8622,7944,2905,6120,1564,1839,5570,7579,1318,2677,5257,4418,5601,7935,7656,5192,1864,5886,6083,5580,6202,8869,1636,7907,4759,9082,5854,3185,7631,6854,5872,5632,5280,1431,2077,9717,7431,4256,8261,9680,4487,4752,4286],
|
||||
[1571,1428,8599,1230,7772,4221,8523,9049,4042,8726,7567,6736,9033,2104,4879,4967,6334,6716,3994,1269,8995,6539,3610,7667,6560,6065,874,848,4597,1711,7161,4811,6734,5723,6356,6026,9183,2586,5636,1092,7779,7923,8747,6887,7505,9909,1792,3233,4526,3176,1508,8043,720,5212,6046,4988,709,5277,8256,3642,1391,5803,1468,2145,3970,6301,7767,2359,8487,9771,8785,7520,856,1605,8972,2402,2386,991,1383,5963],
|
||||
[1822,4824,5957,6511,9868,4113,301,9353,6228,2881,2966,6956,9124,9574,9233,1601,7340,973,9396,540,4747,8590,9535,3650,7333,7583,4806,3593,2738,8157,5215,8472,2284,9473,3906,6982,5505,6053,7936,6074,7179,6688,1564,1103,6860,5839,2022,8490,910,7551,7805,881,7024,1855,9448,4790,1274,3672,2810,774,7623,4223,4850,6071,9975,4935,1915,9771,6690,3846,517,463,7624,4511,614,6394,3661,7409,1395,8127],
|
||||
[8738,3850,9555,3695,4383,2378,87,6256,6740,7682,9546,4255,6105,2000,1851,4073,8957,9022,6547,5189,2487,303,9602,7833,1628,4163,6678,3144,8589,7096,8913,5823,4890,7679,1212,9294,5884,2972,3012,3359,7794,7428,1579,4350,7246,4301,7779,7790,3294,9547,4367,3549,1958,8237,6758,3497,3250,3456,6318,1663,708,7714,6143,6890,3428,6853,9334,7992,591,6449,9786,1412,8500,722,5468,1371,108,3939,4199,2535],
|
||||
[7047,4323,1934,5163,4166,461,3544,2767,6554,203,6098,2265,9078,2075,4644,6641,8412,9183,487,101,7566,5622,1975,5726,2920,5374,7779,5631,3753,3725,2672,3621,4280,1162,5812,345,8173,9785,1525,955,5603,2215,2580,5261,2765,2990,5979,389,3907,2484,1232,5933,5871,3304,1138,1616,5114,9199,5072,7442,7245,6472,4760,6359,9053,7876,2564,9404,3043,9026,2261,3374,4460,7306,2326,966,828,3274,1712,3446],
|
||||
[3975,4565,8131,5800,4570,2306,8838,4392,9147,11,3911,7118,9645,4994,2028,6062,5431,2279,8752,2658,7836,994,7316,5336,7185,3289,1898,9689,2331,5737,3403,1124,2679,3241,7748,16,2724,5441,6640,9368,9081,5618,858,4969,17,2103,6035,8043,7475,2181,939,415,1617,8500,8253,2155,7843,7974,7859,1746,6336,3193,2617,8736,4079,6324,6645,8891,9396,5522,6103,1857,8979,3835,2475,1310,7422,610,8345,7615],
|
||||
[9248,5397,5686,2988,3446,4359,6634,9141,497,9176,6773,7448,1907,8454,916,1596,2241,1626,1384,2741,3649,5362,8791,7170,2903,2475,5325,6451,924,3328,522,90,4813,9737,9557,691,2388,1383,4021,1609,9206,4707,5200,7107,8104,4333,9860,5013,1224,6959,8527,1877,4545,7772,6268,621,4915,9349,5970,706,9583,3071,4127,780,8231,3017,9114,3836,7503,2383,1977,4870,8035,2379,9704,1037,3992,3642,1016,4303],
|
||||
[5093,138,4639,6609,1146,5565,95,7521,9077,2272,974,4388,2465,2650,722,4998,3567,3047,921,2736,7855,173,2065,4238,1048,5,6847,9548,8632,9194,5942,4777,7910,8971,6279,7253,2516,1555,1833,3184,9453,9053,6897,7808,8629,4877,1871,8055,4881,7639,1537,7701,2508,7564,5845,5023,2304,5396,3193,2955,1088,3801,6203,1748,3737,1276,13,4120,7715,8552,3047,2921,106,7508,304,1280,7140,2567,9135,5266],
|
||||
[6237,4607,7527,9047,522,7371,4883,2540,5867,6366,5301,1570,421,276,3361,527,6637,4861,2401,7522,5808,9371,5298,2045,5096,5447,7755,5115,7060,8529,4078,1943,1697,1764,5453,7085,960,2405,739,2100,5800,728,9737,5704,5693,1431,8979,6428,673,7540,6,7773,5857,6823,150,5869,8486,684,5816,9626,7451,5579,8260,3397,5322,6920,1879,2127,2884,5478,4977,9016,6165,6292,3062,5671,5968,78,4619,4763],
|
||||
[9905,7127,9390,5185,6923,3721,9164,9705,4341,1031,1046,5127,7376,6528,3248,4941,1178,7889,3364,4486,5358,9402,9158,8600,1025,874,1839,1783,309,9030,1843,845,8398,1433,7118,70,8071,2877,3904,8866,6722,4299,10,1929,5897,4188,600,1889,3325,2485,6473,4474,7444,6992,4846,6166,4441,2283,2629,4352,7775,1101,2214,9985,215,8270,9750,2740,8361,7103,5930,8664,9690,8302,9267,344,2077,1372,1880,9550],
|
||||
[5825,8517,7769,2405,8204,1060,3603,7025,478,8334,1997,3692,7433,9101,7294,7498,9415,5452,3850,3508,6857,9213,6807,4412,7310,854,5384,686,4978,892,8651,3241,2743,3801,3813,8588,6701,4416,6990,6490,3197,6838,6503,114,8343,5844,8646,8694,65,791,5979,2687,2621,2019,8097,1423,3644,9764,4921,3266,3662,5561,2476,8271,8138,6147,1168,3340,1998,9874,6572,9873,6659,5609,2711,3931,9567,4143,7833,8887],
|
||||
[6223,2099,2700,589,4716,8333,1362,5007,2753,2848,4441,8397,7192,8191,4916,9955,6076,3370,6396,6971,3156,248,3911,2488,4930,2458,7183,5455,170,6809,6417,3390,1956,7188,577,7526,2203,968,8164,479,8699,7915,507,6393,4632,1597,7534,3604,618,3280,6061,9793,9238,8347,568,9645,2070,5198,6482,5000,9212,6655,5961,7513,1323,3872,6170,3812,4146,2736,67,3151,5548,2781,9679,7564,5043,8587,1893,4531],
|
||||
[5826,3690,6724,2121,9308,6986,8106,6659,2142,1642,7170,2877,5757,6494,8026,6571,8387,9961,6043,9758,9607,6450,8631,8334,7359,5256,8523,2225,7487,1977,9555,8048,5763,2414,4948,4265,2427,8978,8088,8841,9208,9601,5810,9398,8866,9138,4176,5875,7212,3272,6759,5678,7649,4922,5422,1343,8197,3154,3600,687,1028,4579,2084,9467,4492,7262,7296,6538,7657,7134,2077,1505,7332,6890,8964,4879,7603,7400,5973,739],
|
||||
[1861,1613,4879,1884,7334,966,2000,7489,2123,4287,1472,3263,4726,9203,1040,4103,6075,6049,330,9253,4062,4268,1635,9960,577,1320,3195,9628,1030,4092,4979,6474,6393,2799,6967,8687,7724,7392,9927,2085,3200,6466,8702,265,7646,8665,7986,7266,4574,6587,612,2724,704,3191,8323,9523,3002,704,5064,3960,8209,2027,2758,8393,4875,4641,9584,6401,7883,7014,768,443,5490,7506,1852,2005,8850,5776,4487,4269],
|
||||
[4052,6687,4705,7260,6645,6715,3706,5504,8672,2853,1136,8187,8203,4016,871,1809,1366,4952,9294,5339,6872,2645,6083,7874,3056,5218,7485,8796,7401,3348,2103,426,8572,4163,9171,3176,948,7654,9344,3217,1650,5580,7971,2622,76,2874,880,2034,9929,1546,2659,5811,3754,7096,7436,9694,9960,7415,2164,953,2360,4194,2397,1047,2196,6827,575,784,2675,8821,6802,7972,5996,6699,2134,7577,2887,1412,4349,4380],
|
||||
[4629,2234,6240,8132,7592,3181,6389,1214,266,1910,2451,8784,2790,1127,6932,1447,8986,2492,5476,397,889,3027,7641,5083,5776,4022,185,3364,5701,2442,2840,4160,9525,4828,6602,2614,7447,3711,4505,7745,8034,6514,4907,2605,7753,6958,7270,6936,3006,8968,439,2326,4652,3085,3425,9863,5049,5361,8688,297,7580,8777,7916,6687,8683,7141,306,9569,2384,1500,3346,4601,7329,9040,6097,2727,6314,4501,4974,2829],
|
||||
[8316,4072,2025,6884,3027,1808,5714,7624,7880,8528,4205,8686,7587,3230,1139,7273,6163,6986,3914,9309,1464,9359,4474,7095,2212,7302,2583,9462,7532,6567,1606,4436,8981,5612,6796,4385,5076,2007,6072,3678,8331,1338,3299,8845,4783,8613,4071,1232,6028,2176,3990,2148,3748,103,9453,538,6745,9110,926,3125,473,5970,8728,7072,9062,1404,1317,5139,9862,6496,6062,3338,464,1600,2532,1088,8232,7739,8274,3873],
|
||||
[2341,523,7096,8397,8301,6541,9844,244,4993,2280,7689,4025,4196,5522,7904,6048,2623,9258,2149,9461,6448,8087,7245,1917,8340,7127,8466,5725,6996,3421,5313,512,9164,9837,9794,8369,4185,1488,7210,1524,1016,4620,9435,2478,7765,8035,697,6677,3724,6988,5853,7662,3895,9593,1185,4727,6025,5734,7665,3070,138,8469,6748,6459,561,7935,8646,2378,462,7755,3115,9690,8877,3946,2728,8793,244,6323,8666,4271],
|
||||
[6430,2406,8994,56,1267,3826,9443,7079,7579,5232,6691,3435,6718,5698,4144,7028,592,2627,217,734,6194,8156,9118,58,2640,8069,4127,3285,694,3197,3377,4143,4802,3324,8134,6953,7625,3598,3584,4289,7065,3434,2106,7132,5802,7920,9060,7531,3321,1725,1067,3751,444,5503,6785,7937,6365,4803,198,6266,8177,1470,6390,1606,2904,7555,9834,8667,2033,1723,5167,1666,8546,8152,473,4475,6451,7947,3062,3281],
|
||||
[2810,3042,7759,1741,2275,2609,7676,8640,4117,1958,7500,8048,1757,3954,9270,1971,4796,2912,660,5511,3553,1012,5757,4525,6084,7198,8352,5775,7726,8591,7710,9589,3122,4392,6856,5016,749,2285,3356,7482,9956,7348,2599,8944,495,3462,3578,551,4543,7207,7169,7796,1247,4278,6916,8176,3742,8385,2310,1345,8692,2667,4568,1770,8319,3585,4920,3890,4928,7343,5385,9772,7947,8786,2056,9266,3454,2807,877,2660],
|
||||
[6206,8252,5928,5837,4177,4333,207,7934,5581,9526,8906,1498,8411,2984,5198,5134,2464,8435,8514,8674,3876,599,5327,826,2152,4084,2433,9327,9697,4800,2728,3608,3849,3861,3498,9943,1407,3991,7191,9110,5666,8434,4704,6545,5944,2357,1163,4995,9619,6754,4200,9682,6654,4862,4744,5953,6632,1054,293,9439,8286,2255,696,8709,1533,1844,6441,430,1999,6063,9431,7018,8057,2920,6266,6799,356,3597,4024,6665],
|
||||
[3847,6356,8541,7225,2325,2946,5199,469,5450,7508,2197,9915,8284,7983,6341,3276,3321,16,1321,7608,5015,3362,8491,6968,6818,797,156,2575,706,9516,5344,5457,9210,5051,8099,1617,9951,7663,8253,9683,2670,1261,4710,1068,8753,4799,1228,2621,3275,6188,4699,1791,9518,8701,5932,4275,6011,9877,2933,4182,6059,2930,6687,6682,9771,654,9437,3169,8596,1827,5471,8909,2352,123,4394,3208,8756,5513,6917,2056],
|
||||
[5458,8173,3138,3290,4570,4892,3317,4251,9699,7973,1163,1935,5477,6648,9614,5655,9592,975,9118,2194,7322,8248,8413,3462,8560,1907,7810,6650,7355,2939,4973,6894,3933,3784,3200,2419,9234,4747,2208,2207,1945,2899,1407,6145,8023,3484,5688,7686,2737,3828,3704,9004,5190,9740,8643,8650,5358,4426,1522,1707,3613,9887,6956,2447,2762,833,1449,9489,2573,1080,4167,3456,6809,2466,227,7125,2759,6250,6472,8089],
|
||||
[3266,7025,9756,3914,1265,9116,7723,9788,6805,5493,2092,8688,6592,9173,4431,4028,6007,7131,4446,4815,3648,6701,759,3312,8355,4485,4187,5188,8746,7759,3528,2177,5243,8379,3838,7233,4607,9187,7216,2190,6967,2920,6082,7910,5354,3609,8958,6949,7731,494,8753,8707,1523,4426,3543,7085,647,6771,9847,646,5049,824,8417,5260,2730,5702,2513,9275,4279,2767,8684,1165,9903,4518,55,9682,8963,6005,2102,6523],
|
||||
[1998,8731,936,1479,5259,7064,4085,91,7745,7136,3773,3810,730,8255,2705,2653,9790,6807,2342,355,9344,2668,3690,2028,9679,8102,574,4318,6481,9175,5423,8062,2867,9657,7553,3442,3920,7430,3945,7639,3714,3392,2525,4995,4850,2867,7951,9667,486,9506,9888,781,8866,1702,3795,90,356,1483,4200,2131,6969,5931,486,6880,4404,1084,5169,4910,6567,8335,4686,5043,2614,3352,2667,4513,6472,7471,5720,1616],
|
||||
[8878,1613,1716,868,1906,2681,564,665,5995,2474,7496,3432,9491,9087,8850,8287,669,823,347,6194,2264,2592,7871,7616,8508,4827,760,2676,4660,4881,7572,3811,9032,939,4384,929,7525,8419,5556,9063,662,8887,7026,8534,3111,1454,2082,7598,5726,6687,9647,7608,73,3014,5063,670,5461,5631,3367,9796,8475,7908,5073,1565,5008,5295,4457,1274,4788,1728,338,600,8415,8535,9351,7750,6887,5845,1741,125],
|
||||
[3637,6489,9634,9464,9055,2413,7824,9517,7532,3577,7050,6186,6980,9365,9782,191,870,2497,8498,2218,2757,5420,6468,586,3320,9230,1034,1393,9886,5072,9391,1178,8464,8042,6869,2075,8275,3601,7715,9470,8786,6475,8373,2159,9237,2066,3264,5000,679,355,3069,4073,494,2308,5512,4334,9438,8786,8637,9774,1169,1949,6594,6072,4270,9158,7916,5752,6794,9391,6301,5842,3285,2141,3898,8027,4310,8821,7079,1307],
|
||||
[8497,6681,4732,7151,7060,5204,9030,7157,833,5014,8723,3207,9796,9286,4913,119,5118,7650,9335,809,3675,2597,5144,3945,5090,8384,187,4102,1260,2445,2792,4422,8389,9290,50,1765,1521,6921,8586,4368,1565,5727,7855,2003,4834,9897,5911,8630,5070,1330,7692,7557,7980,6028,5805,9090,8265,3019,3802,698,9149,5748,1965,9658,4417,5994,5584,8226,2937,272,5743,1278,5698,8736,2595,6475,5342,6596,1149,6920],
|
||||
[8188,8009,9546,6310,8772,2500,9846,6592,6872,3857,1307,8125,7042,1544,6159,2330,643,4604,7899,6848,371,8067,2062,3200,7295,1857,9505,6936,384,2193,2190,301,8535,5503,1462,7380,5114,4824,8833,1763,4974,8711,9262,6698,3999,2645,6937,7747,1128,2933,3556,7943,2885,3122,9105,5447,418,2899,5148,3699,9021,9501,597,4084,175,1621,1,1079,6067,5812,4326,9914,6633,5394,4233,6728,9084,1864,5863,1225],
|
||||
[9935,8793,9117,1825,9542,8246,8437,3331,9128,9675,6086,7075,319,1334,7932,3583,7167,4178,1726,7720,695,8277,7887,6359,5912,1719,2780,8529,1359,2013,4498,8072,1129,9998,1147,8804,9405,6255,1619,2165,7491,1,8882,7378,3337,503,5758,4109,3577,985,3200,7615,8058,5032,1080,6410,6873,5496,1466,2412,9885,5904,4406,3605,8770,4361,6205,9193,1537,9959,214,7260,9566,1685,100,4920,7138,9819,5637,976],
|
||||
[3466,9854,985,1078,7222,8888,5466,5379,3578,4540,6853,8690,3728,6351,7147,3134,6921,9692,857,3307,4998,2172,5783,3931,9417,2541,6299,13,787,2099,9131,9494,896,8600,1643,8419,7248,2660,2609,8579,91,6663,5506,7675,1947,6165,4286,1972,9645,3805,1663,1456,8853,5705,9889,7489,1107,383,4044,2969,3343,152,7805,4980,9929,5033,1737,9953,7197,9158,4071,1324,473,9676,3984,9680,3606,8160,7384,5432],
|
||||
[1005,4512,5186,3953,2164,3372,4097,3247,8697,3022,9896,4101,3871,6791,3219,2742,4630,6967,7829,5991,6134,1197,1414,8923,8787,1394,8852,5019,7768,5147,8004,8825,5062,9625,7988,1110,3992,7984,9966,6516,6251,8270,421,3723,1432,4830,6935,8095,9059,2214,6483,6846,3120,1587,6201,6691,9096,9627,6671,4002,3495,9939,7708,7465,5879,6959,6634,3241,3401,2355,9061,2611,7830,3941,2177,2146,5089,7079,519,6351],
|
||||
[7280,8586,4261,2831,7217,3141,9994,9940,5462,2189,4005,6942,9848,5350,8060,6665,7519,4324,7684,657,9453,9296,2944,6843,7499,7847,1728,9681,3906,6353,5529,2822,3355,3897,7724,4257,7489,8672,4356,3983,1948,6892,7415,4153,5893,4190,621,1736,4045,9532,7701,3671,1211,1622,3176,4524,9317,7800,5638,6644,6943,5463,3531,2821,1347,5958,3436,1438,2999,994,850,4131,2616,1549,3465,5946,690,9273,6954,7991],
|
||||
[9517,399,3249,2596,7736,2142,1322,968,7350,1614,468,3346,3265,7222,6086,1661,5317,2582,7959,4685,2807,2917,1037,5698,1529,3972,8716,2634,3301,3412,8621,743,8001,4734,888,7744,8092,3671,8941,1487,5658,7099,2781,99,1932,4443,4756,4652,9328,1581,7855,4312,5976,7255,6480,3996,2748,1973,9731,4530,2790,9417,7186,5303,3557,351,7182,9428,1342,9020,7599,1392,8304,2070,9138,7215,2008,9937,1106,7110],
|
||||
[7444,769,9688,632,1571,6820,8743,4338,337,3366,3073,1946,8219,104,4210,6986,249,5061,8693,7960,6546,1004,8857,5997,9352,4338,6105,5008,2556,6518,6694,4345,3727,7956,20,3954,8652,4424,9387,2035,8358,5962,5304,5194,8650,8282,1256,1103,2138,6679,1985,3653,2770,2433,4278,615,2863,1715,242,3790,2636,6998,3088,1671,2239,957,5411,4595,6282,2881,9974,2401,875,7574,2987,4587,3147,6766,9885,2965],
|
||||
[3287,3016,3619,6818,9073,6120,5423,557,2900,2015,8111,3873,1314,4189,1846,4399,7041,7583,2427,2864,3525,5002,2069,748,1948,6015,2684,438,770,8367,1663,7887,7759,1885,157,7770,4520,4878,3857,1137,3525,3050,6276,5569,7649,904,4533,7843,2199,5648,7628,9075,9441,3600,7231,2388,5640,9096,958,3058,584,5899,8150,1181,9616,1098,8162,6819,8171,1519,1140,7665,8801,2632,1299,9192,707,9955,2710,7314],
|
||||
[1772,2963,7578,3541,3095,1488,7026,2634,6015,4633,4370,2762,1650,2174,909,8158,2922,8467,4198,4280,9092,8856,8835,5457,2790,8574,9742,5054,9547,4156,7940,8126,9824,7340,8840,6574,3547,1477,3014,6798,7134,435,9484,9859,3031,4,1502,4133,1738,1807,4825,463,6343,9701,8506,9822,9555,8688,8168,3467,3234,6318,1787,5591,419,6593,7974,8486,9861,6381,6758,194,3061,4315,2863,4665,3789,2201,1492,4416],
|
||||
[126,8927,6608,5682,8986,6867,1715,6076,3159,788,3140,4744,830,9253,5812,5021,7616,8534,1546,9590,1101,9012,9821,8132,7857,4086,1069,7491,2988,1579,2442,4321,2149,7642,6108,250,6086,3167,24,9528,7663,2685,1220,9196,1397,5776,1577,1730,5481,977,6115,199,6326,2183,3767,5928,5586,7561,663,8649,9688,949,5913,9160,1870,5764,9887,4477,6703,1413,4995,5494,7131,2192,8969,7138,3997,8697,646,1028],
|
||||
[8074,1731,8245,624,4601,8706,155,8891,309,2552,8208,8452,2954,3124,3469,4246,3352,1105,4509,8677,9901,4416,8191,9283,5625,7120,2952,8881,7693,830,4580,8228,9459,8611,4499,1179,4988,1394,550,2336,6089,6872,269,7213,1848,917,6672,4890,656,1478,6536,3165,4743,4990,1176,6211,7207,5284,9730,4738,1549,4986,4942,8645,3698,9429,1439,2175,6549,3058,6513,1574,6988,8333,3406,5245,5431,7140,7085,6407],
|
||||
[7845,4694,2530,8249,290,5948,5509,1588,5940,4495,5866,5021,4626,3979,3296,7589,4854,1998,5627,3926,8346,6512,9608,1918,7070,4747,4182,2858,2766,4606,6269,4107,8982,8568,9053,4244,5604,102,2756,727,5887,2566,7922,44,5986,621,1202,374,6988,4130,3627,6744,9443,4568,1398,8679,397,3928,9159,367,2917,6127,5788,3304,8129,911,2669,1463,9749,264,4478,8940,1109,7309,2462,117,4692,7724,225,2312],
|
||||
[4164,3637,2000,941,8903,39,3443,7172,1031,3687,4901,8082,4945,4515,7204,9310,9349,9535,9940,218,1788,9245,2237,1541,5670,6538,6047,5553,9807,8101,1925,8714,445,8332,7309,6830,5786,5736,7306,2710,3034,1838,7969,6318,7912,2584,2080,7437,6705,2254,7428,820,782,9861,7596,3842,3631,8063,5240,6666,394,4565,7865,4895,9890,6028,6117,4724,9156,4473,4552,602,470,6191,4927,5387,884,3146,1978,3000],
|
||||
[4258,6880,1696,3582,5793,4923,2119,1155,9056,9698,6603,3768,5514,9927,9609,6166,6566,4536,4985,4934,8076,9062,6741,6163,7399,4562,2337,5600,2919,9012,8459,1308,6072,1225,9306,8818,5886,7243,7365,8792,6007,9256,6699,7171,4230,7002,8720,7839,4533,1671,478,7774,1607,2317,5437,4705,7886,4760,6760,7271,3081,2997,3088,7675,6208,3101,6821,6840,122,9633,4900,2067,8546,4549,2091,7188,5605,8599,6758,5229],
|
||||
[7854,5243,9155,3556,8812,7047,2202,1541,5993,4600,4760,713,434,7911,7426,7414,8729,322,803,7960,7563,4908,6285,6291,736,3389,9339,4132,8701,7534,5287,3646,592,3065,7582,2592,8755,6068,8597,1982,5782,1894,2900,6236,4039,6569,3037,5837,7698,700,7815,2491,7272,5878,3083,6778,6639,3589,5010,8313,2581,6617,5869,8402,6808,2951,2321,5195,497,2190,6187,1342,1316,4453,7740,4154,2959,1781,1482,8256],
|
||||
[7178,2046,4419,744,8312,5356,6855,8839,319,2962,5662,47,6307,8662,68,4813,567,2712,9931,1678,3101,8227,6533,4933,6656,92,5846,4780,6256,6361,4323,9985,1231,2175,7178,3034,9744,6155,9165,7787,5836,9318,7860,9644,8941,6480,9443,8188,5928,161,6979,2352,5628,6991,1198,8067,5867,6620,3778,8426,2994,3122,3124,6335,3918,8897,2655,9670,634,1088,1576,8935,7255,474,8166,7417,9547,2886,5560,3842],
|
||||
[6957,3111,26,7530,7143,1295,1744,6057,3009,1854,8098,5405,2234,4874,9447,2620,9303,27,7410,969,40,2966,5648,7596,8637,4238,3143,3679,7187,690,9980,7085,7714,9373,5632,7526,6707,3951,9734,4216,2146,3602,5371,6029,3039,4433,4855,4151,1449,3376,8009,7240,7027,4602,2947,9081,4045,8424,9352,8742,923,2705,4266,3232,2264,6761,363,2651,3383,7770,6730,7856,7340,9679,2158,610,4471,4608,910,6241],
|
||||
[4417,6756,1013,8797,658,8809,5032,8703,7541,846,3357,2920,9817,1745,9980,7593,4667,3087,779,3218,6233,5568,4296,2289,2654,7898,5021,9461,5593,8214,9173,4203,2271,7980,2983,5952,9992,8399,3468,1776,3188,9314,1720,6523,2933,621,8685,5483,8986,6163,3444,9539,4320,155,3992,2828,2150,6071,524,2895,5468,8063,1210,3348,9071,4862,483,9017,4097,6186,9815,3610,5048,1644,1003,9865,9332,2145,1944,2213],
|
||||
[9284,3803,4920,1927,6706,4344,7383,4786,9890,2010,5228,1224,3158,6967,8580,8990,8883,5213,76,8306,2031,4980,5639,9519,7184,5645,7769,3259,8077,9130,1317,3096,9624,3818,1770,695,2454,947,6029,3474,9938,3527,5696,4760,7724,7738,2848,6442,5767,6845,8323,4131,2859,7595,2500,4815,3660,9130,8580,7016,8231,4391,8369,3444,4069,4021,556,6154,627,2778,1496,4206,6356,8434,8491,3816,8231,3190,5575,1015],
|
||||
[3787,7572,1788,6803,5641,6844,1961,4811,8535,9914,9999,1450,8857,738,4662,8569,6679,2225,7839,8618,286,2648,5342,2294,3205,4546,176,8705,3741,6134,8324,8021,7004,5205,7032,6637,9442,5539,5584,4819,5874,5807,8589,6871,9016,983,1758,3786,1519,6241,185,8398,495,3370,9133,3051,4549,9674,7311,9738,3316,9383,2658,2776,9481,7558,619,3943,3324,6491,4933,153,9738,4623,912,3595,7771,7939,1219,4405],
|
||||
[2650,3883,4154,5809,315,7756,4430,1788,4451,1631,6461,7230,6017,5751,138,588,5282,2442,9110,9035,6349,2515,1570,6122,4192,4174,3530,1933,4186,4420,4609,5739,4135,2963,6308,1161,8809,8619,2796,3819,6971,8228,4188,1492,909,8048,2328,6772,8467,7671,9068,2226,7579,6422,7056,8042,3296,2272,3006,2196,7320,3238,3490,3102,37,1293,3212,4767,5041,8773,5794,4456,6174,7279,7054,2835,7053,9088,790,6640],
|
||||
[3101,1057,7057,3826,6077,1025,2955,1224,1114,6729,5902,4698,6239,7203,9423,1804,4417,6686,1426,6941,8071,1029,4985,9010,6122,6597,1622,1574,3513,1684,7086,5505,3244,411,9638,4150,907,9135,829,981,1707,5359,8781,9751,5,9131,3973,7159,1340,6955,7514,7993,6964,8198,1933,2797,877,3993,4453,8020,9349,8646,2779,8679,2961,3547,3374,3510,1129,3568,2241,2625,9138,5974,8206,7669,7678,1833,8700,4480],
|
||||
[4865,9912,8038,8238,782,3095,8199,1127,4501,7280,2112,2487,3626,2790,9432,1475,6312,8277,4827,2218,5806,7132,8752,1468,7471,6386,739,8762,8323,8120,5169,9078,9058,3370,9560,7987,8585,8531,5347,9312,1058,4271,1159,5286,5404,6925,8606,9204,7361,2415,560,586,4002,2644,1927,2824,768,4409,2942,3345,1002,808,4941,6267,7979,5140,8643,7553,9438,7320,4938,2666,4609,2778,8158,6730,3748,3867,1866,7181],
|
||||
[171,3771,7134,8927,4778,2913,3326,2004,3089,7853,1378,1729,4777,2706,9578,1360,5693,3036,1851,7248,2403,2273,8536,6501,9216,613,9671,7131,7719,6425,773,717,8803,160,1114,7554,7197,753,4513,4322,8499,4533,2609,4226,8710,6627,644,9666,6260,4870,5744,7385,6542,6203,7703,6130,8944,5589,2262,6803,6381,7414,6888,5123,7320,9392,9061,6780,322,8975,7050,5089,1061,2260,3199,1150,1865,5386,9699,6501],
|
||||
[3744,8454,6885,8277,919,1923,4001,6864,7854,5519,2491,6057,8794,9645,1776,5714,9786,9281,7538,6916,3215,395,2501,9618,4835,8846,9708,2813,3303,1794,8309,7176,2206,1602,1838,236,4593,2245,8993,4017,10,8215,6921,5206,4023,5932,6997,7801,262,7640,3107,8275,4938,7822,2425,3223,3886,2105,8700,9526,2088,8662,8034,7004,5710,2124,7164,3574,6630,9980,4242,2901,9471,1491,2117,4562,1130,9086,4117,6698],
|
||||
[2810,2280,2331,1170,4554,4071,8387,1215,2274,9848,6738,1604,7281,8805,439,1298,8318,7834,9426,8603,6092,7944,1309,8828,303,3157,4638,4439,9175,1921,4695,7716,1494,1015,1772,5913,1127,1952,1950,8905,4064,9890,385,9357,7945,5035,7082,5369,4093,6546,5187,5637,2041,8946,1758,7111,6566,1027,1049,5148,7224,7248,296,6169,375,1656,7993,2816,3717,4279,4675,1609,3317,42,6201,3100,3144,163,9530,4531],
|
||||
[7096,6070,1009,4988,3538,5801,7149,3063,2324,2912,7911,7002,4338,7880,2481,7368,3516,2016,7556,2193,1388,3865,8125,4637,4096,8114,750,3144,1938,7002,9343,4095,1392,4220,3455,6969,9647,1321,9048,1996,1640,6626,1788,314,9578,6630,2813,6626,4981,9908,7024,4355,3201,3521,3864,3303,464,1923,595,9801,3391,8366,8084,9374,1041,8807,9085,1892,9431,8317,9016,9221,8574,9981,9240,5395,2009,6310,2854,9255],
|
||||
[8830,3145,2960,9615,8220,6061,3452,2918,6481,9278,2297,3385,6565,7066,7316,5682,107,7646,4466,68,1952,9603,8615,54,7191,791,6833,2560,693,9733,4168,570,9127,9537,1925,8287,5508,4297,8452,8795,6213,7994,2420,4208,524,5915,8602,8330,2651,8547,6156,1812,6271,7991,9407,9804,1553,6866,1128,2119,4691,9711,8315,5879,9935,6900,482,682,4126,1041,428,6247,3720,5882,7526,2582,4327,7725,3503,2631],
|
||||
[2738,9323,721,7434,1453,6294,2957,3786,5722,6019,8685,4386,3066,9057,6860,499,5315,3045,5194,7111,3137,9104,941,586,3066,755,4177,8819,7040,5309,3583,3897,4428,7788,4721,7249,6559,7324,825,7311,3760,6064,6070,9672,4882,584,1365,9739,9331,5783,2624,7889,1604,1303,1555,7125,8312,425,8936,3233,7724,1480,403,7440,1784,1754,4721,1569,652,3893,4574,5692,9730,4813,9844,8291,9199,7101,3391,8914],
|
||||
[6044,2928,9332,3328,8588,447,3830,1176,3523,2705,8365,6136,5442,9049,5526,8575,8869,9031,7280,706,2794,8814,5767,4241,7696,78,6570,556,5083,1426,4502,3336,9518,2292,1885,3740,3153,9348,9331,8051,2759,5407,9028,7840,9255,831,515,2612,9747,7435,8964,4971,2048,4900,5967,8271,1719,9670,2810,6777,1594,6367,6259,8316,3815,1689,6840,9437,4361,822,9619,3065,83,6344,7486,8657,8228,9635,6932,4864],
|
||||
[8478,4777,6334,4678,7476,4963,6735,3096,5860,1405,5127,7269,7793,4738,227,9168,2996,8928,765,733,1276,7677,6258,1528,9558,3329,302,8901,1422,8277,6340,645,9125,8869,5952,141,8141,1816,9635,4025,4184,3093,83,2344,2747,9352,7966,1206,1126,1826,218,7939,2957,2729,810,8752,5247,4174,4038,8884,7899,9567,301,5265,5752,7524,4381,1669,3106,8270,6228,6373,754,2547,4240,2313,5514,3022,1040,9738],
|
||||
[2265,8192,1763,1369,8469,8789,4836,52,1212,6690,5257,8918,6723,6319,378,4039,2421,8555,8184,9577,1432,7139,8078,5452,9628,7579,4161,7490,5159,8559,1011,81,478,5840,1964,1334,6875,8670,9900,739,1514,8692,522,9316,6955,1345,8132,2277,3193,9773,3923,4177,2183,1236,6747,6575,4874,6003,6409,8187,745,8776,9440,7543,9825,2582,7381,8147,7236,5185,7564,6125,218,7991,6394,391,7659,7456,5128,5294],
|
||||
[2132,8992,8160,5782,4420,3371,3798,5054,552,5631,7546,4716,1332,6486,7892,7441,4370,6231,4579,2121,8615,1145,9391,1524,1385,2400,9437,2454,7896,7467,2928,8400,3299,4025,7458,4703,7206,6358,792,6200,725,4275,4136,7390,5984,4502,7929,5085,8176,4600,119,3568,76,9363,6943,2248,9077,9731,6213,5817,6729,4190,3092,6910,759,2682,8380,1254,9604,3011,9291,5329,9453,9746,2739,6522,3765,5634,1113,5789],
|
||||
[5304,5499,564,2801,679,2653,1783,3608,7359,7797,3284,796,3222,437,7185,6135,8571,2778,7488,5746,678,6140,861,7750,803,9859,9918,2425,3734,2698,9005,4864,9818,6743,2475,132,9486,3825,5472,919,292,4411,7213,7699,6435,9019,6769,1388,802,2124,1345,8493,9487,8558,7061,8777,8833,2427,2238,5409,4957,8503,3171,7622,5779,6145,2417,5873,5563,5693,9574,9491,1937,7384,4563,6842,5432,2751,3406,7981]
|
||||
];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pathSumFourWays(matrix) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
const testMatrix1 = [
|
||||
[131, 673, 234, 103, 18],
|
||||
[201, 96, 342, 965, 150],
|
||||
[630, 803, 746, 422, 111],
|
||||
[537, 699, 497, 121, 956],
|
||||
[805, 732, 524, 37, 331]
|
||||
];
|
||||
|
||||
pathSumFourWays(testMatrix1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pathSumFourWays(matrix) {
|
||||
const MOVES = [
|
||||
[-1, 0],
|
||||
[1, 0],
|
||||
[0, -1],
|
||||
[0, 1]
|
||||
];
|
||||
|
||||
function getAllowedMoves(row, col) {
|
||||
const moves = [];
|
||||
for (let i = 0; i < MOVES.length; i++) {
|
||||
const [rowChange, colChange] = MOVES[i];
|
||||
const nextRow = row + rowChange;
|
||||
const nextCol = col + colChange;
|
||||
if (isMoveAllowed(nextRow, nextCol)) {
|
||||
moves.push([nextRow, nextCol]);
|
||||
}
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
function isMoveAllowed(nextRow, nextCol) {
|
||||
if (nextRow < 0 || nextRow >= size || nextCol < 0 || nextCol >= size) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const size = matrix.length;
|
||||
const resultMatrix = new Array(size)
|
||||
.fill()
|
||||
.map(() => new Array(size).fill(Infinity));
|
||||
|
||||
const [startRow, startCol] = new Array(2).fill(size - 1);
|
||||
const queue = [[startRow, startCol]];
|
||||
resultMatrix[startRow][startCol] = matrix[startRow][startCol];
|
||||
|
||||
while (queue.length > 0) {
|
||||
const [curRow, curCol] = queue.shift();
|
||||
|
||||
const allowedMoves = getAllowedMoves(curRow, curCol);
|
||||
|
||||
for (let i = 0; i < allowedMoves.length; i++) {
|
||||
const [nextRow, nextCol] = allowedMoves[i];
|
||||
const bestPath = resultMatrix[nextRow][nextCol];
|
||||
const newPath = resultMatrix[curRow][curCol] + matrix[nextRow][nextCol];
|
||||
if (newPath < bestPath) {
|
||||
resultMatrix[nextRow][nextCol] = newPath;
|
||||
queue.push(allowedMoves[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return resultMatrix[0][0];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,268 @@
|
||||
---
|
||||
id: 5900f3c11000cf542c50fed3
|
||||
title: 'Problem 84: Monopoly odds'
|
||||
challengeType: 1
|
||||
forumTopicId: 302198
|
||||
dashedName: problem-84-monopoly-odds
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the game, *Monopoly*, the standard board is set up in the following way:
|
||||
|
||||
<div style="text-align: center;">
|
||||
<table cellspacing="1" cellpadding="5" border="0" style="background-color: black; color: black;" align="center">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">GO</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">A1</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">CC1</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">A2</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">T1</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">R1</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">B1</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">CH1</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">B2</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">B3</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">JAIL</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">H2</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">C1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">T2</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">U1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">H1</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">C2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">CH3</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">C3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">R4</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">R2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">G3</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">D1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">CC3</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">CC2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">G2</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">D2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">G1</td>
|
||||
<td colspan="9"> </td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">D3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">G2J</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">F3</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">U2</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">F2</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">F1</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">R3</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">E3</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">E2</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">CH2</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">E1</td>
|
||||
<td style="background-color: #ffffff; color: black; padding: 5px; border: 1px solid black;">FP</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div><br>
|
||||
|
||||
A player starts on the GO square and adds the scores on two 6-sided dice to determine the number of squares they advance in a clockwise direction. Without any further rules we would expect to visit each square with equal probability: 2.5%. However, landing on G2J (Go To Jail), CC (community chest), and CH (chance) changes this distribution.
|
||||
|
||||
In addition to G2J, and one card from each of CC and CH, that orders the player to go directly to jail, if a player rolls three consecutive doubles, they do not advance the result of their 3rd roll. Instead they proceed directly to jail.
|
||||
|
||||
At the beginning of the game, the CC and CH cards are shuffled. When a player lands on CC or CH they take a card from the top of the respective pile and, after following the instructions, it is returned to the bottom of the pile. There are sixteen cards in each pile, but for the purpose of this problem we are only concerned with cards that order a movement; any instruction not concerned with movement will be ignored and the player will remain on the CC/CH square.
|
||||
|
||||
<ul>
|
||||
<li>Community Chest (2/16 cards):</li>
|
||||
<ol>
|
||||
<li>Advance to GO</li>
|
||||
<li>Go to JAIL</li>
|
||||
</ol>
|
||||
|
||||
<li>Chance (10/16 cards):</li>
|
||||
<ol>
|
||||
<li>Advance to GO</li>
|
||||
<li>Go to JAIL</li>
|
||||
<li>Go to C1</li>
|
||||
<li>Go to E3</li>
|
||||
<li>Go to H2</li>
|
||||
<li>Go to R1</li>
|
||||
<li>Go to next R (railway company)</li>
|
||||
<li>Go to next R</li>
|
||||
<li>Go to next U (utility company)</li>
|
||||
<li>Go back 3 squares.</li>
|
||||
</ol>
|
||||
</ul>
|
||||
|
||||
The heart of this problem concerns the likelihood of visiting a particular square. That is, the probability of finishing at that square after a roll. For this reason it should be clear that, with the exception of G2J for which the probability of finishing on it is zero, the CH squares will have the lowest probabilities, as 5/8 request a movement to another square, and it is the final square that the player finishes at on each roll that we are interested in. We shall make no distinction between "Just Visiting" and being sent to JAIL, and we shall also ignore the rule about requiring a double to "get out of jail", assuming that they pay to get out on their next turn.
|
||||
|
||||
By starting at GO and numbering the squares sequentially from 00 to 39 we can concatenate these two-digit numbers to produce strings that correspond with sets of squares.
|
||||
|
||||
Statistically it can be shown that the three most popular squares, in order, are JAIL (6.24%) = Square 10, E3 (3.18%) = Square 24, and GO (3.09%) = Square 00. So these three most popular squares can be listed with the six-digit modal string `102400`.
|
||||
|
||||
If, instead of using two 6-sided dice, two `n`-sided dice are used, find the six-digit modal string.
|
||||
|
||||
# --hints--
|
||||
|
||||
`monopolyOdds(8)` should return a string.
|
||||
|
||||
```js
|
||||
assert(typeof monopolyOdds(8) === 'string');
|
||||
```
|
||||
|
||||
`monopolyOdds(8)` should return string `102400`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(monopolyOdds(8), '102400');
|
||||
```
|
||||
|
||||
`monopolyOdds(10)` should return string `100024`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(monopolyOdds(10), '100024');
|
||||
```
|
||||
|
||||
`monopolyOdds(20)` should return string `100005`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(monopolyOdds(20), '100005');
|
||||
```
|
||||
|
||||
`monopolyOdds(4)` should return string `101524`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(monopolyOdds(4), '101524');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function monopolyOdds(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
monopolyOdds(8);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function monopolyOdds(n) {
|
||||
function chanceCard(position, chanceCardPosition) {
|
||||
chanceCardPosition = (chanceCardPosition + 1) % 16;
|
||||
if (chanceCardPosition < 6) {
|
||||
position = chanceCardsMoves[chanceCardPosition];
|
||||
} else if (chanceCardPosition === 6 || chanceCardPosition === 7) {
|
||||
position = nextMovesFromR[position];
|
||||
} else if (chanceCardPosition === 8) {
|
||||
position = nextMovesFromU[position];
|
||||
} else if (chanceCardPosition === 9) {
|
||||
position -= 3;
|
||||
}
|
||||
return [position, chanceCardPosition];
|
||||
}
|
||||
|
||||
function chestCard(position, chestPosition) {
|
||||
chestPosition = (chestPosition + 1) % 16;
|
||||
if (chestPosition < 2) {
|
||||
position = chestCardsMoves[chestPosition];
|
||||
}
|
||||
return [position, chestPosition];
|
||||
}
|
||||
|
||||
function isChest(position) {
|
||||
return position === 2 || position === 17 || position === 33;
|
||||
}
|
||||
|
||||
function isChance(position) {
|
||||
return position === 7 || position === 22 || position === 36;
|
||||
}
|
||||
|
||||
function isJail(position) {
|
||||
return position === 30;
|
||||
}
|
||||
|
||||
function roll(dice) {
|
||||
return Math.floor(Math.random() * dice) + 1;
|
||||
}
|
||||
|
||||
function getTopThree(board) {
|
||||
return sortByVisits(board)
|
||||
.slice(0, 3)
|
||||
.map(elem => elem[0].toString().padStart(2, '0'))
|
||||
.join('');
|
||||
}
|
||||
|
||||
function sortByVisits(board) {
|
||||
return board
|
||||
.map((element, index) => [index, element])
|
||||
.sort((a, b) => a[1] - b[1])
|
||||
.reverse();
|
||||
}
|
||||
|
||||
const rounds = 2000000;
|
||||
const chestCardsMoves = [0, 10];
|
||||
const chanceCardsMoves = [0, 10, 11, 24, 39, 5];
|
||||
const nextMovesFromR = { 7: 15, 22: 25, 36: 5 };
|
||||
const nextMovesFromU = { 7: 12, 36: 12, 22: 28 };
|
||||
|
||||
const board = new Array(40).fill(0);
|
||||
let doubleCount = 0;
|
||||
let curPosition = 0;
|
||||
let curChestCard = 0;
|
||||
let curChanceCard = 0;
|
||||
|
||||
for (let i = 0; i < rounds; i++) {
|
||||
const dice1 = roll(n);
|
||||
const dice2 = roll(n);
|
||||
|
||||
if (dice1 === dice2) {
|
||||
doubleCount++;
|
||||
} else {
|
||||
doubleCount = 0;
|
||||
}
|
||||
|
||||
if (doubleCount > 2) {
|
||||
curPosition = 10;
|
||||
doubleCount = 0;
|
||||
} else {
|
||||
curPosition = (curPosition + dice1 + dice2) % 40;
|
||||
|
||||
if (isChance(curPosition)) {
|
||||
[curPosition, curChanceCard] = chanceCard(curPosition, curChanceCard);
|
||||
} else if (isChest(curPosition)) {
|
||||
[curPosition, curChestCard] = chestCard(curPosition, curChestCard);
|
||||
} else if (isJail(curPosition)) {
|
||||
curPosition = 10;
|
||||
}
|
||||
}
|
||||
board[curPosition]++;
|
||||
}
|
||||
return getTopThree(board);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 5900f3c11000cf542c50fed4
|
||||
title: 'Problem 85: Counting rectangles'
|
||||
challengeType: 1
|
||||
forumTopicId: 302199
|
||||
dashedName: problem-85-counting-rectangles
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles:
|
||||
|
||||
<img class="img-responsive center-block" alt="a diagram of the different rectangles found within a 3 by 2 rectangular grid" src="https://cdn-media-1.freecodecamp.org/project-euler/counting-rectangles.png" style="background-color: white; padding: 10px;" />
|
||||
|
||||
Although there may not exists a rectangular grid that contains exactly `n` rectangles, find the area of the grid with the nearest solution.
|
||||
|
||||
# --hints--
|
||||
|
||||
`countingRectangles(18)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof countingRectangles(18) === 'number');
|
||||
```
|
||||
|
||||
`countingRectangles(18)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingRectangles(18), 6);
|
||||
```
|
||||
|
||||
`countingRectangles(250)` should return `22`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingRectangles(250), 22);
|
||||
```
|
||||
|
||||
`countingRectangles(50000)` should return `364`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingRectangles(50000), 364);
|
||||
```
|
||||
|
||||
`countingRectangles(1000000)` should return `1632`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingRectangles(1000000), 1632);
|
||||
```
|
||||
|
||||
`countingRectangles(2000000)` should return `2772`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(countingRectangles(2000000), 2772);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function countingRectangles(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
countingRectangles(18);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function countingRectangles(n) {
|
||||
function numberOfRectangles(h, w) {
|
||||
return (h * (h + 1) * w * (w + 1)) / 4;
|
||||
}
|
||||
|
||||
function rectangleArea(h, w) {
|
||||
return h * w;
|
||||
}
|
||||
|
||||
let rectanglesCount = 1;
|
||||
let maxSide = 1;
|
||||
while (rectanglesCount < n) {
|
||||
maxSide++;
|
||||
rectanglesCount = numberOfRectangles(maxSide, 1);
|
||||
}
|
||||
|
||||
let bestDiff = Math.abs(rectanglesCount - n);
|
||||
let bestSize = [maxSide, 1];
|
||||
|
||||
let curHeight = maxSide - 1;
|
||||
let curWidth = 1;
|
||||
|
||||
for (curWidth; curWidth < curHeight; curWidth++) {
|
||||
for (curHeight; curHeight > curWidth; curHeight--) {
|
||||
rectanglesCount = numberOfRectangles(curHeight, curWidth);
|
||||
const curDiff = Math.abs(rectanglesCount - n);
|
||||
if (curDiff < bestDiff) {
|
||||
bestDiff = curDiff;
|
||||
bestSize = [curHeight, curWidth];
|
||||
}
|
||||
|
||||
if (rectanglesCount < n) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return rectangleArea(...bestSize);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,94 @@
|
||||
---
|
||||
id: 5900f3c31000cf542c50fed5
|
||||
title: 'Problem 86: Cuboid route'
|
||||
challengeType: 1
|
||||
forumTopicId: 302200
|
||||
dashedName: problem-86-cuboid-route
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the opposite corner. By travelling on the surfaces of the room the shortest "straight line" distance from S to F is 10 and the path is shown on the diagram.
|
||||
|
||||
<img class="img-responsive center-block" alt="a diagram of a spider and fly's path from one corner of a cuboid room to the opposite corner" src="https://cdn-media-1.freecodecamp.org/project-euler/cuboid-route.png" style="background-color: white; padding: 10px;" />
|
||||
|
||||
However, there are up to three "shortest" path candidates for any given cuboid and the shortest route doesn't always have integer length.
|
||||
|
||||
It can be shown that there are exactly `2060` distinct cuboids, ignoring rotations, with integer dimensions, up to a maximum size of M by M by M, for which the shortest route has integer length when M = 100. This is the least value of M for which the number of solutions first exceeds two thousand; the number of solutions when M = 99 is `1975`.
|
||||
|
||||
Find the least value of M such that the number of solutions first exceeds `n`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`cuboidRoute(2000)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof cuboidRoute(2000) === 'number');
|
||||
```
|
||||
|
||||
`cuboidRoute(2000)` should return `100`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cuboidRoute(2000), 100);
|
||||
```
|
||||
|
||||
`cuboidRoute(25000)` should return `320`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cuboidRoute(25000), 320);
|
||||
```
|
||||
|
||||
`cuboidRoute(500000)` should return `1309`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cuboidRoute(500000), 1309);
|
||||
```
|
||||
|
||||
`cuboidRoute(1000000)` should return `1818`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(cuboidRoute(1000000), 1818);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function cuboidRoute(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
cuboidRoute(2000);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function cuboidRoute(n) {
|
||||
// Based on https://www.mathblog.dk/project-euler-86-shortest-path-cuboid/
|
||||
function getLength(a, b) {
|
||||
return Math.sqrt(a ** 2 + b ** 2);
|
||||
}
|
||||
|
||||
let M = 2;
|
||||
let counter = 0;
|
||||
|
||||
while (counter < n) {
|
||||
M++;
|
||||
for (let baseHeightWidth = 3; baseHeightWidth <= 2 * M; baseHeightWidth++) {
|
||||
const pathLength = getLength(M, baseHeightWidth);
|
||||
if (Number.isInteger(pathLength)) {
|
||||
if (baseHeightWidth <= M) {
|
||||
counter += Math.floor(baseHeightWidth / 2);
|
||||
} else {
|
||||
counter += 1 + M - Math.floor((baseHeightWidth + 1) / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return M;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,131 @@
|
||||
---
|
||||
id: 5900f3c51000cf542c50fed8
|
||||
title: 'Problem 87: Prime power triples'
|
||||
challengeType: 1
|
||||
forumTopicId: 302201
|
||||
dashedName: problem-87-prime-power-triples
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is `28`. In fact, there are exactly four numbers below fifty that can be expressed in such a way:
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
28 = 2<sup>2</sup> + 2<sup>3</sup> + 2<sup>4</sup><br>
|
||||
33 = 3<sup>2</sup> + 2<sup>3</sup> + 2<sup>4</sup><br>
|
||||
49 = 5<sup>2</sup> + 2<sup>3</sup> + 2<sup>4</sup><br>
|
||||
47 = 2<sup>2</sup> + 3<sup>3</sup> + 2<sup>4</sup>
|
||||
</div><br>
|
||||
|
||||
How many numbers below `n` can be expressed as the sum of a prime square, prime cube, and prime fourth power?
|
||||
|
||||
# --hints--
|
||||
|
||||
`primePowerTriples(50)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof primePowerTriples(50) === 'number');
|
||||
```
|
||||
|
||||
`primePowerTriples(50)` should return `4`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primePowerTriples(50), 4);
|
||||
```
|
||||
|
||||
`primePowerTriples(10035)` should return `684`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primePowerTriples(10035), 684);
|
||||
```
|
||||
|
||||
`primePowerTriples(500000)` should return `18899`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primePowerTriples(500000), 18899);
|
||||
```
|
||||
|
||||
`primePowerTriples(5000000)` should return `138932`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primePowerTriples(5000000), 138932);
|
||||
```
|
||||
|
||||
`primePowerTriples(50000000)` should return `1097343`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(primePowerTriples(50000000), 1097343);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function primePowerTriples(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
primePowerTriples(50);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function primePowerTriples(n) {
|
||||
function getSievePrimes(max) {
|
||||
const primes = [];
|
||||
const primesMap = new Array(max).fill(true);
|
||||
primesMap[0] = false;
|
||||
primesMap[1] = false;
|
||||
|
||||
for (let i = 2; i <= max; i += 2) {
|
||||
if (primesMap[i]) {
|
||||
primes.push(i);
|
||||
for (let j = i * i; j <= max; j = j + i) {
|
||||
primesMap[j] = false;
|
||||
}
|
||||
}
|
||||
if (i === 2) {
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
function getPowersSummed(numbers, powers, limit, curSum) {
|
||||
if (curSum >= limit) {
|
||||
return [];
|
||||
} else if (powers.length === 0) {
|
||||
return [curSum];
|
||||
}
|
||||
|
||||
const powersSummed = [];
|
||||
|
||||
const curPower = powers[0];
|
||||
const powersLeft = powers.slice(1);
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
const curNumber = numbers[i];
|
||||
const nextSum = curSum + curNumber ** curPower;
|
||||
if (nextSum >= limit) {
|
||||
return powersSummed;
|
||||
}
|
||||
const result = getPowersSummed(
|
||||
numbers,
|
||||
powersLeft,
|
||||
limit,
|
||||
curSum + curNumber ** curPower
|
||||
);
|
||||
powersSummed.push(...result);
|
||||
}
|
||||
return powersSummed;
|
||||
}
|
||||
|
||||
const maximumBaseNumber = Math.floor(Math.sqrt(n - 2 ** 3 - 2 ** 4)) + 1;
|
||||
const primes = getSievePrimes(maximumBaseNumber);
|
||||
const uniqueSums = new Set(getPowersSummed(primes, [2, 3, 4], n, 0));
|
||||
|
||||
return uniqueSums.size;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,110 @@
|
||||
---
|
||||
id: 5900f3c51000cf542c50fed6
|
||||
title: 'Problem 88: Product-sum numbers'
|
||||
challengeType: 1
|
||||
forumTopicId: 302203
|
||||
dashedName: problem-88-product-sum-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A natural number, `N`, that can be written as the sum and product of a given set of at least two natural numbers, $\\{a_1, a_2, \ldots , a_k\\}$ is called a product-sum number: $N = a_1 + a_2 + \cdots + a_k = a_1 × a_2 × \cdots × a_k$.
|
||||
|
||||
For example, 6 = 1 + 2 + 3 = 1 × 2 × 3.
|
||||
|
||||
For a given set of size, `k`, we shall call the smallest N with this property a minimal product-sum number. The minimal product-sum numbers for sets of size, `k` = 2, 3, 4, 5, and 6 are as follows.
|
||||
|
||||
<div style='margin-left: 4em;'>
|
||||
<var>k</var>=2: 4 = 2 × 2 = 2 + 2<br>
|
||||
<var>k</var>=3: 6 = 1 × 2 × 3 = 1 + 2 + 3<br>
|
||||
<var>k</var>=4: 8 = 1 × 1 × 2 × 4 = 1 + 1 + 2 + 4<br>
|
||||
<var>k</var>=5: 8 = 1 × 1 × 2 × 2 × 2 = 1 + 1 + 2 + 2 + 2<br>
|
||||
<var>k</var>=6: 12 = 1 × 1 × 1 × 1 × 2 × 6 = 1 + 1 + 1 + 1 + 2 + 6
|
||||
</div><br>
|
||||
|
||||
Hence for 2 ≤ `k` ≤ 6, the sum of all the minimal product-sum numbers is 4 + 6 + 8 + 12 = 30; note that `8` is only counted once in the sum.
|
||||
|
||||
In fact, as the complete set of minimal product-sum numbers for 2 ≤ `k` ≤ 12 is $\\{4, 6, 8, 12, 15, 16\\}$, the sum is `61`.
|
||||
|
||||
What is the sum of all the minimal product-sum numbers for 2 ≤ `k` ≤ `limit`?
|
||||
|
||||
# --hints--
|
||||
|
||||
`productSumNumbers(6)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof productSumNumbers(6) === 'number');
|
||||
```
|
||||
|
||||
`productSumNumbers(6)` should return `30`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(productSumNumbers(6), 30);
|
||||
```
|
||||
|
||||
`productSumNumbers(12)` should return `61`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(productSumNumbers(12), 61);
|
||||
```
|
||||
|
||||
`productSumNumbers(300)` should return `12686`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(productSumNumbers(300), 12686);
|
||||
```
|
||||
|
||||
`productSumNumbers(6000)` should return `2125990`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(productSumNumbers(6000), 2125990);
|
||||
```
|
||||
|
||||
`productSumNumbers(12000)` should return `7587457`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(productSumNumbers(12000), 7587457);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function productSumNumbers(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
productSumNumbers(6);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function productSumNumbers(limit) {
|
||||
function getProductSums(curProduct, curSum, factorsCount, start) {
|
||||
const k = curProduct - curSum + factorsCount;
|
||||
if (k <= limit) {
|
||||
if (curProduct < minimalProductSums[k]) {
|
||||
minimalProductSums[k] = curProduct;
|
||||
}
|
||||
for (let i = start; i < Math.floor((limit / curProduct) * 2) + 1; i++) {
|
||||
getProductSums(curProduct * i, curSum + i, factorsCount + 1, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const minimalProductSums = new Array(limit + 1).fill(2 * limit);
|
||||
getProductSums(1, 1, 1, 2);
|
||||
|
||||
const uniqueProductSums = [...new Set(minimalProductSums.slice(2))];
|
||||
|
||||
let sum = 0;
|
||||
for (let i = 0; i < uniqueProductSums.length; i++) {
|
||||
sum += uniqueProductSums[i];
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user