Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. // Narcissistic Numbers
  2.  
  3. // A narcissistic number is a non-negative integer (n) with m digits where the sum of all the individual digits raised to the power m is equal to n.
  4.  
  5. // For example:
  6.  
  7. // If n is 153, then m (the number of digits) is 3 and:
  8.  
  9. // 13 + 53 + 33 = 1 + 125 + 27 = 153
  10.  
  11. // So, 153 is a narcissistic number.
  12.  
  13. // Objective: Write a script to generate and output the first 25 narcissistic integers.
  14.  
  15. function narcissisticNumber(range) {
  16. var narcissisticNumbers = [];
  17.  
  18. for(var n = 0; n <= range; n++) {
  19. var m = n.toString().length;
  20. var nToArray = n.toString().split("");
  21. var sumOfdigets = 0;
  22. nToArray.forEach(function(diget){
  23. sumOfdigets+= Math.pow(diget, m);
  24. });
  25.  
  26. if(sumOfdigets == n) {
  27. narcissisticNumbers.push(sumOfdigets);
  28. }
  29. }
  30. return narcissisticNumbers;
  31. }
  32. narcissisticNumber(25);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement