Task: Smarandache prime-digital sequence


C++: #include <iostream>
#include <cstdint>

using integer = uint32_t;

integer next_prime_digit_number(integer n) {
    if (n == 0)
        return 2;
    switch (n % 10) {
    case 2:
        return n + 1;
    case 3:
    case 5:
        return n + 2;
    default:
        return 2 + next_prime_digit_number(n/10) * 10;
    }
}

bool is_prime(integer n) {
    if (n < 2)
        return false;
    if (n % 2 == 0)
        return n == 2;
    if (n % 3 == 0)
        return n == 3;
    if (n % 5 == 0)
        return n == 5;
    constexpr integer wheel[] = { 4,2,4,2,4,6,2,6 };
    integer p = 7;
    for (;;) {
        for (integer w : wheel) {
            if (p * p > n)
                return true;
            if (n % p == 0)
                return false;
            p += w;
        }
    }
}

int main() {
    std::cout.imbue(std::locale(""));
    const integer limit = 1000000000;
    integer n = 0, max = 0;
    std::cout << "First 25 SPDS primes:\n";
    for (int i = 0; n < limit; ) {
        n = next_prime_digit_number(n);
        if (!is_prime(n))
            continue;
        if (i < 25) {
            if (i > 0)
                std::cout << ' ';
            std::cout << n;
        }
        else if (i == 25)
            std::cout << '\n';
        ++i;
        if (i == 100)
            std::cout << "Hundredth SPDS prime: " << n << '\n';
        else if (i == 1000)
            std::cout << "Thousandth SPDS prime: " << n << '\n';
        else if (i == 10000)
            std::cout << "Ten thousandth SPDS prime: " << n << '\n';
        max = n;
    }
    std::cout << "Largest SPDS prime less than " << limit << ": " << max << '\n';
    return 0;
}




Java: public class SmarandachePrimeDigitalSequence {

    public static void main(String[] args) {
        long s = getNextSmarandache(7);
        System.out.printf("First 25 Smarandache prime-digital sequence numbers:%n2 3 5 7 ");
        for ( int count = 1 ; count <= 21 ; s = getNextSmarandache(s) ) {
            if ( isPrime(s) ) {
                System.out.printf("%d ", s);
                count++;
            }
        }
        System.out.printf("%n%n");
        for (int i = 2 ; i <=5 ; i++ ) {
            long n = (long) Math.pow(10, i);
            System.out.printf("%,dth Smarandache prime-digital sequence number = %d%n", n, getSmarandachePrime(n));
        }
    }
    
    private static final long getSmarandachePrime(long n) {
        if ( n < 10 ) {
            switch ((int) n) {
            case 1:  return 2;
            case 2:  return 3;
            case 3:  return 5;
            case 4:  return 7;
            }
        }
        long s = getNextSmarandache(7);
        long result = 0;
        for ( int count = 1 ; count <= n-4 ; s = getNextSmarandache(s) ) {
            if ( isPrime(s) ) {
                count++;
                result = s;
            }
        }
        return result;
    }
    
    private static final boolean isPrime(long test) {
        if ( test % 2 == 0 ) return false;
        for ( long i = 3 ; i <= Math.sqrt(test) ; i += 2 ) {
            if ( test % i == 0 ) {
                return false;
            }
        }
        return true;
    }

    private static long getNextSmarandache(long n) {
        
        if ( n % 10 == 3 ) {
            return n+4;
        }
        long retVal = n-4;
        
        
        int k = 0;
        while ( n % 10 == 7 ) {
            k++;
            n /= 10;
        }
        
        
        long digit = n % 10;

        
        long coeff = (digit == 2 ? 1 : 2);
        
        
        retVal += coeff * Math.pow(10, k);
        
        
        while ( k > 1 ) {
            retVal -= 5 * Math.pow(10, k-1);
            k--;
        }
        
        
        return retVal;
    }

}