A prime number (or a prime) is a natural number greater than 1 that can only be divided by 1 and itself.
Ex-First few Prime number are-
2, 3, 5, 7, 11, 13, 17, 19,...
A digit prime is a prime number whose sum of digits is also prime.
For example the prime number 23 is a digit prime because 2+3=5 and 5 is a prime number. 17 is not a digit prime because 1+7 = 8, and 8 is not a prime number. Write a program to find out the number of digit primes within a certain range less than 1000000.and also print the Mth digit prime number between the range.
**

Recommended Answers

All 8 Replies

Something like this?

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

#define LOWER_BOUND (0)
#define UPPER_BOUND (1000000)   // Not inclusive.

typedef void (*prime_callback)(unsigned int);

static unsigned int       digit_prime_count = 0;
static const unsigned int M                 = 10;  // Starting from 1.


bool is_prime(const unsigned int n)
{
    int i;

    if (n >= 2)
    {
        for (i = 2; i <= (unsigned int) sqrt(n); i++)
            if (n % i == 0) return false;

        return true;
    }
    return false;
}


bool is_digit_prime(unsigned int n)
{
    char digit_sum = 0;

    if (is_prime(n))
    {
        while (n > 0)
        {
            digit_sum += n % 10;
            n          = n / 10;
        }
        return is_prime(digit_sum);
    }
    return false;
}


void process_digit_primes(      unsigned int   lower_bound,
                          const unsigned int   upper_bound,
                          const prime_callback f)
{
    while(lower_bound < upper_bound)
    {
        if (is_digit_prime(lower_bound))
            f(lower_bound);

        lower_bound++;
    }
}


void print_prime(unsigned int n)
{
    digit_prime_count++;

    if (digit_prime_count == M)
        printf("%dth digit prime: %u.\n", M, n);
}


int main(void)
{
    process_digit_primes(LOWER_BOUND, UPPER_BOUND, print_prime);
    printf("The amount of digit primes in the interval [%u,%u): %u\n",
                        LOWER_BOUND, UPPER_BOUND, digit_prime_count);
    return 0;
}

std bool is not working

std bool is not working

I believe it's a C99 thing.. Compile with the C99 compiler flag or just replace every bool with unsigned char, every true with 1 and every false with 0.

Or replace #include <stdbool.h> with

#define true  (1)
#define false (0)
#define bool  unsigned char

can i have this code in java?

Atleast the trolls are working as intended...

Showing once again why volunteering a program, without the original poster doing any work whatsoever, is a bad idea.

Whether it's homework, or a problem solving website like SPOJ or Euler Project, having us code up a gratis program for them, is just wrong. They did none of the work.

And can you code that in Lisp, Fortran, ADA, Go, Rust, Scheme, Python, Perl, and Brainfuck, for me?

commented: nods +14

I understand the concern and I've had discussions about it before. I (still) think that people don't really learn anything if you have to force them to do it. Sure, I think it's stupid to just copy paste code from the internet and I'm sure that'll come back to bite them in the ass, but that's not my problem. I like doing these kind of small simple problems just as a distraction between working on more complex things.. But I'll try to meet somewhere in the middle :<

And can you code that in Lisp, Fortran, ADA, Go, Rust, Scheme, Python, Perl, and Brain####, for me?

Don't challenge me :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.