Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #JavaScript output
- var print, sequence;
- var __slice = Array.prototype.slice;
- sequence = function(n, m) {
- var a, f, max, p, r, s, sum, total;
- if (m == null) {
- m = 0;
- }
- f = function(p, multiplier) {
- return p * p + multiplier;
- };
- s = "";
- max = n * n;
- sum = 0;
- for (p = 1; 1 <= n ? p <= n : p >= n; 1 <= n ? p++ : p--) {
- sum += f(p, m);
- }
- total = 0;
- for (p = 1; 1 <= n ? p <= n : p >= n; 1 <= n ? p++ : p--) {
- r = n - p + 1;
- a = f(r, m) / sum;
- total += a;
- if (s) {
- s += ", ";
- }
- s += a.toFixed(4);
- }
- return "" + s + "\nfor n: " + n + ", m: " + m + ", total: " + total + "\n";
- };
- print = function() {
- var rest;
- rest = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- return console.log.apply(console, rest);
- };
- print(sequence(3));
- print(sequence(5, -0.5));
- print(sequence(5, 0));
- print(sequence(5, 1.5));
- print(sequence(5, 2));
Add Comment
Please, Sign In to add comment