A collection of exercises for Python beginners(Q19)
As a Python beginner, I’ve done a lot of practice. Most of these exercises are very interesting. In order for the other beginners to get to learn the basic usage of the language faster, I have compiled the results of my exercises for your reference. As I am a foreign student, this exercise is written in English. Most of the exercises come from 《C语言初学者趣味编程100例》(贾蓓/郭强/刘占敏). A small number of cases come from the Internet. If any case violates your rights, please tell me, and I will delete it as soon as possible.
Q19.Autoconservative number
Question:
Autoconservative number is the mantissa of the square of a number equal to the number itself. For example:52 = 25 252 = 625 762 = 5776 93762 = 87909376.Find all the Autoconservative number within 100000.
Python Code:
import time
start=time.time()
def zishoushu(a):
b=a
list1=[]
while a>0:
list1.append(a%10)
a=a//10
c=pow(b,2)
list2=[]
j=0
while c>0:
list2.append(c%10)
c=c//10
for i in range(len(list1)):
if list1[i]==list2[i]:
j=j+1
if j==len(list1):
print(b)
def zishoushu1(a):
b=a
list1=[]
while a>0:
list1.append(a%10)
a=a//10
c=pow(b,2)
d=c%pow(10,len(list1))
if d==b:
print(b)
for x in range(1,1000000):
zishoushu1(x)
end=time.time()
print(end-start)
Running Results:
1
5
6
25
76
376
625
9376
90625
109376
890625
7.59375