Jump to content

Factors of an integer: Difference between revisions

Undo revision 101016 by 81.255.154.129 (previous version worked fine; no need to make it more complex)
(Undo revision 101016 by 81.255.154.129 (previous version worked fine; no need to make it more complex))
Line 272:
 
=={{header|C}}==
<lang c>#include <stdlibstdio.h>
#include <stdiostdlib.h>
 
typedef struct {
int *list;
{
short count;
int *list;
short count;
} Factors;
 
void xferFactors( Factors *fctrs, int *flist, int flix )
{
int ix, ij;
int newSize = fctrs->count + flix;
if (newSize > flix) {
fctrs->list = (newSize > flix) ? realloc(fctrs->list, newSize * sizeof(int)) : malloc(newSize * sizeof(int));
for (ij = 0, ix = fctrs->count;list ix= <realloc( fctrs->list, newSize; ij++,* ix++sizeof(int));
}
fctrs->list[ix] = flist[ij];
else {
fctrs->count = newSize;
fctrs->list = (newSize > flix) ? realloc( fctrs->list, newSize= * sizeofmalloc(int)) : 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;
for (dvsr=1; dvsr*dvsr < num; dvsr++) {
fctrs->list = NULL;
for (dvsr = 1; dvsr * dvsr < if (num; % dvsr++ != 0) continue;
if ( flix >== 0300) {
{
xferFactors( fctrs, flist, flix );
if (num % dvsr != 0)
flix = 0;
continue;
if (flix == 300) }
flist[flix++] = dvsr;
{
flist[flix++] = num/dvsr;
xferFactors(fctrs, flist, flix);
flix = 0;}
if (num %if (dvsr*dvsr !== 0num)
}
flist[flix++] = dvsr;
flist[ if (flix++] => num/dvsr;0)
xferFactors( fctrs, flist, flix );
}
 
if (dvsr*dvsr == num)
return fctrs;
flist[flix++] = dvsr;
if (flix > 0)
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 );
{
factor printf("\nfactors of %d are:\n ", nums2factor[i], &ftors);
sep = ' ';
(void) printf("\nfactors of %d are:\n ", nums2factor[i]);
for (j=0; j<ftors.count; j++) {
sep = ' ';
for (j = 0; j < printf("%c %d", sep, ftors.count; list[j++]);
sep = ',';
{
}
(void) printf("%c %d", sep, ftors.list[j]);
sep = ',' printf("\n");
}
return 0;
(void) printf("\n");
}
return EXIT_SUCCESS;
}</lang>
 
1,150

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.