1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Modulo arithmetic

  1. Sep 9, 2013 #1


    User Avatar
    Homework Helper

    I've stumbled across an arithmetic problem that's getting the better of me, so I need your help!

    I have a constant set of integers [itex][m,n], m>0, m\neq n[/itex] and a variable integer k>0. If we multiply k by successively increasing positive integers t, we will eventually get kt > m. Now, what I want is the output to be kt if [itex]kt \in [m,n][/itex] and n if [itex]kt>n[/itex].

    So for example, let's consider m = 45, n = 50 and k = 8.

    We get the sequence 8, 16, 24, 32, 40, 48

    At this point, we have a value larger or equal to m=45, and 48 < n=50 so the output is 48.

    For k=11 we have
    11, 22, 33, 44, 55

    Since we now have a value larger than m, but the value is also larger than n, the output needs to be n=50.

    So finally, my question is, can I create a function that isn't piecewise that defines this process? The piecewise function wasn't such a big deal,

    f(k,m,n) =
    \Big\lceil \frac{m}{k} \Big\rceil k, & \text{if}\hspace{5 mm} \Big\lceil \frac{m}{k} \Big\rceil k < n \\ \\
    n, & \text{if}\hspace{5 mm} \Big\lceil \frac{m}{k} \Big\rceil k \geq n

    But I can't figure out how to produce a non-piecewise function.
  2. jcsd
  3. Sep 9, 2013 #2


    User Avatar
    2017 Award

    Staff: Mentor

    [m,n] means the interval from m to n?

    So t is the smallest integer t such that kt >=m?

    You can implement min(a,b) (the smaller value of a and b) as (|a+b|-|a-b|)/2, I think that should help.
  4. Sep 9, 2013 #3


    User Avatar
    Homework Helper

    Yes and yes. Sorry, I should have been more clear when defining my variables.

    That's perfect! Thanks mfb.
  5. Sep 9, 2013 #4


    User Avatar
    Homework Helper

    Now a method to implement absolute value that isn't piecewise would be needed.
    Last edited: Sep 9, 2013
  6. Sep 9, 2013 #5


    User Avatar
    Homework Helper

    Good point, but this problem was actually for a computing project of mine and since the abs(x) function already exists as a library function, its piecewise definition isn't an issue.
  7. Sep 9, 2013 #6


    User Avatar
    Homework Helper

    For 32 bit signed two complement integers, and assuming the compiler does an arithmetic right shifts:

    #define abs(a) ((a^(a>>31))+((a>>31)&1))
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Threads - Modulo arithmetic Date
Find α+β+αβ Apr 11, 2017
Congruence Modulo Proof Feb 27, 2014
Number of theory modulo Oct 17, 2012
Problem ; modulo p Oct 13, 2012
I don't know how to find the four square roots of an elliptic curve modulo a prime Jul 19, 2012