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;
}
时间: 2024-09-29 09:30:09

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

Project Euler:Problem 50 Consecutive prime sum

The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds t

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 once; for example, the 5-digit number, 15234, is 1 through 5 pandigital. The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing mult

Project Euler:Problem 38 Pandigital multiples

Take the number 192 and multiply it by each of 1, 2, and 3: 192 × 1 = 192 192 × 2 = 384 192 × 3 = 576 By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3) The same c

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

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 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way: 28 = 22 + 23 + 24 33 = 32 + 23 + 24 49 = 52 + 23 + 24 47

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 + 2 3 + 3 + 2 + 2 2 + 2 + 2 + 2 + 2 What is the first value which can be written as the sum of primes in over five thousand different ways? #include <i

Project Euler:Problem 58 Spiral primes

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18  5  4  3 12 29 40 19  6  1  2 11 28 41 20  7  8  9 10 27 42 21 22 23 24 25 26 43 44 45 46

Project Euler:Problem 69 Totient maximum

Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9