简体   繁体   English

欧拉计划问题编号 4

[英]Project Euler problem number 4

n = 0
for a in xrange(999, 100, -1):
    for b in xrange(a, 100, -1):
        x = a * b
        if x > n:
            s = str(a * b)
            if s == s[::-1]:
                 n = a * b
print n

I have a question about this solution to the problem .我对这个问题的解决方案有疑问。 I know it is right but I am wondering why in the xrange(999,100,-1) the -1 is there For the a and b for loops.我知道这是对的,但我想知道为什么在xrange(999,100,-1)中存在-1对于 a 和 b for 循环。 Please explain.请解释。 I am new to this :)我是新来的 :)

The third parameter to xrange() is the increment value. xrange()的第三个参数是增量值。 The default is 1, which means the counter will count in an increasing direction.默认值为 1,表示计数器将按递增方向计数。 To count in a decreasing direction, use -1.要按递减方向计数,请使用 -1。 Your a counter will go from 999 to 101 (the xrange() iterator stops just before it reaches the second parameter value).a计数器将从 999 变为 101( xrange()迭代器在它到达第二个参数值之前停止)。

For future reference, see the xrange() documentation .如需将来参考,请参阅xrange()文档

The -1 specifies a negative step. -1 指定负步骤。 Thus moving from 999 descending to 100 (exclusive).因此从 999 下降到 100(不包括)。

xrange function takes three arguments: start , stop and step . xrange函数接受三个参数: startstopstep It returns a range of numbers starting from start continuing to stop , but not including it.它返回从startstop的一系列数字,但不包括它。 If 'start' is bigger than stop , negative step must be provided.如果 'start' 大于stop ,则必须提供负步长。

So basically xrange(999, 100, -1) will give you [999, 998, ..., 101]所以基本上xrange(999, 100, -1)会给你[999, 998, ..., 101]

这意味着您正在以 1 为增量减少(因此是负号),从 999 到 100

This is what I got for the project Euler #4 question:这是我为项目 Euler #4 问题得到的结果:

def Palindrome(s):
    if s == s[::-1]:
        return True
    else:
        return False

i = 100
j = 100
greatest = 0
while (i <= 999):
    while (j <= 999):
        product = i * j
        if (product > greatest and Palindrome(str(product))):
            greatest = product
        j += 1
    j = 100
    i += 1
print "Answer: " + str(greatest)

-M1K3 -M1K3

My Python solution:我的 Python 解决方案:

container = []

for i in range(100, 999):
    for j in range(100, 999):
        num = i * j
        if str(num) == str(num)[::-1]:
            container.append(num)

print(max(container))
__author__ = 'shreysatapara'
f=0
for a in range(100,1000):
    for b in range(100,1000):
        c=a*b
        d=c
        s=0
        x=0
        for i in range(0,len(str(c))):
            s=c%10
            x=x*10+s
            c=c//10
            if(x==d):
                if(x>f):
                    f=x
                    print(f)

bingo answer is your last number in compiler....宾果游戏答案是您在编译器中的最后一个数字....

import time
start_time = time.time()
largest_number = 0
for i in range(999,100,-1):
    for j in range(i,100,-1):
        string_number = str(i * j)
        if string_number == string_number[::-1]:
            if i*j > largest_number:
                largest_number = i * j
print(largest_number)
print(time.time() - start_time," seconds")
for x in range(100, 1000):
    for y in range(100, 1000):
        product = x*y
        if product > x*y:
            break
        if str(product) == str(product)[::-1] and product > 900000:
            print (x, y, product)

The negative one is the modifier.负数是修饰符。 Basically the loops start at 999, end at 100, and get there by modifying each number by negative one.基本上循环从 999 开始,在 100 结束,然后通过将每个数字修改为负一来到达那里。

def reverse(s):
    str = ""
    for i in s:
        str = i + str
    return str


MAXN = 0
for i in range(100, 1000):
    for j in range(100, 1000):
        a = i * j
        b = str(a)
        if b == reverse(b) and a > MAXN:
            MAXN = a
print(MAXN)

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

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