Skip to content

Instantly share code, notes, and snippets.

@Lytol
Created November 7, 2010 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lytol/667090 to your computer and use it in GitHub Desktop.
Save Lytol/667090 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main() {
int firstInt, secondInt, firstIntFactors[20], secondIntFactors[20];
int firstIntMaxIndex = -1, secondIntMaxIndex = -1;
int commonFactors[20], gcd, factor, i, j;
printf("Two integers: ");
scanf("%d %d", &int_1, &int_2);
printf("%d:", int_1);
/* Factorize int_1 into an array then repeat for int_2 */
factor = 2;
while (firstInt > 1) {
if ((firstInt % factor) == 0) {
firstIntMaxIndex++;
firstIntFactors[firstIntMaxIndex] = factor;
printf(" %d", factor);
firstInt /= factor;
} else {
factor++;
}
}
printf("\n%d:", int_2);
/* Reset factor for second factorization */
factor = 2;
while (secondInt > 1) {
if ((secondInt % factor) == 0) {
secondIntMaxIndex++;
secondIntFactors[secondIntMaxIndex] = factor;
printf(" %d", factor);
secondInt /= factor;
} else {
factor++;
}
}
// printf("\ngcd:");
//
// Loop through both factor arrays and fill commonFactors. As common factors are found,
// make sure to multiply the gcd by each factor.
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment