Skip to content

Instantly share code, notes, and snippets.

@johnboker
Created June 9, 2015 17:49
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 johnboker/bedcf9b7a66ad8425694 to your computer and use it in GitHub Desktop.
Save johnboker/bedcf9b7a66ad8425694 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
void print_factors(long n);
int main(int argc, char **argv)
{
long n;
while (1)
{
scanf("%ld", &n);
if (n==0)
{
break;
}
printf("%ld = ", n);
if (n<0)
{
printf("-1 x ");
n = -n;
}
print_factors(n);
printf("\n");
}
return 0;
}
void print_factors(long n)
{
int first = 1;
while (n%2==0)
{
if (first)
{
printf("2");
first = 0;
}
else
{
printf(" x 2");
}
n /= 2;
}
long i = 3;
long sqn = (sqrt(n))+1;
while (i <= sqn)
{
if ((n%i)==0)
{
if (first)
{
printf("%ld", i);
first = 0;
}
else
{
printf(" x %ld", i);
}
n /= i;
}
else
{
i += 2;
}
}
if (n>1)
{
if(first)
{
printf("%ld", n);
}
else
{
printf(" x %ld", n);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment