0

Using just basic math that you can write on paper (ie, not any software specific tools/features/algorithms) can you help me think of a formula that will do this?

I need to input an integer and return either a 1 or a 0. Input of 0 should return 0, and any other input should always return 1.

I realize for coding purposes I could just use an "if" statement that always returns 1 or 0 depending on whether n == 0.

Ben Mora
  • 123
  • 4

3 Answers3

6

Try $$f(x)=|\text{sgn} (x) |$$

If you feel like making use of the sign function is still ‘cheating’, try this instead: $$f(x) = \bigg\lceil \frac{x^2}{x^2+1}\bigg \rceil$$ Another classic: $$f(x)=\lceil \sin^2x \rceil$$

Vishu
  • 14,469
  • How do you get $f(0) = 0$? – J.-E. Pin Jul 04 '20 at 18:07
  • @J.-E.Pin $\text{sgn}(0)$ is defined to be $0$. See https://en.m.wikipedia.org/wiki/Sign_function. – Vishu Jul 04 '20 at 18:10
  • OK, I didn't know that. – J.-E. Pin Jul 04 '20 at 18:14
  • That will return all sorts of decimals though. I want always 1 or 0. – Ben Mora Jul 04 '20 at 19:16
  • 2
    @BenMora The output of sgn(x) is always $-1,0$ or $1$, and the absolute turns the $-1$ to a $1$. – Vishu Jul 04 '20 at 19:55
  • Maybe I don't know what "sgn" means then. I didn't think it was the same as "sin" but saw a different question somewhere that seemed to be using "sgn" and "sin" interchangeably. I have to assume I am wrong at this point, because the sin of 1 is 0.017 or 0.84 depending on whether deg or rad. – Ben Mora Jul 05 '20 at 04:48
  • 1
    @BenMora Yep. See the link for the definition of sgn(x), then. I’ve also added another function you could use, if you don’t like the sign function. – Vishu Jul 05 '20 at 10:24
  • Thanks for the help! I've learned more than I set out to! – Ben Mora Jul 05 '20 at 20:00
2

A possible solution is $$ f(x) = \min(1, |x|) $$

J.-E. Pin
  • 40,163
  • eh, yes, but "min" is more of a programming thing isn't it? I'm thinking what a highschool level student could come up with based on what they learn in math classes, with no knowledge of CS required. – Ben Mora Jul 04 '20 at 19:17
  • I disagree. min is a very elementary function that can be explained to any 6-year kid. It was used long before programming even existed. – J.-E. Pin Jul 04 '20 at 20:20
  • But how do you do min? Obviously I could use my brain to instantly know the smaller of any two numbers, but how would you prove it? (this might be a not-so-relevant question more for the fun of thinking about it at this point. – Ben Mora Jul 05 '20 at 04:43
  • I just feel like using "min" is almost the same as saying that the formula to answer the question is the question itself. Q: How do you get a 0 or a 1 when given a 0 or a different integer? A: You just pick a 0 when given 0, and pick 1 when given a different integer. – Ben Mora Jul 05 '20 at 04:46
0

This might be helpful to computer scientists.

For any positive number:

f(x) = min(1, ceiling(x))

Returns 0 if x == 0 and 1 if x > 0.

For any number:

f(x) = min(1, ceiling(abs(x)))

Returns 0 if x == 0 and 1 if x != 0.

keith
  • 324
  • Please don't answer old questions that already have answers. That bumps them to the head of the active queue and wastes time for folks like me who follow that queue. – Ethan Bolker Feb 05 '23 at 13:36
  • I feel my answer is valuable. It is much more efficient than the proposed answers for implementation on a computer. – keith Feb 05 '23 at 15:50