Help with some logic questions

This is a discussion on Help with some logic questions within the C++ Programming forums, part of the General Programming Boards category; Okay I am a basic C++ student in high school...I am to write a function on getting a GCF from ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    15

    Help with some logic questions

    Okay I am a basic C++ student in high school...I am to write a function on getting a GCF from 2 integers...I am first thinking of the logic of the problem first and how to solve it...So far I've come up with one idea, I am not so sure it is good but its what my mind has come up with...Please either tell me that its a bad method on trying to do this and if it is a bad method, what is a better one?

    Okay I am going to find the prime factors of each numbers. Then I will multiply the common numbers together to get the GCF...all idea's are welcome

    Also for another program I am working on, is zero considered an even number?(I'm writing another function to remove all even numbers from an array and I am wondering if zero is included as an even number)

  2. #2
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I don't remember enough about GCM, GCF, LCM, LCF to tell you the answers to the first. But for 0 being an even number I would have to say yes. Because

    Code:
    (0 % 2 == 0)
    is a true statement.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes, 0 is an even number
    http://mathworld.wolfram.com/EvenNumber.html
    An integer of the form n=2k , where n is an integer. The even numbers are therefore ..., -4, -2, 0, 2, 4, 6, 8, 10, ... (Sloane's A005843).
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You can write a quick, not to mention easier to code, nonrecursive algorithm with these facts
    gcd(a, 0) = a
    Since a divides a and 0

    gcd(a, b) = gcd(b, a % b)
    Suppose there is a g that divides a and b. Does that
    g divide a % b? Yes, simply let r = a % b and you
    have a = bq + r where q and r are integers such that 0 <= r < q.

    This algorithm is called Euclid's algorithm and it's supposed to be
    the oldest non-trivial algorithm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic questions
    By zacs7 in forum Game Programming
    Replies: 1
    Last Post: 05-17-2008, 02:09 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21