What Does This Function Do?

1. Please, give your answers in comments – what does the function written below do:

a)      recognizes prime numbers

b)      recognizes prime numbers in interval [100;999]

c)      neither a) nor b) is correct

d)      other variant (please, specify)

2. And the second question is – is this function a correct solution if the task is as follows: `Please, write a function that recognizes all the prime numbers consisting of exactly 3 digits`:

a) yes

b) no

The function (in C++):

bool f(int x) {

 for (int y=2;y<x;y++)

  if (x%y==0) return false;

 return true;

}

P.S. I wish all of you wrote your thoughts in order to make a student of mine understand the essence of the thing!

Advertisements

The URI to TrackBack this entry is: https://edgarsr.wordpress.com/2008/01/25/what-does-this-function-do/trackback/

RSS feed for comments on this post.

5 CommentsLeave a comment

  1. 1: d) prime numbers and prime numbers (100,999), because there is no exception that would allow to get all prime numbers except those from 100 to 999.

    2: The actual question to the task was to “create function that recognises prime numbers from 3 digits”. There was no word for “exactly”, saying the function has to be limited from 100 to 999.

    But answer to your question here is: b) no, it does not.

  2. OK, but it is quite understandable – if there is said that the function must recognize 3-digital prime numbers, then it is not done with no purpose 😉

    Then, you could notice it was not said the function must recognize EXACTLY prime numbers 😉 And so you could offer a function recognizing every integer as a correct solution 😀

    bool f(int x) {return true;}

  3. My solution involved creating all prime numbers recognising function with limiting input to numbers from 100 to 999.
    so:

    do i check numbers with 3 digits? yes
    does function check 3 digit numbers? yes
    problem solved.

  4. Exactly – like you said, your solution was a function recognizing all prime numbers that is not correct (or what is as correct as a function recognizing all integers).

    Whether or not you did other part of the whole task, it is a different talk (but if we talk about it, I have to admit the other part was correct, so you got half of a score overall).

  5. Vecais labais php. Ko vēl pastāstīt


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: