js/src/tests/js1_1/regress/perfect.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/
michael@0 4 */
michael@0 5
michael@0 6 // Some simple testing of new, eval and some string stuff.
michael@0 7
michael@0 8 // constructor -- expression array initialization
michael@0 9 function ExprArray(n,v)
michael@0 10 {
michael@0 11 // Initializes n values to v coerced to a string.
michael@0 12 for (var i = 0; i < n; i++) {
michael@0 13 this[i] = "" + v;
michael@0 14 }
michael@0 15 }
michael@0 16
michael@0 17
michael@0 18 // Print the perfect numbers up to n and the sum expression for n's divisors.
michael@0 19 function perfect(n)
michael@0 20 {
michael@0 21 print("The perfect numbers up to " + n + " are:");
michael@0 22 var results = [];
michael@0 23
michael@0 24 // We build sumOfDivisors[i] to hold a string expression for
michael@0 25 // the sum of the divisors of i, excluding i itself.
michael@0 26 var sumOfDivisors = new ExprArray(n+1,1);
michael@0 27 for (var divisor = 2; divisor <= n; divisor++) {
michael@0 28 for (var j = divisor + divisor; j <= n; j += divisor) {
michael@0 29 sumOfDivisors[j] += " + " + divisor;
michael@0 30 }
michael@0 31 // At this point everything up to 'divisor' has its sumOfDivisors
michael@0 32 // expression calculated, so we can determine whether it's perfect
michael@0 33 // already by evaluating.
michael@0 34 if (eval(sumOfDivisors[divisor]) == divisor) {
michael@0 35 print("" + divisor + " = " + sumOfDivisors[divisor]);
michael@0 36 results.push(divisor);
michael@0 37 }
michael@0 38 }
michael@0 39 print("That's all.");
michael@0 40 return results;
michael@0 41 }
michael@0 42
michael@0 43
michael@0 44 print("\nA number is 'perfect' if it is equal to the sum of its")
michael@0 45 print("divisors (excluding itself).\n");
michael@0 46
michael@0 47 reportCompare(perfect(500).join(), "6,28,496");

mercurial