tags:

views:

160

answers:

2

Hi. I need to create a method, in the following signature:

int x (int y);

That's the example of values that this it should return:

x(3) = 1
x(4) = 1
x(5) = 2
x(6) = 2
x(7) = 3
x(8) = 3
x(9) = 4
x(10) = 4
...

Any ideas how could I do it?

Thank you.

EDIT: That's what I've got so far:

    static int x(int y)
    {
        return (y / 2) - 1;
    }

but the problem is that:

x(3) = 0
x(4) = 1
x(5) = 1
x(6) = 2
+6  A: 

Subtract 1 then integer divide by 2.

Ignacio Vazquez-Abrams
Thank you! but I should help in Stack Overflow when there's a test... Can you please tell me how did you find it? Thanks!
TTT
It's a stepped linear progression. "Stepped" means that flooring or integer division is involved, and "linear" means multiplication by a constant. Shifting the progression so that the x-intercept is at the origin gives a translation of -1, and the slope being 1/2 means division by 2.
Ignacio Vazquez-Abrams
+3  A: 

If you want to make a joke of those who asked you to do this (and if you know only values for 3 .. 10), you could also write the following method:

static int x(int y) {
  return (int)(10.0 * Math.Sin((double)y / 21.0));
}

It's probably not what they meant, but it should give the same results for arguments from 3 to 10 :-). And how did I find it? I know the graph of sin function, which is ascending in the beginning. Then I just tried to find out some 'magic constants' to find a configuration in which it returns the numbers you wanted...

Tomas Petricek
funny, but they ended the sequence with "...", so it looks like he is accepting numbers from 3 to infinity.
Wallacoloo
`(int)(10 * Math.Sin(y / 21d))`
Yuriy Faktorovich
@Yuriy: thanks, fixed. @wallacoloo: Well, the function works for numbers from 3 to infinity. It just gives different results than the most obvious function, but that doesn't mean its wrong (if you know only values [x(3), ..., x(10)] - if you knew more values, you could decide which one is correct, or find other functions...).
Tomas Petricek