Factors of an integer: Difference between revisions

Line 272:
 
=={{header|C}}==
<lang c>#include <stdiostdlib.h>
#include <stdlibstdio.h>
 
typedef struct {
{
int *list;
short count;
} Factors;
 
void xferFactors( Factors *fctrs, int *flist, int flix )
{
int ix, ij;
int newSize = fctrs->count + flix;
fctrs->list = (newSize > flix) ? realloc(fctrs->list, =newSize malloc* sizeof(int)) : malloc(newSize * sizeof(int));
if (newSize > flix) {
for (ij = 0, ix = fctrs->listcount; =ix realloc( fctrs->list,< newSize; *ij++, sizeof(intix++));
fctrs->list[ix] = flist[ij];
}
fctrs->count = newSize;
else {
fctrs->list = malloc( newSize * sizeof(int));
}
for (ij=0,ix=fctrs->count; ix<newSize; ij++,ix++) {
fctrs->list[ix] = flist[ij];
}
fctrs->count = newSize;
}
 
Factors *factor( int num, Factors *fctrs)
{
int flist[301], flix;
int dvsr;
flix = 0;
fctrs->count = 0;
if (fctrs->list) free(fctrs->list);
free(fctrs->list = NULL);
fctrs->list = NULL;
for (dvsr=1; dvsr*dvsr < num; dvsr++) {
for (dvsr = 1; dvsr * dvsr < if (num %; dvsr != 0++) continue;
{
if ( flix == 300) {
if (num if% (dvsr*dvsr =!= num0)
xferFactors( fctrs, flist, flix );
continue;
flix = 0;
if (flix == }300)
{
flist[flix++] = dvsr;
xferFactors( fctrs, flist, flix );
flist[flix++] = num/dvsr;
flix }= 0;
}
if (dvsr*dvsr == num)
flist[flix++] = dvsr;
if (flist[flix++] >= 0)num/dvsr;
}
xferFactors( fctrs, flist, flix );
if (dvsr*dvsr == num)
 
flist[flix++] = dvsr;
return fctrs;
if ( flix ==> 3000) {
xferFactors( fctrs, flist, flix );
return fctrs;
}
int main(int argc, char *argv[])
{
int nums2factor[] = { 2059, 223092870, 3135, 45 };
Factors ftors = { NULL, 0};
char sep;
int i, j;
 
for (i = 0; i < 4; i++) {
{
factor( nums2factor[i], &ftors );
printffactor("\nfactors of %d are:\n ", nums2factor[i], &ftors);
(void) printf("\nfactors of %d are:\n ", nums2factor[i]);
sep = ' ';
for (j=0; j<ftors.count; j++) {
for (j = 0; j printf("%c %d", sep,< ftors.list[count; j]++);
{
sep = ',';
(void) printf("%c %d", sep, ftors.list[j]);
}
sep = printf("\n")',';
}
(void) printf("\n");
return 0;
}
return EXIT_SUCCESS;
}</lang>
 
Anonymous user