Project Euler:Problem 41 Pandigital prime
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的更多相关文章
- 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 ...
- 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 ...
- 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 + ...
- Project Euler:Problem 58 Spiral primes
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Ruby on rails初体验(二)
体验一中添加了一个最基本的支架和一个简单的数据迁移,实现了一个基本的增删改查的功能列表.体验二中要在次功能上继续丰满一下功能.实现如下效果: 在每个公司中都包含有不同的部门,按照体验一中的方法,添加一 ...
- Linux文件名命名规范
注意:在Linux下全部文件和文件夹命名都是小写的! Linux系统区分英文字符的大小写.命名目录和命名文件的规则是相同的.除非有特别的原因,否则用户创建的文件和目录名要使用小写字符.大多数的Linu ...
- win8.1安装VMware Error:This product may not be installed on a comuputer that has Microsoft HyperV installed
之前用的win7,安装虚机没遇到这问题,换了win8.1后,再安装虚机,就会出现下面的错误.没办法,还是记录一下吧. Error:This product may not be installed o ...
- 使用Git Gui从Bonobo服务器中克隆Repository(仓库)
刚开始在使用Git Gui从Bonobo服务器中克隆Repository(仓库)中遇到一些问题,如下图所示: 后来百度,有人遇到类似的问题,得到解决思路,请大家参考这里.大意说出现这个问题是因为要克隆 ...
- 2013年9月29日 iOS 周报
新闻 Apple Tech Talks 2013 在中国上海的iOS Tech Talks活动将于11月12日展开,活动主要针对iOS 7.活动分为App开放日和游戏开放日,主要内容可查看链接.当你看 ...
- 测试Apache服务器及httpd: Could not reliably determine the server's fully qualified domain name解决办法
测试Apache服务器: 重启apache: sudo /usr/local/apache/bin/apachectl restart 若出现错误: httpd: Could not reliably ...
- VS2010 MFC中 使用CListCtrl的排序功能
list 控件是creat的,不是拖在对话框上的.想使用CListCtrl的排序功能却犯了愁~~~ 还好找到方法,如下: .h文件里:afx_msg void OnLvnColumnclickList ...
- 简单便捷的纯PHP网盘程序 Veno File Manager 2.6.3(VFM2)
体验过很多国外网盘程序,例如:Owncloud.Bedrive.YetiShare.XFilesharing.uCloud.Cloudshare 等等,诸如此类,VFM2与这些臃肿的商用或非商用来的程 ...
- linux下怎样对串口编程
Linux操作系统从一開始就对串行口提供了非常好的支持,本文就Linux下的串行口通讯编程进行简单的介绍. 串口简单介绍串行口是计算机一种经常使用的接口.具有连接线少.通讯简单.得到广泛的使用. 经常 ...
- SQLServer-----SQLServer 2008 R2安装