두 개의 3 자리 숫자로 구성된 최대 회문을 찾으려고합니다. 내 손님은 회문이 형태가 abccba
이 될 것이므로 각 숫자를 반복하고 두 자리 3 자리 숫자의 곱인 가장 큰 숫자로 멈추게 할 것입니다.Nested Loop in Python
코드
def hasLargeDivisors(n):
"""
Function to determine if a number has two divisors
greater than 99
"""
d = 999
while n/d > 99 and n/d < 999 and d > 99:
if n % d is 0:
return True
d-=1
return False
def compisitePalindrome():
"""
Function to find the largest palindrome
that is the product of 2 three-digit numbers
"""
for a in reversed(xrange(1, 9)):
for b in reversed(xrange(0, 9)):
for c in reversed(xrange(0, 9)):
num = a*100001 + b*10010 + c*1100
if hasLargeDivisors(num):
return num
return 0
이 조각 올바르지 인 888,888 = 962 * (924)을 생성한다.
이 코드
def hasLargeDivisors(n):
"""
Function to determine if a number has two divisors
greater than 99
"""
d = 999
while n/d > 99 and n/d < 999 and d > 99:
if n % d is 0:
return True
d-=1
return False
def compisitePalindrome():
"""
Function to find the largest palindrome
that is the product of 2 three-digit numbers
"""
a = 9
for b in reversed(xrange(0, 9)):
for c in reversed(xrange(0, 9)):
num = a*100001 + b*10010 + c*1100
if hasLargeDivisors(num):
return num
return 0
올바른 인 906,609 = 993 * (913)을 생성한다.
어디서 잘못되었는지 알 수 없습니다.
palindrome 인 숫자가 '11'로 나눌 수 있다는 사실을 고려하여 알고리즘을 매우 단순화 할 수 있습니다. 그래서 두 개의 3 자리 숫자의 배수 인 '11 '의 가장 높은 배수로 시작한 다음'xrange'에서'-11'을 사용하여 단계를 밟고 그 숫자가 회귀 식인지 확인하십시오. –
감사합니다. 그들은 해결책을 준 답을 제공했습니다. 나는 지금 그것을 읽고 있고 또한 당신이 언급 한 것을 제안합니다. –