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. MySQL完整备份,还原

    # 备份 添加编码 --default-character-set=utf8 防止中文乱码 把.sql文件导入MySQL, 汉字出现乱码?在.sql文件头中添加:set names 'gbk'; 或  ...

  2. jenkins 管理员账号丢失

    在jenkins 设置权限后,无法登录 参考: 如何修改jenkins配置权限   https://zhidao.baidu.com/question/497256501658876284.html

  3. AC日记——幸运号码 51nod 1043

    幸运号码 思路: 传说中的数位dp: 不难发现,n(n<1000) ,那么,n个数的最大和为9*1000=9000: 对于9000*1000的时间范围,我们可以用dp来解决: dp[i][j], ...

  4. facebook architecture 2 【转】

    At the scale that Facebook operates, a lot of traditional approaches to serving web content breaks d ...

  5. luogu P2285 [HNOI2004]打鼹鼠

    题目描述 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气. ...

  6. eclipse 国际化 $NON-NLS-1$ 含义

    一.$NON-NLS-1$ 含义 Eclipse 如果每行代码里有这个字符串:$NON-NLS-1$ 表示:这一行的第一个字符串是不需要国际化的.同理$NON-NLS-2$,$NON-NLS-3$.. ...

  7. Beginning Auto Layout Tutorial in iOS 7: Part 6

    Gallery example 屏幕有四个分开的相同的矩形,每个矩形有一个label和一个image view.创建一个Gallery的项目.在Main.storyboard中,拖拉一个view大小为 ...

  8. 有关javaScript面向对象和原型笔记

    javaScript是一种比較特殊的语言,ECMAScript中没有类的概念.跟其它面向对象的语言有一定的差别.它的对象也与基于类的语言中的对象有所不同,严格来说,javascript对象是一组没有特 ...

  9. 网络库libevent、libev、libuv对比

    Libevent.libev.libuv三个网络库,都是c语言实现的异步事件库Asynchronousevent library). 异步事件库本质上是提供异步事件通知(Asynchronous Ev ...

  10. 【GLSL教程】(九)其他说明 【转】

    http://blog.csdn.net/racehorse/article/details/6664775 法线矩阵 在很多顶点shader中都用到了gl_NormalMatrix.这里将介绍这个矩 ...