You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
854 B
C
36 lines
854 B
C
#include "genlib.h"
|
|
|
|
/*determines the greatest prime factor of an integer*/
|
|
int maxfactor(int n)
|
|
{
|
|
int test_fact(int* pnum, int fact, int* pmaxfac);
|
|
int lnum, fact;
|
|
int maxfac;
|
|
|
|
maxfac = 1;
|
|
lnum = n;
|
|
|
|
if (lnum != 0 && lnum != 1) {
|
|
fact = 2;
|
|
if (test_fact(&lnum, fact, &maxfac)) {
|
|
fact = 3;
|
|
if (test_fact(&lnum, fact, &maxfac)) {
|
|
fact = 5;
|
|
for (;;) {
|
|
if (!test_fact(&lnum, fact, &maxfac))
|
|
break;
|
|
fact += 2;
|
|
if (!test_fact(&lnum, fact, &maxfac))
|
|
break;
|
|
fact += 4;
|
|
}
|
|
}
|
|
}
|
|
if (lnum != 1) {
|
|
if (lnum > maxfac)
|
|
maxfac = lnum;
|
|
}
|
|
}
|
|
return (maxfac);
|
|
}
|