Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #JavaScript output
  2.  
  3. var print, sequence;
  4. var __slice = Array.prototype.slice;
  5. sequence = function(n, m) {
  6. var a, f, max, p, r, s, sum, total;
  7. if (m == null) {
  8. m = 0;
  9. }
  10. f = function(p, multiplier) {
  11. return p * p + multiplier;
  12. };
  13. s = "";
  14. max = n * n;
  15. sum = 0;
  16. for (p = 1; 1 <= n ? p <= n : p >= n; 1 <= n ? p++ : p--) {
  17. sum += f(p, m);
  18. }
  19. total = 0;
  20. for (p = 1; 1 <= n ? p <= n : p >= n; 1 <= n ? p++ : p--) {
  21. r = n - p + 1;
  22. a = f(r, m) / sum;
  23. total += a;
  24. if (s) {
  25. s += ", ";
  26. }
  27. s += a.toFixed(4);
  28. }
  29. return "" + s + "\nfor n: " + n + ", m: " + m + ", total: " + total + "\n";
  30. };
  31. print = function() {
  32. var rest;
  33. rest = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  34. return console.log.apply(console, rest);
  35. };
  36. print(sequence(3));
  37. print(sequence(5, -0.5));
  38. print(sequence(5, 0));
  39. print(sequence(5, 1.5));
  40. print(sequence(5, 2));
Add Comment
Please, Sign In to add comment