有人可以解释 Dmitry 是如何计算 NMin 的吗?
Can someone explain how Dmitry got the NMin math down?
原问题位于Project Euler Largest palindrome product下方
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.
上下文中的问题位于
Dmitry Answering Largest palindrome product - C#. I got the correct answer but did it from min to max instead of max to min so I looked for a more efficient answer to study. I understand what all the code does, but I can't figure out where Dmitry开始求公式从最大被乘数常数求最小被乘数。我正在浏览几个编码挑战网站,为一些技术面试做准备。
这一行:
const int NMin = NMax - (NMax + 1) / 10 + 1;
OF
// Store the maximum palindrome number here:
long maxNumber = 0;
// The maximum multiplicand (typically: 9...9):
const int NMax = 999;
// The minimum multiplicand.
// Obviously, it couldn't be less than 90...0:
const int NMin = NMax - (NMax + 1) / 10 + 1;
for (int i = NMax; i > NMin; i--)
{
// Starting from i since i * j = j * i for any i, j:
for (int j = i; j > NMin; j--)
{
long number = Math.BigMul(i, j);
// The fastest condition should be the first in the `if` statement:
if (number > maxNumber && isPalindome(number))
{
maxNumber = number;
Console.WriteLine("{0} = {1} * {2}", number, i, j);
break; // Leave the `j` loop, because it's guaranteed that there is
// no numbers greater than `number` for the current `i`
}
}
}
我浏览过的网站包括:
- Advent Of Code
- HackerEarth and HackerRank
- Leetcode I've been attempting to finish the Comprehensive Study Plans
- Project Euler 现在只有问题 #4
如 problem 4 所述,two-digit 个数字乘积的最大回文是 91*99
。我相信 Dmitry 认识到对于给定的数字范围(他计算的 3、4 或 5 但实际上是无穷大)的所有最大回文必须是 9x -> 9y
(其中 x
代表 0,y
代表9)。 x
和 y
所需的数量是 digit - 1
如果你总是想要 最高回文。 这里较低的 90% 根本不值得检查回文,因为它们不会产生最高的乘法。
因此他每次都可以根据他提供的等式计算出最小值:
NMin = NMax - (NMax + 1) / 10 + 1 // NMin = 900 if NMax = 999
在 4 位回文的情况下,这会产生 9,000 -> 9,999
或 90,000 -> 99,999
5.
这里要注意的重要一点是他可以 hard-coded NMin
或者选择一个更大的最小值。
原问题位于Project Euler Largest palindrome product下方
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.
上下文中的问题位于 Dmitry Answering Largest palindrome product - C#. I got the correct answer but did it from min to max instead of max to min so I looked for a more efficient answer to study. I understand what all the code does, but I can't figure out where Dmitry开始求公式从最大被乘数常数求最小被乘数。我正在浏览几个编码挑战网站,为一些技术面试做准备。
这一行:
const int NMin = NMax - (NMax + 1) / 10 + 1;
OF
// Store the maximum palindrome number here:
long maxNumber = 0;
// The maximum multiplicand (typically: 9...9):
const int NMax = 999;
// The minimum multiplicand.
// Obviously, it couldn't be less than 90...0:
const int NMin = NMax - (NMax + 1) / 10 + 1;
for (int i = NMax; i > NMin; i--)
{
// Starting from i since i * j = j * i for any i, j:
for (int j = i; j > NMin; j--)
{
long number = Math.BigMul(i, j);
// The fastest condition should be the first in the `if` statement:
if (number > maxNumber && isPalindome(number))
{
maxNumber = number;
Console.WriteLine("{0} = {1} * {2}", number, i, j);
break; // Leave the `j` loop, because it's guaranteed that there is
// no numbers greater than `number` for the current `i`
}
}
}
我浏览过的网站包括:
- Advent Of Code
- HackerEarth and HackerRank
- Leetcode I've been attempting to finish the Comprehensive Study Plans
- Project Euler 现在只有问题 #4
如 problem 4 所述,two-digit 个数字乘积的最大回文是 91*99
。我相信 Dmitry 认识到对于给定的数字范围(他计算的 3、4 或 5 但实际上是无穷大)的所有最大回文必须是 9x -> 9y
(其中 x
代表 0,y
代表9)。 x
和 y
所需的数量是 digit - 1
如果你总是想要 最高回文。 这里较低的 90% 根本不值得检查回文,因为它们不会产生最高的乘法。
因此他每次都可以根据他提供的等式计算出最小值:
NMin = NMax - (NMax + 1) / 10 + 1 // NMin = 900 if NMax = 999
在 4 位回文的情况下,这会产生 9,000 -> 9,999
或 90,000 -> 99,999
5.
这里要注意的重要一点是他可以 hard-coded NMin
或者选择一个更大的最小值。