Blum integer: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: Specialized primeFactors routine - about 20% faster than before.)
Line 29: Line 29:
#include <locale.h>
#include <locale.h>


int inc[8] = {4, 2, 4, 2, 4, 6, 2, 6};

// Assumes n is odd and bails out when factor count exceeds 2.
void primeFactors(int n, int *factors, int *length) {
void primeFactors(int n, int *factors, int *length) {
if (n < 2) return;
int count = 0;
int count = 0;
int inc[8] = {4, 2, 4, 2, 4, 6, 2, 6};
if (n == 1) goto end;
while (!(n%2)) {
factors[count++] = 2;
n /= 2;
}
while (!(n%3)) {
while (!(n%3)) {
factors[count++] = 3;
factors[count++] = 3;
if (count > 2) goto end;
n /= 3;
n /= 3;
}
}
while (!(n%5)) {
while (!(n%5)) {
factors[count++] = 5;
factors[count++] = 5;
if (count > 2) goto end;
n /= 5;
n /= 5;
}
}
Line 48: Line 48:
if (!(n%k)) {
if (!(n%k)) {
factors[count++] = k;
factors[count++] = k;
if (count > 2) goto end;
n /= k;
n /= k;
} else {
} else {
Line 57: Line 58:
factors[count++] = n;
factors[count++] = n;
}
}
end:
*length = count;
*length = count;
}
}