Project Euler Problem 41 Pandigital prime

Pandigital prime

Problem 41

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 ton exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?


C++(Faster):

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 9;

int val[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int v[N];

inline long getval(int n)
{
    long value = 0;
    for(int i=n; i<N; i++) {
        value *= 10;
        value += val[i];
    }

    prev_permutation(val + n, val + N);

    return value;
}

inline bool isprime(long n)
{
    if(n % 2 == 0)
        return false;

    long end = sqrt(n);

    for(int i=3; i<=end; i+=2)
        if(n % i == 0)
            return false;

    return true;
}

int main()
{
    // 1+2+3+4+5+6+7+8+9 = 45 = 3 * 15  x=3k not is prime
    // 1+2+3+4+5+6+7+8 = 36 = 3 * 12
    // 1+2+3+4+5+6 = 21 = 3 * 7
    // 1+2+3+4+5 = 15 = 3 * 5 
    // 1+2+3 = 6 = 3 * 2
    // 1+2 = 3 = 3 * 1
    
    long value, max;
    int digits[] = {N-7, N-4};

    for(int i=0; i<2; i++) {
        max = value = getval(digits[i]);
        for(;;) {
            if(isprime(value)) {
                cout << value << endl;
                i = N;
                break;
            } else {
                value = getval(digits[i]);
                if(value == max)
                    break;
            }
        }
    }

    return 0;
}



C++(Simpler):

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 9;

int val[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int v[N];

inline long getval(int n)
{
    long value = 0;
    for(int i=n; i<N; i++) {
        value *= 10;
        value += val[i];
    }

    prev_permutation(val + n, val + N);

    return value;
}

inline bool isprime(long n)
{
    if(n % 2 == 0)
        return false;

    long end = sqrt(n);

    for(int i=3; i<=end; i+=2)
        if(n % i == 0)
            return false;

    return true;
}

int main()
{
    long value, max;

    for(int i=0; i<N; i++) {
        max = value = getval(i);
        for(;;) {
            if(isprime(value)) {
                cout << value << endl;
                i = N;
                break;
            } else {
                value = getval(i);
                if(value == max)
                    break;
            }
        }
    }

    return 0;
}


C++:

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 9;

int val[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int v[N];

inline void copyval(int n)
{
    for(int i=n; i<N; i++)
        v[i] = val[i];
}

inline long getval(int n)
{
    long value = 0;
    for(int i=n; i<N; i++) {
        value *= 10;
        value += v[i];
    }

    prev_permutation(v + n, v + N);

    return value;
}

inline bool isprime(long n)
{
    if(n % 2 == 0)
        return false;

    long end = sqrt(n);

    for(int i=3; i<=end; i+=2)
        if(n % i == 0)
            return false;

    return true;
}

int main()
{
    long value, max;

    for(int i=0; i<N; i++) {
        copyval(i);

        max = value = getval(i);
        for(;;) {
            if(isprime(value)) {
                cout << value << endl;
                i = N;
                break;
            } else {
                value = getval(i);
                if(value == max)
                    break;
            }
        }
    }

    return 0;
}



posted on 2017-03-28 18:33  海岛Blog  阅读(157)  评论(0编辑  收藏  举报

导航