JS algorithm set (i)
★
Recently some friends and I said to JS in some of the algorithm is very confused, know this algorithm is what is going on, but it is not in code to write it, here I share with you the idea of the algorithm to do Narcissus number, and its expansion to the self-power number of the algorithm, I hope to be helpful to everyone.
1, verify whether a number is Narcissus number① to write the algorithm of Narcissus number, we first to understand what is the number of daffodils, narcissus number refers to a 3-bit positive integer, its number on each bit of the sum of 3 power equals itself. (Example: 1^3 + 5^3+ 3^3 = 153); ② know what Narcissus numbers are and we begin to analyze how to do it. By definition, we first have to take each number out, and then by verifying that the equation is set to find the number of daffodils, ③ so how to get a three-digit number of each, very simple, or take 153来 do example, 153%10 take the remainder for 1 will be taken out, and then 153%100 to take the remainder, Then divided by 10 rounding, get 10 bits, 153/100 rounding to obtain the hundred number; ④ got three digits how to judge, through if judgment statement to Judge 1^3 + 5^3+ 3^3 = 153 Whether set up, set up print out. Let's start by writing a code to determine the number of daffodils:
1 while(true){2 varNum=number (Prompt (' Please enter a three-digit number ')));3 if(num<1000&num>=100){4 vara=num%10;//Digit number5 varB=parseint (NUM%100/10); 10-digit number6 varC=parseint (num/100); Number of Hundred7}Else {8Alert (' Input error; ');9 Continue;Ten } One if(num==a*a*a+b*b*b+c*c*c) { AAlert (num+ "is Narcissus who"); - Break; -}Else { theAlert (num+ "is not Narcissus who"); - Break; - } -}
2, print out all the number of daffodils
① we already know how to determine whether a number is the number of daffodils, then how to print all the number of daffodils, the first thought must be the cycle.
② since the number of daffodils is a three-digit number, then we just have to take all the three digits to judge on it, say not much, look at the code:
1document.write (' Narcissus number: ')2 for(varnum=100;num<1000;num++){3 vara=num%10;//Digit number4 varB=parseint (NUM%100/10); 10-digit number5 varC=parseint (num/100); Number of Hundred6 if(num==a*a*a+b*b*b+c*c*c) {7document.write (num+ ', ')8 }9}
3, self-power number of the algorithm, we will expand the number of daffodils, to do a self-power number algorithm;
① first understand what is the self-power number, the self-power number refers to an n-bit positive integer (n≥3), and its number on each bit of the sum of the n power equals to itself. (Example: 1^3 + 5^3+ 3^3 = 153);
② Narcissus number is only a part of self-power number, from the number of daffodils expand, to determine whether a number is a self-power number, first of all to judge it is a few
③ then take the number of each bit, to judge, take 1634 as an example, see the code Comment:
1 while(true) {2 varnum = number (prompt (' Please enter a value greater than 100 '));3 if(Num >= && parsefloat (num) = =parseint (num)) {4 vari = 100;5 varCount = 0, sum = 0, A;6 /*determine num is a number of digits*/7 while(true) {8 if(num/i >= 1) {9I *= 10;Tencount++;//count = 1; when 3 digits, 2 o'clock is four digits; 1634 for example: count=2 One}Else { AI/= 10; When you enter here, divide the num<i by 10 and NUM, and take 1634 for example: i=1000 - Break; - } the }
/ * take each bit and calculate the sum of the powers of each digit several times * / - while(I >= 1) { - varb = 1; -A = parseint (num% (i *)/i);//Take every digit of NUM, take the thousand to 1634/1000 rounding; hundred: 1634%1000/100 10 bits: 1634%100/10 take the whole bit 1634%10;
The hundred and 10-bit laws are 1634% (i*10)/I take the whole, while the thousands and units of the test, 1634%10000=1634;1634%10/1; still set up + for(varj = 1; J <= Count + 2; J + +) { -b *= A;//depending on the number of digits, determine the power of each digit several times. + } Asum + = b;//sum the power of each digit several times; ati/=10; Update loop variable remove one - }
/ * Determine if it is a self-power number * / - if(num = =sum) { -Alert (num + "is a self-power number"); - Break; -}Else { inAlert (num + "is not a self-power number"); - Break; to } +}Else { -Alert (' Input error; '); the Continue; * } $}
this time Here's the share .
Thank you for watching.
feel good Please praise
I hope we can inspire you .
There are better ways or different opinions please contact me in the message area .
JS algorithm Set (i) Narcissus number and expansion (judging from power number)