Home > Mobile >  How to write a code that will count the number of prime digits that consists only of prime numbers
How to write a code that will count the number of prime digits that consists only of prime numbers

Time:09-14

First, I need to input N. For example, from 1 to 5, there is 3 prime number: 2,3,5. Now, I need to find prime numbers that consists only from odd numbers. 53 is it's example.

CodePudding user response:

It's always easier to think about code like this if you partition different functionality off into different functions. You can, theoretically, interweave all the code together, to have code that's checking for prime numbers and odd digits at the same time, but it's much harder to write, read, understand, and debug, so unless there's some compelling reason to compress everything (like, maybe, efficiency), for everyday purposes it's much easier to keep things separate.

So although you haven't quite written your code that way yet, what I'm imagining is that we have a number n we're interested in checking. In your case it's the loop variable in your for(i = 2; i <= N; i ) loop. If you were just counting primes, you'd have

if(isprime(n))
    count  ;

Here I'm imagining that you have a separate function called isprime() that you can call to see if a number is prime. (It would contain the same kind of code you currently have for setting your isPrime variable to true or false.) And then, if you want to count how many of the prime numbers have only odd digits, you might go to something like this:

if(isprime(n)) {
    count  ;
    if(has_only_odd_digits(n))
        count2  ;
}

Arranged this way, count will be the count of how many numbers were prime, and count2 will be the count of how many numbers were prime and had only odd digits. (Different arrangements are obviously possible to answer different variants of the question, like how many numbers had only odd digits, whether or not they were prime.)

And then the obvious question is: Where does the hypothetical has_only_odd_digits function come from? We'll have to write it ourselves, of course, but the nice thing is that while we're writing it, we'll only have to think about the problem of determining whether digits are even or odd; we won't have to worry about prime numbers at all.

So let's take a first cut at writing has_only_odd_digits. You had a version, sort of, in the code you originally posted:

int has_only_odd_digits(int n)
{
    if(n % 2 == 0)    /* if even */
         return 0;
    else return 1;
}

But this is wrong: it just tests whether the number n itself is even or odd. It will return "false" for 2 and 20 and 222, and it will return "true" for 3 and 13 and 13579, but it will also return "true" for 2461, since 2461 is odd, even though it happens to have mostly even digits. So for a working version of has_only_odd_digits we need something more like this:

int has_only_odd_digits(int n)
{
    for(each digit d of n) {
        if(d % 2 == 0)
            return 0;
    }
    return 1;
}

This is pseudocode, not real C code yet. Notice that this time there's no else in the if statement. If we find an even digit, we're done. But if we find an odd digit, we can't necessarily return "true" yet — we have to check all the digits, and only return true" if none of the digits were even.

But the real question is, how do we get at those digits? An int variable is not represented internally by anything remotely like an array of its decimal digits. No, an int variable is an integer, a number, and the only way to get at its decimal-digit representation is to, literally, divide it repeatedly by 10.

So, fleshing out our code just a little bit more, we're going to have something like

int has_only_odd_digits(int n)
{
    while(n != 0) {
        int d = one digit of n;
        if(d % 2 == 0)
            return 0;
        n = the rest of n, except for the digit d we just checked;
    }
    return 1;
}

And that's as far as I'm going to take it: I think you've got the idea by now, and in case this is a homework assignment, I'm not going to do all the work for you. :-) I'll note that there's also still a discrepancy to nail down between your question title and your problem description in the question: are you looking for prime numbers with odd digits, or prime digits? (And if the latter, I'm honestly not sure what to do with 1's, but that'll be a question for you and your instructor, I guess.)

But, in closing, I want to point out a couple of things: besides learning how to count primes with odd digits, I hope you've also also begun to appreciated two, much more general, related points:

  1. If you break a complicated program up into separate functions, it's much easier to think about each function (each part of the problem) in isolation.

  2. If you're not sure how to do something, often you can attack it in little steps, sort of a "successive refinement" approach, like I did with the several different versions of the has_only_odd_digits function here.

Now, it's true, you can't necessarily just break a complicated problem up into functions any old way, and when you're solving a problem by stepwise refinement, you have to choose the right steps. But, still, these are definitely valuable approaches, and with time and experience, you'll find it easier and easier to see how to break a big problem down into pieces you can handle.

  • Related