Daffodil number, peach three plum four olive seven

Number of daffodils:

The daffodil number refers to a 3-digit number, and the sum of the 3 powers of the numbers on each of its digits is equal to itself;

For example 153 is the "Daffodil Number" because: 153 = 13 + 53 + 33.

In number theory, Narcissistic number, also known as narcissistic number, self-power number, Armstrong number or Armstrong number, refers to an N-digit number, the Nth power sum of each number equal to that number.

Let's take a closer look at daffodil numbers with the following two examples:

The method of finding the number of daffodils is transformed into the method of finding the digits of units, tens and hundreds.

  • Single digit method: i%10
  • How to find the tens digit: (i/10)%10
  • How to find the hundreds digit: i/100

Find the number of daffodils within 1000

//第一种写法
for(var i = 1;i<10;i++){
            for(var j=0;j<=9;j++){
                for(var k=0;k<=9;k++){
                    if( i*i*i + j*j*j + k*k*k === i*100 +j*10 +k){
                        console.log(i,j,k)
                    }
                }
            }
        }


//第二种写法
for( var j = 100;j<1000;j++){
        var k = parseInt(j/100);
        var l = parseInt((j/10)%10);
        var m = parseInt(j%10);
        if(j == k*k*k + l*l*l + m*m*m){
            console.log(j);
        }
    }

operation result:

 peach three plum four olive seven

One peach is 3 yuan, one plum is 4 yuan, and 7 olives are 1 yuan. If one hundred yuan can be bought for one hundred yuan, how many peaches, plums, and olives are there for each?

Peaches cost three pennies each, plums cost four pennies, olives seven cost one penny, three kinds of fruit cost one hundred pennies for one hundred

The following code:

 for (var i = 1; ; i++) {
            for (var j = 1; ; j++) {
                for (var k = 1; ; k++) {
                    if (i * 3 + j * 4 + k / 7 === 100 && i + j + k === 100) {
                        console.log("桃子" + i + "个", "李子" + j + "个", "橄榄" + k + "个")
                    }

                    if (i * 3 + j * 4 + k / 7 > 100) {
                        break;
                    }
                }

                if (i * 3 + j * 4 > 100) {
                    break;
                }
            }

            if (i * 3 > 100) {
                break
            }
        }

operation result:

 You can use your mind in your life.

Guess you like

Origin blog.csdn.net/Z_CH8648/article/details/127624878