Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

110 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 5900f3951000cf542c50fea8
title: 问题41Pandigital prime
challengeType: 5
videoUrl: ''
dashedName: problem-41-pandigital-prime
---
# --description--
我们将说*n*数字是pandigital如果它使用所有数字1到*n*恰好一次。例如2143是一个4位数的pandigital也是素数。什么是最大的*n长*数字pandigital素数
# --hints--
`pandigitalPrime(4)`应该返回4231。
```js
assert(pandigitalPrime(4) == 4231);
```
`pandigitalPrime(7)`应该返回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;
}
```