Blum integer: Difference between revisions
Content added Content deleted
(→{{header|Go}}: Oops!) |
(→{{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) { |
||
⚫ | |||
int count = 0; |
int count = 0; |
||
if (n == 1) goto end; |
|||
while (!(n%2)) { |
|||
⚫ | |||
n /= 2; |
|||
⚫ | |||
while (!(n%3)) { |
while (!(n%3)) { |
||
factors[count++] = 3; |
factors[count++] = 3; |
||
⚫ | |||
n /= 3; |
n /= 3; |
||
} |
} |
||
while (!(n%5)) { |
while (!(n%5)) { |
||
factors[count++] = 5; |
factors[count++] = 5; |
||
⚫ | |||
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; |
||
} |
} |
||
⚫ | |||
*length = count; |
*length = count; |
||
} |
} |