c program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sekitoleko
    New Member
    • Sep 2006
    • 21

    c program

    how can i write down a simple c expression that evaluates to the largest multiple of a number n,not greater than a given number m.
  • Soujiro
    New Member
    • Jan 2007
    • 35

    #2
    Originally posted by sekitoleko
    how can i write down a simple c expression that evaluates to the largest multiple of a number n,not greater than a given number m.
    Now that's it.. You need to study first.. Banfa's Tutorial

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      Simplest (though probably not most efficient) way would be to start with m and test whether n mod m = 0 if it does we have a winner!! If it doesn't then decrement m and start again.

      This is no easy feat, and in fact the strength of public key Cryptography, relies on (roughly) this problem being difficult for large m. If you had a nice long list of primes (where the largest prime is the largest prime below m) [p1, p2, p3, p4, p5, p6, ....], however, you could try a slightly different approach:
      (Not in any particular languag but the flow of logic is the important thing)
      Code:
      currentDivisor = 1;
      currentPrime = primeList.start();
      while(currentDivisor < m)
      {
        if(n % currentPrime ==0)
        {
          currentDivisor = currentDivisor * currentPrime;
        }
        else
        {
          currentPrime = primeList.next();
        }
      }
      NOTICE: the current prime is not changed if the current prime divides and there is good reason for this. If the number is divisible by, say, 9 then we need to test divisibility by 3 more than once.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by DeMan
        Simplest (though probably not most efficient) way would be to start with m and test whether n mod m = 0 if it does we have a winner!! If it doesn't then decrement m and start again.
        We are looking for multiples of n so I think you mean

        if m mod n == 0 your on to a winner

        However if m mod n != 0 then

        (m - m mod n) mod n == 0 so (m - m mod n) is your answer.

        Actually to avoid an if statement (m - m mod n) is always your answer since if m mod n == 0 then (m - m mod n) == (m - 0) == m.

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          Sorry you're right - I shouldn't read without thinking,

          You are absolutely right!!

          Comment

          • cworld
            New Member
            • Jan 2007
            • 15

            #6
            Originally posted by sekitoleko
            how can i write down a simple c expression that evaluates to the largest multiple of a number n,not greater than a given number m.
            yes i think first You need to study some basic Tutorial

            Comment

            Working...