2017-04-10 00:48:43 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
//config:config FACTOR
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "factor (2.7 kb)"
|
2017-04-10 00:48:43 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: factor factorizes integers
|
2017-04-10 00:48:43 +05:30
|
|
|
|
|
|
|
//applet:IF_FACTOR(APPLET(factor, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_FACTOR) += factor.o
|
|
|
|
|
|
|
|
//usage:#define factor_trivial_usage
|
2017-04-14 17:53:49 +05:30
|
|
|
//usage: "[NUMBER]..."
|
2017-04-10 00:48:43 +05:30
|
|
|
//usage:#define factor_full_usage "\n\n"
|
|
|
|
//usage: "Print prime factors"
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2017-04-10 02:24:57 +05:30
|
|
|
#if 0
|
|
|
|
# define dbg(...) bb_error_msg(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define dbg(...) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef unsigned long long wide_t;
|
|
|
|
|
2017-04-10 00:48:43 +05:30
|
|
|
#if ULLONG_MAX == (UINT_MAX * UINT_MAX + 2 * UINT_MAX)
|
|
|
|
/* "unsigned" is half as wide as ullong */
|
|
|
|
typedef unsigned half_t;
|
2017-04-10 02:24:57 +05:30
|
|
|
#define HALF_MAX UINT_MAX
|
2017-04-10 00:48:43 +05:30
|
|
|
#define HALF_FMT ""
|
|
|
|
#elif ULLONG_MAX == (ULONG_MAX * ULONG_MAX + 2 * ULONG_MAX)
|
|
|
|
/* long is half as wide as ullong */
|
|
|
|
typedef unsigned long half_t;
|
2017-04-10 02:24:57 +05:30
|
|
|
#define HALF_MAX ULONG_MAX
|
2017-04-10 00:48:43 +05:30
|
|
|
#define HALF_FMT "l"
|
|
|
|
#else
|
|
|
|
#error Cant find an integer type which is half as wide as ullong
|
|
|
|
#endif
|
|
|
|
|
2017-04-11 11:04:56 +05:30
|
|
|
static half_t isqrt_odd(wide_t N)
|
2017-04-10 02:24:57 +05:30
|
|
|
{
|
|
|
|
half_t s = isqrt(N);
|
2017-04-10 15:17:48 +05:30
|
|
|
/* Subtract 1 from even s, odd s won't change: */
|
|
|
|
/* (doesnt work for zero, but we know that s != 0 here) */
|
|
|
|
s = (s - 1) | 1;
|
2017-04-10 02:24:57 +05:30
|
|
|
return s;
|
|
|
|
}
|
2017-04-10 00:48:43 +05:30
|
|
|
|
2017-04-10 02:24:57 +05:30
|
|
|
static NOINLINE void factorize(wide_t N)
|
|
|
|
{
|
|
|
|
half_t factor;
|
|
|
|
half_t max_factor;
|
2017-04-10 15:40:46 +05:30
|
|
|
// unsigned count3;
|
|
|
|
// unsigned count5;
|
|
|
|
// unsigned count7;
|
2017-04-13 14:21:13 +05:30
|
|
|
// ^^^^^^^^^^^^^^^ commented-out simple sieving code (easier to grasp).
|
2017-04-10 15:40:46 +05:30
|
|
|
// Faster sieving, using one word for potentially up to 6 counters:
|
|
|
|
// count upwards in each mask, counter "triggers" when it sets its mask to "100[0]..."
|
|
|
|
// 10987654321098765432109876543210 - bits 31-0 in 32-bit word
|
|
|
|
// 17777713333311111777775555333 - bit masks for counters for primes 3,5,7,11,13,17
|
|
|
|
// 100000100001000010001001 - value for adding 1 to each mask
|
|
|
|
// 10000010000010000100001000100 - value for checking that any mask reached msb
|
|
|
|
enum {
|
|
|
|
SHIFT_3 = 1 << 0,
|
|
|
|
SHIFT_5 = 1 << 3,
|
|
|
|
SHIFT_7 = 1 << 7,
|
|
|
|
INCREMENT_EACH = SHIFT_3 | SHIFT_5 | SHIFT_7,
|
|
|
|
MULTIPLE_OF_3 = 1 << 2,
|
|
|
|
MULTIPLE_OF_5 = 1 << 6,
|
|
|
|
MULTIPLE_OF_7 = 1 << 11,
|
2017-04-13 17:04:40 +05:30
|
|
|
MULTIPLE_DETECTED = MULTIPLE_OF_3 | MULTIPLE_OF_5 | MULTIPLE_OF_7,
|
2017-04-10 15:40:46 +05:30
|
|
|
};
|
|
|
|
unsigned sieve_word;
|
2017-04-10 00:48:43 +05:30
|
|
|
|
|
|
|
if (N < 4)
|
|
|
|
goto end;
|
2017-04-10 02:24:57 +05:30
|
|
|
|
2017-04-10 00:48:43 +05:30
|
|
|
while (!(N & 1)) {
|
|
|
|
printf(" 2");
|
|
|
|
N >>= 1;
|
|
|
|
}
|
|
|
|
|
2017-04-10 15:17:48 +05:30
|
|
|
/* The code needs to be optimized for the case where
|
|
|
|
* there are large prime factors. For example,
|
|
|
|
* this is not hard:
|
|
|
|
* 8262075252869367027 = 3 7 17 23 47 101 113 127 131 137 823
|
|
|
|
* (the largest factor to test is only ~sqrt(823) = 28)
|
|
|
|
* but this is:
|
|
|
|
* 18446744073709551601 = 53 348051774975651917
|
|
|
|
* the last factor requires testing up to
|
|
|
|
* 589959129 - about 100 million iterations.
|
2017-04-13 14:21:13 +05:30
|
|
|
* The slowest case (largest prime) for N < 2^64 is
|
|
|
|
* factor 18446744073709551557 (0xffffffffffffffc5).
|
2017-04-10 15:17:48 +05:30
|
|
|
*/
|
2017-04-10 02:24:57 +05:30
|
|
|
max_factor = isqrt_odd(N);
|
2017-04-10 15:40:46 +05:30
|
|
|
// count3 = 3;
|
|
|
|
// count5 = 6;
|
|
|
|
// count7 = 9;
|
|
|
|
sieve_word = 0
|
2017-04-13 17:04:40 +05:30
|
|
|
/* initial count for SHIFT_n is (n-1)/2*3: */
|
2017-04-10 15:40:46 +05:30
|
|
|
+ (MULTIPLE_OF_3 - 3 * SHIFT_3)
|
|
|
|
+ (MULTIPLE_OF_5 - 6 * SHIFT_5)
|
|
|
|
+ (MULTIPLE_OF_7 - 9 * SHIFT_7)
|
2017-04-13 17:04:40 +05:30
|
|
|
//+ (MULTIPLE_OF_11 - 15 * SHIFT_11)
|
2017-04-13 17:08:16 +05:30
|
|
|
//+ (MULTIPLE_OF_13 - 18 * SHIFT_13)
|
|
|
|
//+ (MULTIPLE_OF_17 - 24 * SHIFT_17)
|
2017-04-10 15:40:46 +05:30
|
|
|
;
|
2017-04-10 00:48:43 +05:30
|
|
|
factor = 3;
|
|
|
|
for (;;) {
|
2017-04-10 02:42:38 +05:30
|
|
|
/* The division is the most costly part of the loop.
|
|
|
|
* On 64bit CPUs, takes at best 12 cycles, often ~20.
|
|
|
|
*/
|
|
|
|
while ((N % factor) == 0) { /* not likely */
|
2017-04-10 00:48:43 +05:30
|
|
|
N = N / factor;
|
2017-04-10 13:43:14 +05:30
|
|
|
printf(" %"HALF_FMT"u", factor);
|
2017-04-10 02:24:57 +05:30
|
|
|
max_factor = isqrt_odd(N);
|
2017-04-10 00:48:43 +05:30
|
|
|
}
|
|
|
|
next_factor:
|
2017-04-10 02:24:57 +05:30
|
|
|
if (factor >= max_factor)
|
2017-04-10 00:48:43 +05:30
|
|
|
break;
|
|
|
|
factor += 2;
|
2017-04-10 04:11:11 +05:30
|
|
|
/* Rudimentary wheel sieving: skip multiples of 3, 5 and 7:
|
2017-04-10 00:48:43 +05:30
|
|
|
* Every third odd number is divisible by three and thus isn't a prime:
|
2017-04-10 02:42:38 +05:30
|
|
|
* 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47...
|
|
|
|
* ^ ^ ^ ^ ^ ^ ^ _ ^ ^ _ ^ ^ ^ ^
|
2017-04-10 02:24:57 +05:30
|
|
|
* (^ = primes, _ = would-be-primes-if-not-divisible-by-5)
|
2017-04-10 15:17:48 +05:30
|
|
|
* The numbers with space under them are excluded by sieve 3.
|
2017-04-10 00:48:43 +05:30
|
|
|
*/
|
2017-04-10 15:40:46 +05:30
|
|
|
// count7--;
|
|
|
|
// count5--;
|
|
|
|
// count3--;
|
|
|
|
// if (count3 && count5 && count7)
|
|
|
|
// continue;
|
|
|
|
sieve_word += INCREMENT_EACH;
|
2017-04-13 17:04:40 +05:30
|
|
|
if (!(sieve_word & MULTIPLE_DETECTED))
|
2017-04-10 03:54:16 +05:30
|
|
|
continue;
|
2017-04-10 15:17:48 +05:30
|
|
|
/*
|
|
|
|
* "factor" is multiple of 3 33% of the time (count3 reached 0),
|
|
|
|
* else, multiple of 5 13% of the time,
|
|
|
|
* else, multiple of 7 7.6% of the time.
|
|
|
|
* Cumulatively, with 3,5,7 sieving we are here 54.3% of the time.
|
|
|
|
*/
|
2017-04-10 15:40:46 +05:30
|
|
|
// if (count3 == 0)
|
|
|
|
// count3 = 3;
|
|
|
|
if (sieve_word & MULTIPLE_OF_3)
|
|
|
|
sieve_word -= SHIFT_3 * 3;
|
|
|
|
// if (count5 == 0)
|
|
|
|
// count5 = 5;
|
|
|
|
if (sieve_word & MULTIPLE_OF_5)
|
|
|
|
sieve_word -= SHIFT_5 * 5;
|
|
|
|
// if (count7 == 0)
|
|
|
|
// count7 = 7;
|
|
|
|
if (sieve_word & MULTIPLE_OF_7)
|
|
|
|
sieve_word -= SHIFT_7 * 7;
|
2017-04-10 03:54:16 +05:30
|
|
|
goto next_factor;
|
2017-04-10 00:48:43 +05:30
|
|
|
}
|
|
|
|
end:
|
|
|
|
if (N > 1)
|
|
|
|
printf(" %llu", N);
|
|
|
|
bb_putchar('\n');
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:53:49 +05:30
|
|
|
static void factorize_numstr(const char *numstr)
|
|
|
|
{
|
|
|
|
wide_t N;
|
|
|
|
|
|
|
|
/* Leading + is ok (coreutils compat) */
|
|
|
|
if (*numstr == '+')
|
|
|
|
numstr++;
|
|
|
|
N = bb_strtoull(numstr, NULL, 10);
|
|
|
|
if (errno)
|
|
|
|
bb_show_usage();
|
|
|
|
printf("%llu:", N);
|
|
|
|
factorize(N);
|
|
|
|
}
|
|
|
|
|
2017-04-10 00:48:43 +05:30
|
|
|
int factor_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int factor_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
//// coreutils has undocumented option ---debug (three dashes)
|
|
|
|
//getopt32(argv, "");
|
|
|
|
//argv += optind;
|
|
|
|
argv++;
|
|
|
|
|
2017-04-14 17:53:49 +05:30
|
|
|
if (!*argv) {
|
|
|
|
/* Read from stdin, several numbers per line are accepted */
|
|
|
|
for (;;) {
|
|
|
|
char *numstr, *line;
|
|
|
|
line = xmalloc_fgetline(stdin);
|
|
|
|
if (!line)
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
numstr = line;
|
|
|
|
for (;;) {
|
|
|
|
char *end;
|
|
|
|
numstr = skip_whitespace(numstr);
|
|
|
|
if (!numstr[0])
|
|
|
|
break;
|
|
|
|
end = skip_non_whitespace(numstr);
|
2017-04-17 19:27:07 +05:30
|
|
|
if (*end != '\0')
|
2017-04-14 17:53:49 +05:30
|
|
|
*end++ = '\0';
|
|
|
|
factorize_numstr(numstr);
|
|
|
|
numstr = end;
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 00:48:43 +05:30
|
|
|
|
|
|
|
do {
|
2017-04-14 17:53:49 +05:30
|
|
|
/* Leading spaces are ok (coreutils compat) */
|
|
|
|
factorize_numstr(skip_whitespace(*argv));
|
2017-04-10 00:48:43 +05:30
|
|
|
} while (*++argv);
|
|
|
|
|
2017-04-10 02:24:57 +05:30
|
|
|
return EXIT_SUCCESS;
|
2017-04-10 00:48:43 +05:30
|
|
|
}
|