Factors of an integer: Difference between revisions
Content added Content deleted
Line 272: | Line 272: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<lang c>#include < |
<lang c>#include <stdlib.h> |
||
#include < |
#include <stdio.h> |
||
typedef struct |
typedef struct |
||
{ |
|||
int *list; |
|||
short count; |
|||
} Factors; |
} Factors; |
||
void xferFactors( |
void xferFactors(Factors *fctrs, int *flist, int flix) |
||
{ |
{ |
||
int ix, ij; |
|||
int newSize = fctrs->count + flix; |
|||
⚫ | |||
if (newSize > flix) { |
|||
for (ij = 0, ix = fctrs->count; ix < newSize; ij++, ix++) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
else { |
|||
⚫ | |||
} |
|||
for (ij=0,ix=fctrs->count; ix<newSize; ij++,ix++) { |
|||
⚫ | |||
} |
|||
⚫ | |||
} |
} |
||
Factors *factor( |
Factors *factor(int num, Factors *fctrs) |
||
{ |
{ |
||
int flist[301], flix; |
|||
int dvsr; |
|||
flix = 0; |
|||
fctrs->count = 0; |
|||
if (fctrs->list) |
|||
free(fctrs->list); |
|||
fctrs->list = NULL; |
|||
for (dvsr=1; dvsr*dvsr < num; dvsr++) { |
|||
for (dvsr = 1; dvsr * dvsr < num; dvsr++) |
|||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
continue; |
|||
flix = 0; |
|||
if (flix == 300) |
|||
{ |
|||
⚫ | |||
⚫ | |||
flist[flix++] = num/dvsr; |
|||
flix = 0; |
|||
⚫ | |||
⚫ | |||
flist[flix++] = dvsr; |
|||
flist[flix++] = num/dvsr; |
|||
} |
|||
⚫ | |||
if (dvsr*dvsr == num) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
int main(int argc, char*argv[]) |
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 ); |
|||
factor(nums2factor[i], &ftors); |
|||
(void) printf("\nfactors of %d are:\n ", nums2factor[i]); |
|||
sep = ' '; |
|||
for (j=0; j<ftors.count; j++) { |
|||
for (j = 0; j < ftors.count; j++) |
|||
{ |
|||
sep = ','; |
|||
(void) printf("%c %d", sep, ftors.list[j]); |
|||
} |
|||
sep = ','; |
|||
} |
|||
(void) printf("\n"); |
|||
return 0; |
|||
} |
|||
return EXIT_SUCCESS; |
|||
}</lang> |
}</lang> |
||