-1

I want to know if A and B are relatively prime using Euclidean Algorithm. A and B are large numbers that cannot be stored in any data type(in C), so they are stored in a linked list. In the algorithm, the operator % is used. My question is, is there a way to compute for A mod B without actually directly using the % operator. I found out that % is distributive over addition:

A%B = ((a1%B)+(a2%B))%B.

But the problem still persists because I will still be doing %B operations.

techraf
  • 64,883
  • 27
  • 193
  • 198
I. Yuson
  • 1
  • 1

1 Answers1

0

You need calculate a % b without the % operator. OK? By definition the modulo operation finds the remainder after division of one number by another.

In python:

# mod = a % b
def mod(a, b): 
    return a-b*int(a/b)

>>> x = [mod(i,j) for j in range(1,100) for i in range(1,100)]
>>> y = [i % j for j in range(1,100) for i in range(1,100)]
>>> x == y

True

In C++:

#include <iostream>
#include <math.h>
using namespace std;

unsigned int mod(unsigned int a, unsigned int b) {
    return (unsigned int)(a-b*floor(a/b));
}

int main() {
    for (unsigned int i=1; i<=sizeof(unsigned int); ++i)
        for (unsigned int j=1; j<=sizeof(unsigned int); ++j)
            if (mod(i,j) != i%j)
                cout << "Somthing wrong!!";
    cout << "Proved for all unsigned int!";
    return 0;
}

Proved for all unsigned int!

Now, just extend the result to your big numbers...!!!

Jose Raul Barreras
  • 849
  • 1
  • 13
  • 19