changeset 1411:dd336488a69b draft

factor: catch integer overflow. Now factor 9223372036854775783 (largest positive 64 bit signed prime) takes a couple minutes but gives the right answer.
author Rob Landley <rob@landley.net>
date Fri, 01 Aug 2014 18:50:46 -0500
parents ee0109c35b34
children b513c9c64c71
files toys/other/factor.c
diffstat 1 files changed, 3 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/toys/other/factor.c	Fri Aug 01 09:08:00 2014 -0500
+++ b/toys/other/factor.c	Fri Aug 01 18:50:46 2014 -0500
@@ -49,7 +49,9 @@
 
   // test odd numbers.
   for (ll=3; ;ll += 2) {
-    if (ll*ll>l) {
+    long lll = ll*ll;
+
+    if (lll>l || lll<ll) {
       if (l>1) printf(" %ld", l);
       break;
     }