fix(challenges): add solution, test to project euler problems

This commit achieves the following:
Add solution to problems: 21, 25, 29, 35, 55
Add/modify tests to problems: 5, 12, 14, 25, 55
Update description to problems: 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 14, 15, 16, 17, 55

fix(challenges): add solution and tests for project euler problem 55

Added solution and tests to evaluate problem 55 of Project Euler challenges.
This commit is contained in:
Bharath Kumar
2018-08-28 18:19:15 +05:30
committed by Kristofer Koishigawa
parent 68daaf73d9
commit f572324009

View File

@ -97,7 +97,7 @@
"description": [ "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:", "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>", "<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 four million, find the sum of the even-valued terms." "By considering the terms in the Fibonacci sequence whose values do not exceed <code>n</code>th term, find the sum of the even-valued terms."
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -105,7 +105,7 @@
"ext": "js", "ext": "js",
"name": "index", "name": "index",
"contents": [ "contents": [
"function fiboEvenSum(number) {", "function fiboEvenSum(n) {",
" // You can do it!", " // You can do it!",
" return true;", " return true;",
"}", "}",
@ -160,7 +160,7 @@
"translations": {}, "translations": {},
"description": [ "description": [
"The prime factors of 13195 are 5, 7, 13 and 29.", "The prime factors of 13195 are 5, 7, 13 and 29.",
"What is the largest prime factor of the number 600851475143 ?" "What is the largest prime factor of the given <code>number</code>?"
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -204,7 +204,7 @@
"translations": {}, "translations": {},
"description": [ "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.", "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 3-digit numbers." "Find the largest palindrome made from the product of two <code>n</code>-digit numbers."
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -212,7 +212,7 @@
"ext": "js", "ext": "js",
"name": "index", "name": "index",
"contents": [ "contents": [
"function largestPalindromeProduct(digit) {", "function largestPalindromeProduct(n) {",
" // Good luck!", " // Good luck!",
" return true;", " return true;",
"}", "}",
@ -234,11 +234,21 @@
"testString": "testString":
"assert.strictEqual(smallestMult(5), 60, '<code>smallestMult(5)</code> should return 60.');" "assert.strictEqual(smallestMult(5), 60, '<code>smallestMult(5)</code> should return 60.');"
}, },
{
"text": "<code>smallestMult(7)</code> should return 420.",
"testString":
"assert.strictEqual(smallestMult(7), 420, '<code>smallestMult(7)</code> should return 420.');"
},
{ {
"text": "<code>smallestMult(10)</code> should return 2520.", "text": "<code>smallestMult(10)</code> should return 2520.",
"testString": "testString":
"assert.strictEqual(smallestMult(10), 2520, '<code>smallestMult(10)</code> should return 2520.');" "assert.strictEqual(smallestMult(10), 2520, '<code>smallestMult(10)</code> should return 2520.');"
}, },
{
"text": "<code>smallestMult(13)</code> should return 360360.",
"testString":
"assert.strictEqual(smallestMult(13), 360360, '<code>smallestMult(13)</code> should return 360360.');"
},
{ {
"text": "<code>smallestMult(20)</code> should return 232792560.", "text": "<code>smallestMult(20)</code> should return 232792560.",
"testString": "testString":
@ -251,7 +261,7 @@
"translations": {}, "translations": {},
"description": [ "description": [
"2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.", "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 20?" "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to <code>n</code>?"
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -303,7 +313,7 @@
"The square of the sum of the first ten natural numbers is,", "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>", "<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.", "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 one hundred natural numbers and the square of the sum." "Find the difference between the sum of the squares of the first <code>n</code> natural numbers and the square of the sum."
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -311,7 +321,7 @@
"ext": "js", "ext": "js",
"name": "index", "name": "index",
"contents": [ "contents": [
"function sumSquareDifference(number) {", "function sumSquareDifference(n) {",
" // Good luck!", " // Good luck!",
" return true;", " return true;",
"}", "}",
@ -360,7 +370,7 @@
"translations": {}, "translations": {},
"description": [ "description": [
"By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.", "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 10 001st prime number?" "What is the <code>n</code>th prime number?"
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -368,7 +378,7 @@
"ext": "js", "ext": "js",
"name": "index", "name": "index",
"contents": [ "contents": [
"function nthPrime(number) {", "function nthPrime(n) {",
" // Good luck!", " // Good luck!",
" return true;", " return true;",
"}", "}",
@ -424,7 +434,7 @@
"<div style='text-align: center;'>84580156166097919133875499200524063689912560717606</div>", "<div style='text-align: center;'>84580156166097919133875499200524063689912560717606</div>",
"<div style='text-align: center;'>05886116467109405077541002256983155200055935729725</div>", "<div style='text-align: center;'>05886116467109405077541002256983155200055935729725</div>",
"<div style='text-align: center;'>71636269561882670428252483600823257530420752963450</div>", "<div style='text-align: center;'>71636269561882670428252483600823257530420752963450</div>",
"Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?" "Find the <code>n</code> adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -432,7 +442,7 @@
"ext": "js", "ext": "js",
"name": "index", "name": "index",
"contents": [ "contents": [
"function largestProductinaSeries(number) {", "function largestProductinaSeries(n) {",
" // Good luck!", " // Good luck!",
" 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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5,3,0,4,2,0,7,5,2,9,6,3,4,5,0];",
" return true;", " return true;",
@ -477,7 +487,7 @@
"A Pythagorean triplet is a set of three natural numbers, <var>a</var> < <var>b</var> < <var>c</var>, for which,", "A Pythagorean triplet is a set of three natural numbers, <var>a</var> < <var>b</var> < <var>c</var>, for which,",
"<div style='text-align: center;'><var>a</var><sup>2</sup> + <var>b</var><sup>2</sup> = <var>c</var><sup>2</sup></div>", "<div style='text-align: center;'><var>a</var><sup>2</sup> + <var>b</var><sup>2</sup> = <var>c</var><sup>2</sup></div>",
"For example, 3<sup>2</sup> + 4<sup>2</sup> = 9 + 16 = 25 = 5<sup>2</sup>.", "For example, 3<sup>2</sup> + 4<sup>2</sup> = 9 + 16 = 25 = 5<sup>2</sup>.",
"There exists exactly one Pythagorean triplet for which <var>a</var> + <var>b</var> + <var>c</var> = 1000. Find the product <var>abc</var>." "There exists exactly one Pythagorean triplet for which <var>a</var> + <var>b</var> + <var>c</var> = 1000. Find the product <var>abc</var> such that <var>a</var> + <var>b</var> + <var>c</var> = <code>n</code>."
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -599,7 +609,7 @@
"<div style='text-align: center;'>01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48</div>", "<div style='text-align: center;'>01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48</div>",
"", "",
"The product of these numbers is 26 × 63 × 78 × 14 = 1788696.", "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 the 20×20 grid?" "What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in a given <code>arr</code> grid?"
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -668,9 +678,21 @@
}, },
{ {
"text": "text":
"<code>divisibleTriangleNumber()</code> should return 76576500.", "<code>divisibleTriangleNumber(167)</code> should return 1385280.",
"testString": "testString":
"assert.strictEqual(divisibleTriangleNumber(500), 76576500, '<code>divisibleTriangleNumber()</code> should return 76576500.');" "assert.strictEqual(divisibleTriangleNumber(167), 1385280, '<code>divisibleTriangleNumber(167)</code> should return 1385280.');"
},
{
"text":
"<code>divisibleTriangleNumber(374)</code> should return 17907120.",
"testString":
"assert.strictEqual(divisibleTriangleNumber(374), 17907120, '<code>divisibleTriangleNumber(374)</code> should return 17907120.');"
},
{
"text":
"<code>divisibleTriangleNumber(500)</code> should return 76576500.",
"testString":
"assert.strictEqual(divisibleTriangleNumber(500), 76576500, '<code>divisibleTriangleNumber(500)</code> should return 76576500.');"
} }
], ],
"solutions": [ "solutions": [
@ -689,7 +711,7 @@
"<div style='padding-left: 4em;'><b>21:</b> 1, 3, 7, 21</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>", "<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.", "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 five hundred divisors?" "What is the value of the first triangle number to have over <code>n</code> divisors?"
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -983,6 +1005,18 @@
"testString": "testString":
"assert.strictEqual(longestCollatzSequence(5847), 3711, '<code>longestCollatzSequence(5847)</code> should return 3711.');" "assert.strictEqual(longestCollatzSequence(5847), 3711, '<code>longestCollatzSequence(5847)</code> should return 3711.');"
}, },
{
"text":
"<code>longestCollatzSequence(46500)</code> should return 35655.",
"testString":
"assert.strictEqual(longestCollatzSequence(46500), 35655, '<code>longestCollatzSequence(46500)</code> should return 35655.');"
},
{
"text":
"<code>longestCollatzSequence(54512)</code> should return 52527.",
"testString":
"assert.strictEqual(longestCollatzSequence(54512), 52527, '<code>longestCollatzSequence(54512)</code> should return 52527.');"
},
{ {
"text": "text":
"<code>longestCollatzSequence(1000000)</code> should return 837799.", "<code>longestCollatzSequence(1000000)</code> should return 837799.",
@ -1001,7 +1035,7 @@
"Using the rule above and starting with 13, we generate the following sequence:", "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>", "<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.", "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 one million, produces the longest chain?", "Which starting number, under the given <code>limit</code>, produces the longest chain?",
"NOTE: Once the chain starts the terms are allowed to go above one million." "NOTE: Once the chain starts the terms are allowed to go above one million."
], ],
"files": { "files": {
@ -1052,7 +1086,7 @@
"", "",
"<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://i.imgur.com/1Atixoj.gif\">", "<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://i.imgur.com/1Atixoj.gif\">",
"", "",
"How many such routes are there through a 20×20 grid?" "How many such routes are there through a given <code>gridSize</code>?"
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -1099,7 +1133,7 @@
"translations": {}, "translations": {},
"description": [ "description": [
"2<sup>15</sup> = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.", "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>1000</sup>?" "What is the sum of the digits of the number 2<sup><code>exponent</code></sup>?"
], ],
"files": { "files": {
"indexjs": { "indexjs": {
@ -1146,7 +1180,7 @@
"translations": {}, "translations": {},
"description": [ "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 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 1000 (one thousand) inclusive were written out in words, how many letters would be used? ", "If all the numbers from 1 to given <code>limit</code> inclusive were written out in words, how many letters would be used? ",
"<b>NOTE:</b> 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." "<b>NOTE:</b> 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."
], ],
"files": { "files": {
@ -1352,7 +1386,9 @@
"assert.strictEqual(sumAmicableNum(10000), 31626, '<code>sumAmicableNum(10000)</code> should return 31626.');" "assert.strictEqual(sumAmicableNum(10000), 31626, '<code>sumAmicableNum(10000)</code> should return 31626.');"
} }
], ],
"solutions": [], "solutions": [
"const sumAmicableNum = (n) => {\n const fsum = (n) => {\n let sum = 1;\n for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++)\n if (Math.floor(n % i) === 0)\n sum += i + Math.floor(n / i);\n return sum;\n };\n let d = [];\n let amicableSum = 0;\n for (let i=2; i<n; i++) d[i] = fsum(i);\n for (let i=2; i<n; i++) {\n let dsum = d[i];\n if (d[dsum]===i && i!==dsum) amicableSum += i+dsum;\n }\n return amicableSum/2;\n};"
],
"translations": {}, "translations": {},
"description": [ "description": [
"Let d(<var>n</var>) be defined as the sum of proper divisors of <var>n</var> (numbers less than <var>n</var> which divide evenly into <var>n</var>).", "Let d(<var>n</var>) be defined as the sum of proper divisors of <var>n</var> (numbers less than <var>n</var> which divide evenly into <var>n</var>).",
@ -1547,27 +1583,29 @@
"title": "Problem 25: 1000-digit Fibonacci number", "title": "Problem 25: 1000-digit Fibonacci number",
"tests": [ "tests": [
{ {
"text": "<code>digitFibonacci(5)</code> should return 20.", "text": "<code>digitFibonacci(5)</code> should return 21.",
"testString": "testString":
"assert(digitFibonacci(5) == 20, '<code>digitFibonacci(5)</code> should return 20.');" "assert.strictEqual(digitFibonacci(5), 21, '<code>digitFibonacci(5)</code> should return 21.');"
}, },
{ {
"text": "<code>digitFibonacci(10)</code> should return 44.", "text": "<code>digitFibonacci(10)</code> should return 45.",
"testString": "testString":
"assert(digitFibonacci(10) == 44, '<code>digitFibonacci(10)</code> should return 44.');" "assert.strictEqual(digitFibonacci(10), 45, '<code>digitFibonacci(10)</code> should return 45.');"
}, },
{ {
"text": "<code>digitFibonacci(15)</code> should return 68.", "text": "<code>digitFibonacci(15)</code> should return 69.",
"testString": "testString":
"assert(digitFibonacci(15) == 68, '<code>digitFibonacci(15)</code> should return 68.');" "assert.strictEqual(digitFibonacci(15), 69, '<code>digitFibonacci(15)</code> should return 69.');"
}, },
{ {
"text": "<code>digitFibonacci(20)</code> should return 92.", "text": "<code>digitFibonacci(20)</code> should return 93.",
"testString": "testString":
"assert(digitFibonacci(20) == 92, '<code>digitFibonacci(20)</code> should return 92.');" "assert.strictEqual(digitFibonacci(20), 93, '<code>digitFibonacci(20)</code> should return 93.');"
} }
], ],
"solutions": [], "solutions": [
"const digitFibonacci = (n) => {\n const digits = (num) => {\n return num.toString().length;\n };\n let f1 = 1;\n let f2 = 1;\n let index = 3;\n while (true) {\n let fn = f1 + f2;\n if (digits(fn) === n) return index;\n [f1, f2] = [f2, fn];\n index++;\n }\n};"
],
"translations": {}, "translations": {},
"description": [ "description": [
"The Fibonacci sequence is defined by the recurrence relation:", "The Fibonacci sequence is defined by the recurrence relation:",
@ -1786,7 +1824,9 @@
"assert.strictEqual(distinctPowers(30), 755, '<code>distinctPowers(30)</code> should return 755.');" "assert.strictEqual(distinctPowers(30), 755, '<code>distinctPowers(30)</code> should return 755.');"
} }
], ],
"solutions": [], "solutions": [
"const distinctPowers = (n) => {\n let list = [];\n for (let a=2; a<=n; a++) {\n for (let b=2; b<=n; b++) {\n let term = Math.pow(a, b);\n if (list.indexOf(term)===-1) list.push(term);\n }\n }\n return list.length;\n};"
],
"translations": {}, "translations": {},
"description": [ "description": [
"Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:", "Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:",
@ -1846,10 +1886,10 @@
"translations": {}, "translations": {},
"description": [ "description": [
"Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:", "Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:",
"1634 = 14 + 64 + 34 + 44", "1634 = 1<sup>4</sup> + 6<sup>4</sup> + 3<sup>4</sup> + 4<sup>4</sup>",
"8208 = 84 + 24 + 04 + 84", "8208 = 8<sup>4</sup> + 2<sup>4</sup> + 0<sup>4</sup> + 8<sup>4</sup>",
"9474 = 94 + 44 + 74 + 44", "9474 = 9<sup>4</sup> + 4<sup>4</sup> + 7<sup>4</sup> + 4<sup>4</sup>",
"As 1 = 14 is not a sum it is not included.", "As 1 = 1<sup>4</sup> is not a sum it is not included.",
"The sum of these numbers is 1634 + 8208 + 9474 = 19316.", "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." "Find the sum of all the numbers that can be written as the sum of n powers of their digits."
], ],
@ -2085,7 +2125,9 @@
"assert(circularPrimes(1000000) == 55, '<code>circularPrimes(1000000)</code> should return 55.');" "assert(circularPrimes(1000000) == 55, '<code>circularPrimes(1000000)</code> should return 55.');"
} }
], ],
"solutions": [], "solutions": [
"const circularPrimes = (n) => {\n const primeCheck = (num) => {\n if (num === 1) {\n return false;\n }\n for (let i = 2; i <= Math.floor(Math.sqrt(num)); i++) {\n if (num % i === 0) {\n return false;\n }\n }\n return true;\n };\n let count = 1;\n for (let i = 1; i < n; i += 2) {\n if (primeCheck(i)) {\n let flag = true;\n let circularNum = i.toString();\n for (let j = 1; j < i.toString().length; j++) {\n circularNum = circularNum.substring(1) + circularNum.substring(0, 1);\n if (primeCheck(Number(circularNum)) === false) {\n flag = false;\n break;\n }\n }\n if (flag) {\n count++;\n }\n }\n }\n return count;\n};"
],
"translations": {}, "translations": {},
"description": [ "description": [
"The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.", "The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.",
@ -3017,12 +3059,34 @@
"title": "Problem 55: Lychrel numbers", "title": "Problem 55: Lychrel numbers",
"tests": [ "tests": [
{ {
"text": "<code>euler55()</code> should return 249.", "text": "<code>countLychrelNumbers(1000)</code> should return 13.",
"testString": "testString":
"assert.strictEqual(euler55(), 249, '<code>euler55()</code> should return 249.');" "assert.strictEqual(countLychrelNumbers(1000), 13, '<code>countLychrelNumbers(1000)</code> should return 13.');"
},
{
"text": "<code>countLychrelNumbers(5000)</code> should return 76.",
"testString":
"assert.strictEqual(countLychrelNumbers(5000), 76, '<code>countLychrelNumbers(5000)</code> should return 76.');"
},
{
"text": "<code>countLychrelNumbers(10000)</code> should return 249.",
"testString":
"assert.strictEqual(countLychrelNumbers(10000), 249, '<code>countLychrelNumbers(10000)</code> should return 249.');"
},
{
"text": "Your function should count all Lychrel numbers.",
"testString":
"assert.strictEqual(countLychrelNumbers(3243), 39, 'Your function should count all Lychrel numbers.');"
},
{
"text": "Your function should pass all test cases.",
"testString":
"assert.strictEqual(countLychrelNumbers(7654), 140, 'Your function should pass all test cases.');"
} }
], ],
"solutions": [], "solutions": [
"const countLychrelNumbers = (size) => {\n const numReverse = (num) => {\n return Number(num.toString().split('').reverse().join(''));\n };\n const isPalin = (num) => {\n if (numReverse(num) === num) {\n return true;\n }\n return false;\n };\n let total = 0;\n for (let i = 1; i < size; i++) {\n let loopCount = 1;\n let sum = i;\n while (loopCount < 50) {\n sum = sum + numReverse(sum);\n if (isPalin(sum)) {\n break;\n } else {\n loopCount++;\n }\n }\n if (loopCount === 50) {\n total++;\n }\n }\n return total;\n}"
],
"translations": {}, "translations": {},
"description": [ "description": [
"If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.", "If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.",
@ -3033,7 +3097,7 @@
"That is, 349 took three iterations to arrive at a palindrome.", "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).", "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.", "Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.",
"How many Lychrel numbers are there below ten-thousand?", "How many Lychrel numbers are there below <code>num</code>?",
"NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers." "NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers."
], ],
"files": { "files": {
@ -3042,12 +3106,12 @@
"ext": "js", "ext": "js",
"name": "index", "name": "index",
"contents": [ "contents": [
"function euler55() {", "function countLychrelNumbers(num) {",
" // Good luck!", " // Good luck!",
" return true;", " return true;",
"}", "}",
"", "",
"euler55();" "countLychrelNumbers(10000);"
], ],
"head": [], "head": [],
"tail": [] "tail": []