Project Euler:Problem 37 Truncatable primes

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and
7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

#include <iostream>
#include <string>
using namespace std;

bool prim(int a)
{
	if (a == 1)
		return false;
	for (int i = 2; i*i <= a; i++)
	{
		if (a%i == 0)
			return false;
	}
	return true;
}

bool tr_prim(int a)
{
	int num = a;
	int count = 0;
	int tmp[10] = { 0 };
	while (a)
	{
		if (!prim(a))
			return false;
		count++;
		tmp[count] = a % 10;
		a /= 10;
	}
	for (int i = count; i > 1; i--)
	{
		num = num - tmp[i] * pow(10, i - 1);
		if (!prim(num))
			return false;
	}
	return true;
}

int main()
{

	int sum = 0;
	for (int i = 10; i <= 1000000; i++)
	{
		if (tr_prim(i))
		{
			//cout << i << endl;
			sum += i;
		}
	}
	cout << sum << endl;
	system("pause");
	return 0;
时间: 2024-08-03 22:20:11

Project Euler:Problem 37 Truncatable primes的相关文章

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 27 Quadratic primes

Euler discovered the remarkable quadratic formula: n2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n

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 first three consecutive numbers to have three distinct prime factors are: 644 = 22 × 7 × 23 645 = 3 × 5 × 43 646 = 2 × 17 × 19. Find the first four co

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 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

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