#include <stdio.h>
typedef struct {
int k;
int a;
int n;
} Congruencia;
// Função para calcular o máximo divisor comum (MDC) usando o algoritmo de Euclides
int mdc(int x, int y) {
while (x != 0) {
int temp = x;
x = y % x;
y = temp;
}
return y;
}
int mdc_extendido(int a, int b, int *x, int *y) {
if (b == 0) {
*x = 1;
*y = 0;
return a;
}
int x1, y1;
int mdc = mdc_extendido(b, a % b, &x1, &y1);
*x = y1;
*y = x1 - (a / b) * y1;
return mdc;
}
int inverso_modular(int a, int m) {
int x, y;
int mdc = mdc_extendido(a, m, &x, &y);
if (mdc != 1) {
return -1;
}
return (x % m + m) % m;
}
int main(void) {
int q_equa;
Congruencia equacoes[4];
printf("Você deseja resolver quantas equações? (Máximo de 4)\n");
scanf("%i", &q_equa);
if (q_equa < 2 || q_equa > 4) {
printf("Número inválido de equações. Escolha entre 2 e 4.\n");
return 1;
}
printf("Formato: kx ≡ a mod n\n");
for (int i = 0; i < q_equa; i++) {
printf("Digite os valores da equação %d:\n", i + 1);
printf("Coeficiente de x (k%d): ", i + 1);
scanf("%d", &equacoes[i].k);
printf("Resto (a%d): ", i + 1);
scanf("%d", &equacoes[i].a);
printf("Módulo (n%d): ", i + 1);
scanf("%d", &equacoes[i].n);
if (equacoes[i].k != 1) {
int inv = inverso_modular(equacoes[i].k, equacoes[i].n);
if (inv == -1) {
printf("Não foi possível calcular o inverso modular para k = %d e n = %d.\n", equacoes[i].k, equacoes[i].n);
return 1;
}
equacoes[i].k = 1;
equacoes[i].a = (equacoes[i].a * inv) % equacoes[i].n;
}
}
for (int i = 0; i < q_equa; i++) {
for (int j = i + 1; j < q_equa; j++) {
if (mdc(equacoes[i].n, equacoes[j].n) != 1) {
printf("Os módulos %d e %d não são coprimos. O sistema pode não ter solução única.\n", equacoes[i].n, equacoes[j].n);
return 1;
}
}
}
printf("\nEquações finais:\n");
for (int i = 0; i < q_equa; i++) {
printf("x ≡ %d mod %d\n", equacoes[i].a, equacoes[i].n);
}
long long N = 1;
for (int i = 0; i < q_equa; i++) {
N *= equacoes[i].n;
}
printf("\nProduto dos módulos (N): %lld\n", N);
long long N_values[4];
int x_values[4];
for (int i = 0; i < q_equa; i++) {
N_values[i] = N / equacoes[i].n;
x_values[i] = inverso_modular(N_values[i], equacoes[i].n);
if (x_values[i] == -1) {
printf("Não foi possível calcular o inverso modular para N%d = %lld mod %d.\n", i + 1, N_values[i], equacoes[i].n);
return 1;
}
printf("N%d : %lld, x%d : %d\n", i + 1, N_values[i], i + 1, x_values[i]);
}
long long X = 0;
for (int i = 0; i < q_equa; i++) {
X += equacoes[i].a * x_values[i] * N_values[i];
}
X %= N;
printf("Solução geral: x = %lld + %lldt\n", X, N);
return 0;
} Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}