2018-09-30 22:01:58 +00:00
---
id: 5900f3951000cf542c50fea8
challengeType: 5
title: 'Problem 41: Pandigital prime'
---
## Description
< section id = 'description' >
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?
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 13:37:37 +00:00
tests:
- text: < code > pandigitalPrime(4)</ code > should return 4231.
2018-10-20 18:02:47 +00:00
testString: assert(pandigitalPrime(4) == 4231, '< code > pandigitalPrime(4)< / code > should return 4231.');
2018-10-04 13:37:37 +00:00
- text: < code > pandigitalPrime(7)</ code > should return 7652413.
2018-10-20 18:02:47 +00:00
testString: assert(pandigitalPrime(7) == 7652413, '< code > pandigitalPrime(7)< / code > should return 7652413.');
2018-09-30 22:01:58 +00:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function pandigitalPrime(n) {
// Good luck!
return n;
}
pandigitalPrime(7);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```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) {
2018-10-02 14:02:53 +00:00
permutations.push(digitsArr.join(''));
2018-09-30 22:01:58 +00:00
} 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;
2018-10-20 18:02:47 +00:00
let pandigitalNum = '';
2018-09-30 22:01:58 +00:00
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);
2018-10-02 14:02:53 +00:00
digitsArr = currMax.split('');
2018-09-30 22:01:58 +00:00
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;
}
```
< / section >