Home >Web Front-end >JS Tutorial >How to implement narcissus number in javascript

How to implement narcissus number in javascript

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-04-29 09:23:533262browse

This article will introduce to you how to implement the daffodil number in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to implement narcissus number in javascript

Narcissistic number is also known as pluperfect digital invariant (PPDI), narcissistic number, self-exponentiation number, Arms Strong number or Armstrong number (Armstrong number), the narcissus number refers to a 3-digit number, the sum of the third power of the digits in each digit is equal to itself (for example: 1^3 5^3 3^3 = 153).

The following code uses javascript to implement the daffodil number:

for(var i=100;i<1000;i++)
			{
				var a=parseInt(i/100);
				var b=parseInt((i-a*100)/10);
				var c=parseInt(i%10);
				// document.write(b);
				if(i==a*a*a+b*b*b+c*c*c)
				{
					document.write(i+"<br/>");
				}
			}

Among them: the method of obtaining the middle digit number can be obtained in a variety of ways: each Individual methods may be different: For example,

for(var i=100;i<1000;i++)
			{
				var a=parseInt(i/100);
				// var b=parseInt((i-a*100)/10);
				var b=parseInt((i/10)%10);
				var c=parseInt(i%10);
				// document.write(b);
				if(i==a*a*a+b*b*b+c*c*c)
				{
					document.write(i+"<br/>");
				}
			}

for narcissus numbers, they are actually a kind of auto-exponentiation number. These numbers all satisfy (n digits, and the sum of the nth power of each digit is equal to its itself);

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to implement narcissus number in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete