1 /* factor.c - Factor integers
3 * Copyright 2014 Rob Landley <rob@landley.net>
5 * No standard, but it's in coreutils
7 USE_FACTOR(NEWTOY(factor, 0, TOYFLAG_USR|TOYFLAG_BIN))
13 usage: factor NUMBER...
20 static void factor(char *s)
22 unsigned long long l, ll;
28 while(isspace(*s)) s++;
29 if (*s=='-') dash = *s++;
32 l = strtoull(s, &s, 0);
33 if (*s && !isspace(*s)) {
34 error_msg("%s: not integer", err);
35 while (*s && !isspace(*s)) s++;
39 printf("-%llu:"+!dash, l);
41 // Negative numbers have -1 as a factor
42 if (dash) printf(" -1");
44 // Nothing below 4 has factors
50 // Special case factors of 2
56 // test odd numbers until square is > remainder or integer wrap.
57 for (ll=3; ;ll += 2) {
60 if (lll>l || lll<ll) {
61 if (l>1) printf(" %llu", l);
73 void factor_main(void)
78 for (ss = toys.optargs; *ss; ss++) factor(*ss);
83 if (-1 == getline(&s, &len, stdin)) break;