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. php通过$_SERVER['HTTP_USER_AGENT']获取浏览器相关参数

    最近不忙,同事在忙一个app项目.当听到领导安排让他做一个判断苹果还是安卓手机,如果是安卓手机下载安卓app.如果是苹果手机下载苹果app;然后我就上网搜了一下学习学习: php通过$_SERVER[ ...

  2. AC日记——食物链 codevs 1047

    1074 食物链 2001年NOI全国竞赛  时间限制: 3 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 动物王国中有 ...

  3. Codeforces Gym100735 I.Yet another A + B-Java大数 (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    I.Yet another A + B You are given three numbers. Is there a way to replace variables A, B and C with ...

  4. Springboot如何优雅的解决ajax+自定义headers的跨域请求

    1.什么是跨域 由于浏览器同源策略(同源策略,它是由Netscape提出的一个著名的安全策略.现在所有支持JavaScript 的浏览器都会使用这个策略.所谓同源是指,域名,协议,端口相同.),凡是发 ...

  5. 转:ospf学习-----SPF最短路径算法

    ospf学习-----SPF最短路径算法 常见的路由协议比如RIP.IGRP.BGP是距离矢量协议,OSPF和ISIS是数据链路状态协议.矢量协议路由器只知道本身和与自身相连的接口路由信息,矢量图只是 ...

  6. workflow engine Ruote 安装

    今天在安装gem安装Ruote的过程中遇到问题,改用bundle安装: steven@steven-Latitude-D630:/usr$ sudo mkdir bundel [sudo] passw ...

  7. mysql-实现远程连接(授权法)

    远程连接阿里云主机的mysql,遇到以下问题: 1.连接被拒,无法连接 可能原因:1.3306(默认)端口未开放,在控制台设置防火墙规则: 2. host字段的值改为%就表示在任何客户端机器上能以ro ...

  8. 数据库访问的弹性化---WebLogic和Oracle RAC的整合:Active GridLink

        1.  什么是Active GridLink Data Source 从Oracle WebLogic Server 10.3.4版本开始引进了一种单数据源实现来支持Oracle RAC集群. ...

  9. appium 'WebDriver' object has no attribute 'keyevent'

    这个问题是我自己犯二了,开头应该是from appium import webdriver,写成了from selenium import webdriver,也可以运行,就是不能使用appium中独 ...

  10. 【开发总结】—— BABYLON 3D开发常见问题及解决方法

    前言:组内同事们根据长时间的Babylon.js开发实践,一起将项目开发中遇到的问题及解决方法做了一个梳理. ios [最好] 关闭离线缓存—— 解决添加了反射的mesh 丢失的问题 不要使用 pos ...