Gcd

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AmLegacy
    New Member
    • Feb 2008
    • 6

    Gcd

    Can anyone help me with coding the GCD? I want to take two integers 56 and 42 and divide them x amount of times until the remainder is 0. Once the remainder is 0 that divisor becomes my GCD.
  • AmLegacy
    New Member
    • Feb 2008
    • 6

    #2
    int my_gcd (int *m, int *n)

    {
    while (m > 0)
    {
    if (n > m)
    { int t = m; m = n; n = t; }

    m -= n;

    }

    }

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Originally posted by AmLegacy
      int my_gcd (int *m, int *n)

      {
      while (m > 0)
      {
      if (n > m)
      { int t = m; m = n; n = t; }

      m -= n;

      }

      }
      So your code should be directly translatable to pseudocode. Did you try working this out by habd? What if n is less than n (ie, the numbers are reversed)? I think you should put more thought into your algorithm before you start coding.

      Comment

      Working...