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 ...
随机推荐
- pyqt线程实现
# coding=utf-8 __author__ = 'a359680405' from PyQt5.QtCore import * from PyQt5.QtGui import * from P ...
- JDBC-oracle(登陆)
题目: 第一步:创建用户表,并插入数据(插入后记得commit) create table users ( name ), password ) ); '); '); 第二步:编写登陆界面(index ...
- iOS github大全 & iOS7的学习blog
iOS github大全 :有600多个iOS各方面的开源库,并分类了 一天天学习iOS7 :每天学习一点iOS7的新特性
- Android JNI&NDK编程小结及建议
前言 由于网上关于JNI/NDK相关的知识点介绍的比较零散而且不具备参照性,所以写了这篇JNI/NDK笔记,便于作为随时查阅的工具类型的文章,本文主要的介绍了在平时项目中常用的命令.JNI数据类型.签 ...
- zabbix-agent安装报错
最近接触了zabbix,觉得挺好用的,再一次安装agent的过程中,报了如下错误: [root@11005499 ~]# yum install zabbix-agent -y ... groupad ...
- 2016summer 训练第一场
A.http://acm.hdu.edu.cn/showproblem.php?pid=5538 求表面积,只需要将所有的1*1的小块扫描一遍.将每一个块与他相邻四周进行比较,如果该快高度大,则将该快 ...
- JSON API:用 JSON 构建 API 的标准指南中文版
译文地址:https://github.com/justjavac/json-api-zh_CN 假设你和你的团队以前争论过使用什么方式构建合理 JSON 响应格式, 那么 JSON API 就是你的 ...
- 【GLSL教程】(六)逐顶点的光照 【转】
引言 在OpenGL中有三种类型的光:方向光(directional).点光(point).聚光(spotlight).本教程将从方向光讲起,首先我们将使用GLSL来模仿OpenGL中的光. 我们将向 ...
- iOS后台解析
iOS后台 上个月给小妹买了一台6s 她问我双击 Home 键之后 弹出的那些应用会不会耗电 我找到一篇文章 正好说的就是这个问题 摘要翻译一下 原文地址 http://www.speirs.org/ ...
- 每天5道面试题(二)java基础
说出Servlet的生命周期,并说出Servlet和CGI的差别 Servlet被server实例化后,容器执行其init方法,请求到达时执行其service方法,service方法自己主动派遣执行与 ...