aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-03-13 17:23:11 -0500
committerRob Landley <rob@landley.net>2016-03-13 17:23:11 -0500
commit51d71f5c5b7040b50a38d9360561cee626eb2c2e (patch)
tree03b3de33424239d927ac07bb1faa4b1892aea1d2 /toys
parent6d364fce2aebabc3fb4da2955ac499162699841b (diff)
downloadtoybox-51d71f5c5b7040b50a38d9360561cee626eb2c2e.tar.gz
factor: use long long math (64 bit on 32 bit platforms) and handle negative
numbers even though we use unsigned math now.
Diffstat (limited to 'toys')
-rw-r--r--toys/other/factor.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/toys/other/factor.c b/toys/other/factor.c
index 8aa1d935..0e07d714 100644
--- a/toys/other/factor.c
+++ b/toys/other/factor.c
@@ -19,32 +19,31 @@ config FACTOR
static void factor(char *s)
{
- unsigned long l, ll;
+ unsigned long long l, ll;
for (;;) {
char *err = s;
+ int dash = 0;
while(isspace(*s)) s++;
+ if (*s=='-') dash = *s++;
if (!*s) return;
- l = strtoul(s, &s, 0);
+ l = strtoull(s, &s, 0);
if (*s && !isspace(*s)) {
error_msg("%s: not integer", err);
-
- return;
+ while (*s && !isspace(*s)) s++;
+ continue;
}
- printf("%lu:", l);
+ printf("-%llu:"+!dash, l);
// Negative numbers have -1 as a factor
- if (l < 0) {
- printf(" -1");
- l *= -1;
- }
+ if (dash) printf(" -1");
// Nothing below 4 has factors
if (l < 4) {
- printf(" %lu\n", l);
+ printf(" %llu\n", l);
continue;
}
@@ -54,16 +53,16 @@ static void factor(char *s)
l >>= 1;
}
- // test odd numbers.
+ // test odd numbers until square is > remainder or integer wrap.
for (ll=3; ;ll += 2) {
long lll = ll*ll;
if (lll>l || lll<ll) {
- if (l>1) printf(" %lu", l);
+ if (l>1) printf(" %llu", l);
break;
}
while (!(l%ll)) {
- printf(" %lu", ll);
+ printf(" %llu", ll);
l /= ll;
}
}