简体   繁体   English

如何找到每个数字都在3中且所有数字相加等于数字本身的所有三位数

[英]How can I find all the three-digit numbers that each digit is in 3 and the addition of the digits is equal to the number itself

How can I find all the three-digit numbers that each digit is in 3 and the addition of the digits is equal to the number itself. 如何找到每个数字都在3中且所有数字相加的数字等于数字本身。

for example: 例如:

3 ^ 3 + 7 ^ 3 + 1 ^ 3 = 371

Update 更新资料

I have to find all the numbers from 100-999 and then, separate each digit and then perform a three-digit operation (mathematical operation) and check whether all digits are equal to the number itself 我必须找到100-999之间的所有数字,然后将每个数字分开,然后执行三位数字运算(数学运算),并检查所有数字是否等于数字本身

The number 371 fulfills the following condition: the sum of its digits in the third is equal to the number itself 数字371满足以下条件:第三位的数字之和等于数字本身

Answer 回答

You could use something like this 你可以用这样的东西

public static IEnumerable<int> GetDigits(int num)
{
   do { yield return num % 10; } while ((num /= 10) > 0);
}

public static bool SomeWeirdMathsOp(int num)
{
   return num == GetDigits(num).Sum(x => (int)Math.Pow(x, 3));
}

Usage 用法

var list = Enumerable.Range(100, 900).Where(SomeWeirdMathsOp);
foreach (var item in list)
    Console.WriteLine(item);

Output 输出量

153
370
371
407

Full demo here 完整的演示在这里


Benchmarks 基准测试

Just for fun (and because i'm annoying) i decided to benchmark everyone's answers 只是为了好玩(因为我很烦),我决定对每个人的答案进行基准测试

Results 结果

Mode            : Release
Test Framework  : .NET Framework 4.7.1
Benchmarks runs : 100 times (averaged/scale)

Scale : 900
Name      |     Time |    Range | StdDev |    Cycles | Pass
--------------------------------------------------------------
MineTimes | 0.136 ms | 0.016 ms |   0.03 |   456,359 | Yes
Mine      | 0.250 ms | 0.005 ms |   0.05 |   830,104 | Base
Mahsa     | 0.332 ms | 0.008 ms |   0.03 | 1,122,867 | Yes
JohnyL    | 2.135 ms | 0.204 ms |   0.34 | 7,262,956 | Yes

MineTimes is Enigmativity's version which just does the power manually MineTimes是Enigmativity的版本,仅手动执行功能

public static bool SomeWeirdMathsOp(int num)
{
   return num == GetDigits(num).Sum(x => x * x * x);
}

Additional Resources 其他资源

yield (C# Reference) 产量(C#参考)

When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. 在语句中使用yield关键字时,表示出现在其中的方法,运算符或get访问器是迭代器。 Using yield to define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration, 使用yield来定义迭代器,就不需要显式的额外类(该类保留枚举的状态,

% Operator (C# Reference) %运算符(C#参考)

The remainder operator (%) computes the remainder after dividing its first operand by its second. 余数运算符(%)在将其第一个操作数除以第二个操作数后计算出余数。 All numeric types have predefined remainder operators. 所有数字类型都有预定义的余数运算符。

Enumerable.Sum Method (IEnumerable, Func) Enumerable.Sum方法(IEnumerable,Func)

Computes the sum of the sequence of Double values that are obtained by invoking a transform function on each element of the input sequence. 计算通过在输入序列的每个元素上调用转换函数而获得的Double值序列的总和。

Math.Pow Method (Double, Double) Math.Pow方法(双精度,双精度)

Returns a specified number raised to the specified power. 返回提高到指定幂的指定数字。

Enumerable.Range Method (Int32, Int32) Enumerable.Range方法(Int32,Int32)

Generates a sequence of integral numbers within a specified range. 生成指定范围内的整数序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM