两个 3 位数乘积的回文

发布于 2024-12-04 22:10:22 字数 609 浏览 3 评论 0原文

我想找到两个三位数相乘所得到的最大回文数。

我开始时 a 和 b 都是 999,并且每次发生乘法时 a 和 b 都会递减。

a = 999 #Defining Variables
b = 999

for i in range (1000): 
    c= a*b                          #multiply a to b
    if int(str(c)[::-1]) == c:
        print c
    a = a-1                         #decrement the value of a
    c=a*b                           #multiply a by the decremented a
    if int(str(c)[::-1]) == c:
        print c

    b = b-1                         #decrement b so that both a and b have been decremented

结果是 698896、289982、94249、69696...,其中 698896 是第一个数字。目前我仍在试图找出我错过了什么。

I want to find the largest palindrome that can be obtained through the multiplication of two 3-digit numbers.

I started off with a and b both being 999, and to decrement a and b with every multiplication that occurred.

a = 999 #Defining Variables
b = 999

for i in range (1000): 
    c= a*b                          #multiply a to b
    if int(str(c)[::-1]) == c:
        print c
    a = a-1                         #decrement the value of a
    c=a*b                           #multiply a by the decremented a
    if int(str(c)[::-1]) == c:
        print c

    b = b-1                         #decrement b so that both a and b have been decremented

The result has turned up 698896, 289982, 94249, 69696... with 698896 being the first number. Currently I'm still trying to figure out what I'm missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

单挑你×的.吻 2024-12-11 22:10:23

您不能以交替的方式递减 ab,因为这样您会丢失像 a = 999 和 b = 997 这样的值对。

尝试使用嵌套循环,从 999 开始向后计数。

类似

def is_pal(c):
    return int(str(c)[::-1]) == c

maxpal = 0
for a in range(999, 99, -1):
    for b in range(a, 99, -1):
        prod = a * b
        if is_pal(prod) and prod > maxpal:
            maxpal = prod

print maxpal

编辑:保罗评论后修改下限。

You cannot decrement a and b in an alternating fashion, because you're missing value pairs like a = 999 and b = 997 that way.

Try nested looping instead, starting from 999 and counting backwards.

Something like

def is_pal(c):
    return int(str(c)[::-1]) == c

maxpal = 0
for a in range(999, 99, -1):
    for b in range(a, 99, -1):
        prod = a * b
        if is_pal(prod) and prod > maxpal:
            maxpal = prod

print maxpal

EDIT: Modified lower bounds after Paul's comment.

盛夏已如深秋| 2024-12-11 22:10:23

你的算法是错误的。您需要测试 a 的所有值到 b 的所有值,这可以通过使用两个循环(外部循环 a 和内部循环 b)来解决。我还建议您使用 a 和 b 作为循环索引,这可以简化逻辑(更容易记住)。

考虑将回文检查也移至其自己的函数中,以使代码更易于理解。


我不是 Python 程序员,但这是我的 PHP 解决方案:

function palindrome($x) {
  $x = (string) $x;  //Cast $x to string
  $len = strlen($x); //Length of $x

  //Different splitting depending on even or odd length
  if($len % 2 == 0) {
    list($pre, $suf) = str_split($x, $len/2);
  }else{
    $pre = substr($x, 0, $len/2);
    $suf = substr($x, $len/2+1);
  }

  return $pre == strrev($suf);
}

$max = array(0, 0, 0);

//Loop $a from 999 to 100, inclusive.
//Do the same over $b for EVERY $a
for($a = 999; $a >= 100; $a--) {
  for($b = 999; $b >= 100; $b--) {
    $x = $a*$b;

    if(palindrome($x)) {
      echo $a, '*', $b, ' = ', $x, "\n";
      if($x > $max[2]) {
        $max = array($a, $b, $x);
      }
    }
  }
}
echo "\nLargest result: ", $max[0], '*', $max[1], ' = ', $max[2];

You algorithm is wrong. You will need to test all values of a to all values of b, which can be solved by using two loops (the outer for a and the inner for b). I also suggest that you use a and b as loop indices, which simplifies the logic (makes it easier to keep in head).

Consider moving the palindrome check to it's own function as well, to make the code easier to understand.


I'm no Python programmer, but here's my solution in PHP:

function palindrome($x) {
  $x = (string) $x;  //Cast $x to string
  $len = strlen($x); //Length of $x

  //Different splitting depending on even or odd length
  if($len % 2 == 0) {
    list($pre, $suf) = str_split($x, $len/2);
  }else{
    $pre = substr($x, 0, $len/2);
    $suf = substr($x, $len/2+1);
  }

  return $pre == strrev($suf);
}

$max = array(0, 0, 0);

//Loop $a from 999 to 100, inclusive.
//Do the same over $b for EVERY $a
for($a = 999; $a >= 100; $a--) {
  for($b = 999; $b >= 100; $b--) {
    $x = $a*$b;

    if(palindrome($x)) {
      echo $a, '*', $b, ' = ', $x, "\n";
      if($x > $max[2]) {
        $max = array($a, $b, $x);
      }
    }
  }
}
echo "\nLargest result: ", $max[0], '*', $max[1], ' = ', $max[2];
明月松间行 2024-12-11 22:10:23

最快的方法是从最大值999x999=998001之前的最大回环处循环下来。 997799,996699,..并检查每个是否可以在100..999范围内分为A和B。我的代码花了 2200 个周期。您的代码将需要大约 4K 到 8K 周期。

Sub method3a()
iterations = 0
For a = 997 To 0 Step -1
    R = a * 1000 + Val(StrReverse(a))
    b = 999      ' R=b*s
    s = Int(R / b)
    While b >= s
        iterations = iterations + 1
        If R = b * s Then
            Debug.Print "Result=" & R & " iterations=" & iterations
            Exit Sub
        End If
        b = b - 1
        s = Int(R / b)
    Wend
Next

结束子

The fastest method is to cicle down from the biggest polindrome before maximum value 999x999=998001. 997799,996699,.. and check each if it can be divided into A and B in range 100..999. My code took 2200 cycles. Your code will take about 4K to 8K cycles.

Sub method3a()
iterations = 0
For a = 997 To 0 Step -1
    R = a * 1000 + Val(StrReverse(a))
    b = 999      ' R=b*s
    s = Int(R / b)
    While b >= s
        iterations = iterations + 1
        If R = b * s Then
            Debug.Print "Result=" & R & " iterations=" & iterations
            Exit Sub
        End If
        b = b - 1
        s = Int(R / b)
    Wend
Next

End Sub

葵雨 2024-12-11 22:10:23
i = 1000000
test = 0
while test == 0:
    i += -1
    str_i = str(i)
    if str_i[0:3] == str_i[3:][::-1]:
        for j in range(100, 1000):
            if i % j == 0:
                if i/j < 1000 and i/j > 100:
                    print('Largest Palindrome: %s = %s * %s' % (i, j, i//j))
                    test = 1
                    break
i = 1000000
test = 0
while test == 0:
    i += -1
    str_i = str(i)
    if str_i[0:3] == str_i[3:][::-1]:
        for j in range(100, 1000):
            if i % j == 0:
                if i/j < 1000 and i/j > 100:
                    print('Largest Palindrome: %s = %s * %s' % (i, j, i//j))
                    test = 1
                    break
夏有森光若流苏 2024-12-11 22:10:23

在 C# 中 - 要点中的解决方案 - https://gist.github.com/4496303

公共类工作者
{
公职人员(){

    }
    public void start()
    {
        int MAX_NUMBER = 999;
        for (int Number = MAX_NUMBER; Number >= 0; Number--)
        {
            string SNumberLeft = Number.ToString();
            string SNumberRight = Reverse(Number.ToString());
            int palindromic = Convert.ToInt32(SNumberLeft + SNumberRight);

            for (int i = MAX_NUMBER; i >= 1; i--)
            {
                for (int l = MAX_NUMBER; l >= 1; l--)
                {
                    if ((i * l) - palindromic == 0)
                    {
                        System.Diagnostics.Debug.WriteLine("Result :" + palindromic);
                        return;
                    }
                }
            }

           // System.Diagnostics.Debug.WriteLine( palindromic); 
        }           

    }

    public string Reverse(String s)
    {
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }



}

In C# - solution in gist - https://gist.github.com/4496303

public class worker
{
public worker(){

    }
    public void start()
    {
        int MAX_NUMBER = 999;
        for (int Number = MAX_NUMBER; Number >= 0; Number--)
        {
            string SNumberLeft = Number.ToString();
            string SNumberRight = Reverse(Number.ToString());
            int palindromic = Convert.ToInt32(SNumberLeft + SNumberRight);

            for (int i = MAX_NUMBER; i >= 1; i--)
            {
                for (int l = MAX_NUMBER; l >= 1; l--)
                {
                    if ((i * l) - palindromic == 0)
                    {
                        System.Diagnostics.Debug.WriteLine("Result :" + palindromic);
                        return;
                    }
                }
            }

           // System.Diagnostics.Debug.WriteLine( palindromic); 
        }           

    }

    public string Reverse(String s)
    {
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }



}
冷血 2024-12-11 22:10:23

我做到了,并优化并减少了搜索时间的步骤数

0.03525909700010743

palindrome = 906609

count = 3748

i = 913

j = 993

from timeit import timeit

def palindrome(number):
    return str(number) == str(number)[::-1] # chek number is polindrome

def largest():
    max_num = 0
    count = 0
    ccount = 0
    ii = 0
    jj = 0
    for i in range(999, 99, -1): # from largest to smallest
        for j in range(999,  i - 1, -1): # exclude implementation j * i
            mult = i * j # multiplication
            count += 1
            if mult > max_num and palindrome(mult): # chek conditions
                max_num = mult #remember largest
                ii = i
                jj = j
                ccount = count
    return "\npalindrome = {0}\ncount = {1}\ni = {2}\nj = {3}".format(max_num, ccount, ii, jj)
print ("time", timeit('largest()', 'from __main__ import largest', number = 1))
print(largest())

I did it and have optimized and reduced the number of steps for search

time 0.03525909700010743

palindrome = 906609

count = 3748

i = 913

j = 993

from timeit import timeit

def palindrome(number):
    return str(number) == str(number)[::-1] # chek number is polindrome

def largest():
    max_num = 0
    count = 0
    ccount = 0
    ii = 0
    jj = 0
    for i in range(999, 99, -1): # from largest to smallest
        for j in range(999,  i - 1, -1): # exclude implementation j * i
            mult = i * j # multiplication
            count += 1
            if mult > max_num and palindrome(mult): # chek conditions
                max_num = mult #remember largest
                ii = i
                jj = j
                ccount = count
    return "\npalindrome = {0}\ncount = {1}\ni = {2}\nj = {3}".format(max_num, ccount, ii, jj)
print ("time", timeit('largest()', 'from __main__ import largest', number = 1))
print(largest())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文