[gnome-calculator] Improved factorization for numbers less than 2^64-1.
- From: Arth Patel <arthpatel src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator] Improved factorization for numbers less than 2^64-1.
- Date: Tue, 21 May 2013 19:21:45 +0000 (UTC)
commit 8664913437686a98c566935b70119fd3b835477a
Author: Garima Joshi <gjoshi0311 gmail com>
Date: Wed Apr 24 17:31:34 2013 +0530
Improved factorization for numbers less than 2^64-1.
src/number.vala | 38 ++++++++++++++++++++++++++++++++++++++
1 files changed, 38 insertions(+), 0 deletions(-)
---
diff --git a/src/number.vala b/src/number.vala
index 4f40de1..5805502 100644
--- a/src/number.vala
+++ b/src/number.vala
@@ -1646,6 +1646,21 @@ public class Number
return factors;
}
+ // if value < 2^64-1, call for factorize_uint64 function which deals in integers
+
+ uint64 num = 1;
+ num = num << 63;
+ num += (num - 1);
+ var int_max = new Number.unsigned_integer (num);
+
+ if (value.compare (int_max) <= 0)
+ {
+ var factors_int64 = factorize_uint64 (value.to_unsigned_integer ());
+ if (is_negative ())
+ factors_int64.data = factors_int64.data.invert_sign ();
+ return factors_int64;
+ }
+
var divisor = new Number.integer (2);
while (true)
{
@@ -1686,6 +1701,29 @@ public class Number
return factors;
}
+ public List<Number?> factorize_uint64 (uint64 n)
+ {
+ var factors = new List<Number?> ();
+ while (n % 2 == 0)
+ {
+ n /= 2;
+ factors.append (new Number.unsigned_integer (2));
+ }
+
+ for (uint64 divisor = 3; divisor <= n / divisor; divisor += 2)
+ {
+ while (n % divisor == 0)
+ {
+ n /= divisor;
+ factors.append (new Number.unsigned_integer (divisor));
+ }
+ }
+
+ if (n > 1)
+ factors.append (new Number.unsigned_integer (n));
+ return factors;
+ }
+
private Number copy ()
{
var z = new Number ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]