views:

87

answers:

3

I tried using 6%2, but its always giving the value as 2 and not 0. Why and how can I get a solution to this?

A: 
bool prime = PrimeTool.IsPrime(input_Number);
        if (!prime)
        {
            Console.Write("multiple of other number");
            Console.WriteLine(i);
        }
william
I don't think he asked for that @william. He's asking if `y` is a multiple of `x`, (that is, if `y = kx` for some integer `k` ) and not if **x** has other divisors except `1` and `x`
Ed.C
I dun get what you mean..u said.. y=kx, x is a prime number. so.. what is k?
william
@william: You can't use a number's prime/composite status alone to determine whether it's an integral multiple of a specific other number. Let `y = 8` and `x = 3`. `y` is not prime, but its factors are 1, 2, 4 and 8, so 8 is not a multiple of 3. In the same vein, there is no integer `k` that would satisfy the equation `y = kx`.
BoltClock
so.. what does he really want?
william
so.. what does he really want?what is the question?
william
@william, I apologize, my comment above is really messy. Let me start again with prime numbers, some of them: ...-3,-1,0,1,3,5,7,11,13,17,19,23,29 ... As you can notice `"Isprime"` is a **one-argument-operation**. What @wolverine asked is a different **two-argument-operation**. **Y** Multiple **X** in this case means "if I divide **Y** BY **X**, is the result still integer?" - Mathematical definition: "Is there **any** **integer** value **K**, for which **Y = KX** evaluates to **true**? -This is what I was trying to say. Again, keep in mind that these operations have meaning in **Z domain**
Ed.C
+3  A: 
if(!(y%x))
{
...
}

In your case !(6%2) would return true.

(Answer very similar to the original in the question)

Ed.C
A: 

I'm asuming that you want to find out if Y=kX has integer values of k for a given X so that Y=5, X=3 fails (k is 5/3), but Y=6, X=2 passes (k is exactly 3). You are happy that k is either positive or negative.

That way, using Y remainder X == 0 is a good test. As an aside, be careful of negative remainders (e.g. Y % 2 == 1 as a test for oddness fails for negative numbers, use Y % 2 != 0 to be sure)

Code example in Java

public class Example {

  public static void main(String[] args) {
    System.out.println(isIntegerFactor(5,3));  // k is not an integer
    System.out.println(isIntegerFactor(6,3));  // k is 2
    System.out.println(isIntegerFactor(-6,-3)); // k is 2 
    System.out.println(isIntegerFactor(-6,3)); // k is -2
    System.out.println(isIntegerFactor(6,-3)); // k is -2
  }

  public static boolean isIntegerFactor(int y, int x) {
    return (y % x) == 0;
  }

}
Gary Rowe