fix(challenges): Edit Description, Add Tests and Solution for Project Euler 41 (#17088)
Updated the problem description with <i> tags. Removed a couple of tests because they seemed too restrictive for the different approaches users might take when solving this problem. Also added a solution that user @elliotjz contributed to the fCC Arcade Mode. BREAKING CHANGE: Nonepull/17093/head
parent
58e8054f67
commit
fa1cc4a436
|
@ -1491,11 +1491,9 @@
|
|||
"title": "Problem 41: Pandigital prime",
|
||||
"tests": [
|
||||
"assert(pandigitalPrime(4) == 4231, 'message: <code>pandigitalPrime(4)</code> should return 4231.');",
|
||||
"assert(pandigitalPrime(5) == 0, 'message: <code>pandigitalPrime(5)</code> should return 0.');",
|
||||
"assert(pandigitalPrime(6) == 0, 'message: <code>pandigitalPrime(6)</code> should return 0.');",
|
||||
"assert(pandigitalPrime(7) == 7652413, 'message: <code>pandigitalPrime(7)</code> should return 7652413.');"
|
||||
],
|
||||
"solutions": [],
|
||||
"solutions": ["function pandigitalPrime(n) {\n function isPrime(num) {\n for (let i = 2, s = Math.sqrt(num); i <= s; i++) {\n if (num % i === 0) {\n return false;\n }\n }\n return num !== 1;\n }\n\n function getPermutations(n) {\n if (n === 1) {\n permutations.push(digitsArr.join(''));\n } else {\n for (let i = 0; i < n - 1; i++) {\n getPermutations(n - 1);\n // swap(n % 2 === 0 ? i : 0, n - 1);\n if (n % 2 === 0) {\n swap(i, n - 1);\n } else {\n swap(0, n - 1);\n }\n }\n getPermutations(n - 1);\n }\n }\n function swap(x, y) {\n let temp = digitsArr[x];\n digitsArr[x] = digitsArr[y];\n digitsArr[y] = temp;\n }\n let max = 0;\n let permutations = [];\n let digitsArr;\n let pandigitalNum = '';\n\n for (let max = n; max > 0; max--) {\n pandigitalNum += max;\n }\n\n for (let i = 0; i < pandigitalNum.length; i++) {\n if (max > 0) {\n break;\n } else {\n permutations = [];\n const currMax = pandigitalNum.slice(i);\n digitsArr = currMax.split('');\n getPermutations(digitsArr.length);\n\n // sort permutations in descending order\n permutations.sort(function(a, b) {\n return b - a;\n });\n\n for (let perm of permutations) {\n const thisPerm = parseInt(perm);\n if (isPrime(thisPerm)) {\n max = thisPerm;\n break;\n }\n }\n }\n }\n\n return max;\n}"],
|
||||
"translations": {},
|
||||
"challengeSeed": [
|
||||
"function pandigitalPrime(n) {",
|
||||
|
@ -1506,8 +1504,8 @@
|
|||
"pandigitalPrime(7);"
|
||||
],
|
||||
"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?"
|
||||
"We shall say that an <i>n</i>-digit number is pandigital if it makes use of all the digits 1 to <i>n</i> exactly once. For example, 2143 is a 4-digit pandigital and is also prime.",
|
||||
"What is the largest <i>n</i>-length digit pandigital prime that exists?"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue