Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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"); |