Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Narcissistic Numbers
- // 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.
- // For example:
- // If n is 153, then m (the number of digits) is 3 and:
- // 13 + 53 + 33 = 1 + 125 + 27 = 153
- // So, 153 is a narcissistic number.
- // Objective: Write a script to generate and output the first 25 narcissistic integers.
- function narcissisticNumber(range) {
- var narcissisticNumbers = [];
- for(var n = 0; n <= range; n++) {
- var m = n.toString().length;
- var nToArray = n.toString().split("");
- var sumOfdigets = 0;
- nToArray.forEach(function(diget){
- sumOfdigets+= Math.pow(diget, m);
- });
- if(sumOfdigets == n) {
- narcissisticNumbers.push(sumOfdigets);
- }
- }
- return narcissisticNumbers;
- }
- narcissisticNumber(25);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement