あなたのwhileループ:
while (nums>=num1 and nums<=num2 and flag):
#for nums in range(num1, num2+1):
num_str = str(nums)
if num_str == num_str[::-1]:
pal += 1
else:
count = 0
num_el = num_str
while (count < 60):
np_total = int(num_el) + int(num_el [::-1])
count += 1
nums_el = str(np_total)
num_el = nums_el
if num_el == nums_el [::-1]:
nonlych += 1
flag = False
else:
lychrel += 1
print(nums, "looks like a lychrel number")
nums += 1
はwhile
が終了ループするたびに実行されます。あなたのfor
ループ内のbreak
はそれをスキップしていました。
flag
がFalse
に設定されていると、外側のwhile
ループが停止するため、最初に見つかった非リカール番号が最後にテストする番号になります。
ここではできるだけ変更しないようにしています。情報を渡すためにcount
変数を使用する代わりに、isLychrel
のようなフラグを追加できます。
nums = num1
while (nums>=num1 and nums<=num2):
num_str = str(nums)
if num_str == num_str[::-1]:
pal += 1
else:
count = 0
num_el = num_str
while (count < 60):
np_total = int(num_el) + int(num_el [::-1])
count += 1
nums_el = str(np_total)
num_el = nums_el
if num_el == nums_el [::-1]:
nonlych += 1
count = 999 # breaks while loop
if (count != 999):
lychrel += 1
print(nums, "looks like a lychrel number")
nums += 1
Lychrel番号の存在は決して証明されていないので、どのようにカウントするのですか? –
whileループでブレークを使用しないのはなぜですか?あなたの質問を明確にすることができますか?何がうまくいきませんか? –