One line function to find n mod 4 (n % 4)

In summary: Anyway, the point is that if you use %, you should add a line or two to make sure the output is what you want it to be. In summary, the conversation was about finding a one line function to calculate the remainder of a number when it is divided by 4. Several suggestions were given, including using boolean AND, the formula r= (1-(-1)^n)/2, and the algorithm r=n - 4 * floor(n/4). The use of % as a solution was also mentioned, but it was noted that its behavior may not be consistent in all cases. The conversation also touched on the idea of limiting oneself to only certain operators as a mental exercise, but it was ultimately agreed that using a
  • #1
PrakashPhy
35
0
Could anybody help me find a one line function to calculate the remainder of a number when it is divided by 4

For example
A one line function to calculate the remainder when the number is divided by two looks like.

r= (1-(-1)^n)/2

Thank you in advance.
 
Physics news on Phys.org
  • #2
Use boolean AND.

C language example:
remainder = N && 3 ;

A bash example:
remainder=$(( $N & 3 ))
 
  • #3
Rogerio said:
C language example:
remainder = N && 3 ;
Bah, just use (N % 4). In this day and age, pretty much the only effect of such cleverness is to obfuscate code.
 
  • #4
Hurkyl said:
Bah, just use (N % 4). In this day and age, pretty much the only effect of such cleverness is to obfuscate code.

Wrong.
The principal effect is the ability to run faster.
 
Last edited:
  • #5
Rogerio said:
Wrong.
The principal effect is the ability to run faster.
It doesn't. Any compiler worth its salt produces the exact same code in both cases. (at least, with optimization turned on)
 
  • #6
Rogerio said:
Wrong.
The principal effect is the ability to run faster.

If your compiler isn't optimizing this for you, and you care about it, you probably should be using a different compiler that is better suited to the job. Or look for bottlenecks in other parts of the code.

Anyway, my understanding is that gcc will optimize if you use const.
 
  • #7
PrakashPhy said:
Could anybody help me find a one line function to calculate the remainder of a number when it is divided by 4

For example
A one line function to calculate the remainder when the number is divided by two looks like.

r= (1-(-1)^n)/2

Thank you in advance.



Hey man, please look the following function:-

R = ((1-(-1)^n)/2)(2+(-1)^(((n+1)/2)((1-(-1)^n)/2)))-((1+(-1)^n)/2)((-1)^((n/2)((1-(-1)^(n+1))/2))-1)

And please help to simplify it.
 
  • #8
And please other friends, try to understand what prakashphy is searching for.
 
  • #9
scimad said:
And please other friends, try to understand what prakashphy is searching for.
To be quite honest,
r= (1-(-1)^n)/2​
is probably not the answer prakashphy was searching for to the case of n mod 2, even though he thinks it is. We can't be sure until he chimes back in with an explanation of what he is actually trying to do.
 
  • #10
Hurkyl said:
To be quite honest,
r= (1-(-1)^n)/2​
is probably not the answer prakashphy was searching for to the case of n mod 2, even though he thinks it is. We can't be sure until he chimes back in with an explanation of what he is actually trying to do.

I am pretty much sure that r=(1-(-1)^n)/2 and R=... that I have given earlier as a reply is exactly what he wanted. And in fact did u (Hurkyl) try the formula of N mod 4? N mod 2 is sure to work. If you are interested then try n mod 3? I mean make a mathematical function which passes a number n as parameter and gives the remainder when divided by 3 as the output of the formula ( function) and that should be one line.. I hope you got it...!
Thanks.
:mad:
 
  • #11
Hurkyl said:
To be quite honest,
r= (1-(-1)^n)/2​
is probably not the answer prakashphy was searching for to the case of n mod 2, even though he thinks it is. We can't be sure until he chimes back in with an explanation of what he is actually trying to do.

Be sure that r= (1-(-1)^n)/2 is what he is searching, because we are both as part of the problem trying for the solution.
 
  • #12
It would help to know why you need this, instead of having all of us guessing. Do you need to apply analytical methods to it, for example?

Here is a simpler way, but again maybe it's not what you want. You can use
[tex]r=n - 4 \times \lfloor n / 4 \rfloor[/tex]​
where the brackets stand for the floor function.
 
  • #13
scimad said:
I am pretty much sure that r=(1-(-1)^n)/2 and R=... that I have given earlier as a reply is exactly what he wanted.
*shrug* All I have to say is that I have a lot of experience with people getting an idea in their head (e.g. I shall only consider expressions involving +-*/^) and limiting their options to solve a problem, when such limitations simply aren't relevant.


And in fact did u (Hurkyl) try the formula of N mod 4? N mod 2 is sure to work. If you are interested then try n mod 3?
There is a straightforward algorithm to create such an expression involving only +-*/^. The function (-1)x/m is periodic with period m. So you just need to feed it into some function that moves the m different values where you want them to go. (done, for example, by polynomial interpolation)
 
  • #14
i actually searched for what !scimad! came up with.

just for a nice exercise of brain i was limiting myself with ^/*+- only. it may not make a sense in limiting with such operators only but it gives pleasure when we get the problem solved with limited resource.

thank you for your words shared and what scimad got works.
now let's try such a function for n mod 3.
 
  • #15
Hurkyl said:
Bah, just use (N % 4). In this day and age, pretty much the only effect of such cleverness is to obfuscate code.

It's well worth noting that in C or C++, the % operator may not do what you want if N is negative. Worse, the behavior is platform-dependent in that case.

E.g. using some compilers, -1 % 4 returns -1, not 3.
 
  • #16
jbunniii said:
It's well worth noting that in C or C++, the % operator may not do what you want if N is negative. Worse, the behavior is platform-dependent in that case.

E.g. using some compilers, -1 % 4 returns -1, not 3.

That's fair -- I generally try to use unsigned integers so I didn't think of this. It's not platform-dependent, though: I'm pretty sure that the C standard demands that -1%4 == -1. (this is a consequence of its demand that -1/4 == 0)
 
  • #17
Hurkyl said:
That's fair -- I generally try to use unsigned integers so I didn't think of this. It's not platform-dependent, though: I'm pretty sure that the C standard demands that -1%4 == -1. (this is a consequence of its demand that -1/4 == 0)

I believe the behavior is standardized in C99, but in C90 it was platform-dependent. Not 100% sure about C++. I too try to avoid signed integers whenever possible.
 

FAQ: One line function to find n mod 4 (n % 4)

What is the purpose of the "One line function to find n mod 4 (n % 4)"?

The purpose of this function is to find the remainder when a number (n) is divided by 4.

How does the "One line function to find n mod 4 (n % 4)" work?

This function uses the modulus operator (%) to divide the number (n) by 4 and return the remainder as the result.

What input can be used for the "One line function to find n mod 4 (n % 4)"?

The input for this function can be any integer or decimal value.

What is the output of the "One line function to find n mod 4 (n % 4)"?

The output of this function will always be an integer between 0 and 3, representing the remainder when the input number (n) is divided by 4.

Can the "One line function to find n mod 4 (n % 4)" be used for negative numbers?

Yes, this function can be used for negative numbers as well. The remainder will be calculated based on the absolute value of the input number.

Similar threads

Replies
13
Views
2K
Replies
1
Views
1K
Replies
4
Views
1K
Replies
5
Views
2K
Replies
17
Views
2K
Replies
10
Views
1K
Replies
8
Views
1K
Back
Top