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-digit pandigital prime that exists?

#include <iostream>
#include <string>
using namespace std; int res = 0; bool prim(int a)
{
for (int i = 2; i*i <= a; i++)
{
if (a%i == 0)
return false;
}
return true;
} void perm(int list[], int n, int k)
{
int temp1, temp2;
if (n == 1)
{
int sum = 0;
for (int i = k; i > 0; i--)
sum = sum * 10 + list[i];
if (prim(sum)&&sum > res)
res = sum;
}
else
for (int i = 1; i <= n; i++)
{ temp1 = list[i];
list[i] = list[n];
list[n] = temp1; perm(list, n - 1, k); temp2 = list[i];
list[i] = list[n];
list[n] = temp2;
} } int main()
{
for (int j = 9; j >= 1; j--)
{
int list[200];
for (int i = 1; i <= j; i++)
list[i] = i;
perm(list, j, j);
}
cout << res << endl;
system("pause"); return 0;
}

Project Euler:Problem 41 Pandigital prime的更多相关文章

  1. Project Euler:Problem 32 Pandigital products

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  2. Project Euler:Problem 87 Prime power triples

    The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...

  3. Project Euler:Problem 77 Prime summations

    It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + ...

  4. Project Euler:Problem 58 Spiral primes

    Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...

  5. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  6. Project Euler:Problem 47 Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...

  7. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

  8. Project Euler:Problem 86 Cuboid route

    A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...

  9. Project Euler:Problem 76 Counting summations

    It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...

随机推荐

  1. [SaltStack] Salt高可用和负载均衡部署

    Saltstack HA部署 Salt官网是有HA/Rebalance/failover解决方案的, 但版本必须是2014.7, 目前该版本还没有release, 从官网下载的源码包安装使用测试起来一 ...

  2. C#反射(Reflection)详解

    1. 什么是反射2. 命名空间与装配件的关系3. 运行期得到类型信息有什么用4. 如何使用反射获取类型5. 如何根据类型来动态创建对象6. 如何获取方法以及动态调用方法7. 动态创建委托 1.什么是反 ...

  3. Python Challenge 第十三关

    第13关.一张电话的图片,一句话:phone that evil.看到电话,加上之前关卡有些图片有链接,我就在电话按键上都点点试试,果然 5 是个链接,就点了进去.出来一个XML文件,第一句写着:Th ...

  4. Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    E - Restore Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are num ...

  5. atom 默认的换行符设置成LF

    如何把atom 默认的换行符设置成LF Select File->Settings Select ‘Packages’ Under Core Packages->line-ending-s ...

  6. 2016北京集训测试赛(六)Problem B: 矩阵

    Solution 最小割. 参考BZOJ 3144切糕 在那道题的基础上将建图方法稍作变形: 我们对格子进行黑白染色, 对于两个格子之和\(\le k\)的限制, 就可以确定其中一个是白色格子, 一个 ...

  7. delphi中如何将string类型的字符串数据转化成byte[]字节数组类型的数据

    var  S:String;  P:PChar;  B:array of Byte;begin  S:='Hello';  SetLength(B,Length(S)+1);  P:=PChar(S) ...

  8. 浅谈XXE攻击

    一.XXE,即XML External Entity,XML外部实体.ENTITY 实体,在一个甚至多个XML文档中频繁使用某一条数据,我们可以预先定义一个这条数据的“别名”,即一个ENTITY,然后 ...

  9. Mysql中delimiter作用

    1. delimiter delimiter是mysql分隔符.在mysqlclient中分隔符默认是分号(:). 假设一次输入的语句较多,而且语句中间有分号,这时须要新指定一个特殊的分隔符. 2. ...

  10. 我用select做多路复用踩到的坑

    既然说是用select踩到的坑,那么就先直接贴一段使用select的代码上来瞅一下: bool SocketAction(int fd, const char* buf, size_t len, ui ...