Blue Bridge Cup Daily One Question (4): Exclusive square number (python)

Topic:

Xiao Ming was staring at the number 203879 in a daze.
It turns out, 203879 * 203879 = 41566646641
What is the magic of this? Observe carefully, 203879 is a 6-digit number, and the number on each digit of it is different, and there are no digits that make up itself on all the digits after its square.
There is another 6-digit number with such characteristics, please find it out!
Summarize the filtering requirements:
1. 6 positive integers
2. The number on each digit is different
3. Each digit of the square number does not contain any constituent digits of the original number The
answer is a 6-digit positive integer.

Solution:

Since this is a fill-in-the-blank question, there is no need to consider the operating efficiency.
You can judge every 6-digit number and
convert each 6-digit number into a list for processing.
Use the count function in the list to determine
if count is greater than 1, it means that "each digit is not satisfied." The condition
flag of “different numbers on” becomes False.
If the condition of “different digits on each digit” is met
, use a list to determine whether “each digit of the square number does not contain any constituent digits of the original number” is satisfied.
If not satisfied The flag becomes False and the
last two conditions are met, and the flag becomes True and output

Code:

i = 100000

while i < 1000000:
    a = str(i)
    list_1 = list(a)
    flag = True

    for j in list_1:
        if list_1.count(j) > 1:
            flag = False
            break

    if flag is True:
        x = i * i
        x = str(x)
        list_2 = list(x)

        for z in list_2:
            if z in list(a):
                flag = False
                break

    if flag is True:
        print(i)

    i += 1

Answer:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_50791900/article/details/112408977