pythonコード:水仙の花の数+泡の順序付け+推測+完全数+公倍公約数
31215 ワード
練習1:
水仙の数:
練習2:
バブルソート
練習3:当て
練習4:完全数
練習5:9*9乗算表
python3
最大公約数
最小公倍数
判定範囲内の素数(素数)
sum = []for i in range(2,100):
for j in range(2, i):
if i%j == 0:
break
if j == i-1:
sum.append(i)
print(sum)
水仙の数:
'''
'''
import math
#
def pow3(list):
sum = 0
for i in list:
sum += math.pow(int(i),3)
return sum
def flower(): # 1
for i in range(100,1000):
a = i // 1000 #
b = i //100 % 10 #
c = i //10 % 10 #
d = i % 10 #
list = [a,b,c,d]
if pow3(list) == i:
print('100-1000 :%d'%i)
def flower2(): # 2: list
for i in range(100, 1000):
list1 = list(str(i))
if pow3(list1) == i:
print('100-1000 :%d'%i)
def flower3(a,b): # ( raw_input )
for i in range(a,b):
list = [] # 、
r = i
while r:
r,n = divmod(r,10)
list.append(n)
if pow3(list) == i:
print('100-1000 :%d'%i)
練習2:
バブルソート
'''
python:
'''
class Sort(object):
def __init__(self,lists):
if isinstance(lists,list): # type(lists).__name__
self.lists = lists
elif isinstance(lists,str):
self.lists = list(lists)
else:
print(' ')
def bubble(self): #
print(' : ',self.lists)
length = len(self.lists)
count = 0
for i in range(length):
for j in range(i+1,length):
if self.lists[i] > self.lists[j]: #
count +=1
print('%d. , '%count,self.lists[i],self.lists[j])
self.lists[i],self.lists[j] = self.lists[j],self.lists[i]
print(' :',self.lists)
print(' :',self.lists)
return self.lists
import random
def random_int(length): # length 1-100
list = []
for i in range(length):
list.append(random.randint(1,100))
return list
練習3:当て
import random
def guess(number):
numMax = 99
numMin = 1
count = 0
# num = input(' %d-%d :'%(numMin,numMax))
while True:
num = input(' %d-%d :'%(numMin,numMax))
if num < number:
numMin = num
print(" %d-%d"%(numMin,numMax))
count += 1
# continue
elif num > number:
numMax = num
print(" %d-%d"%(numMin,numMax))
count += 1
else:
print "Guess Right for %d times"%count
break
if __name__=='__main__':
a = random.randint(1,99)
guess(a)
from random import randint
def guessNum():
num = randint(0,100) #
flag = False
small_num = 0
big_num = 100
count = 0
while flag == False:
raw_num = int(input(" :"))
if raw_num == num:
count +=1
flag = True
elif raw_num > num:
count += 1
big_num = raw_num
print(" , %d - %d "%(small_num,raw_num))
elif raw_num < num:
count += 1
small_num = raw_num
print(" , %d - %d "%(raw_num,big_num))
else:
print(' 0-100 ')
print('count: %d'%count)
guessNum()
練習4:完全数
def isPerfect(num):
s = sum(i for i in range(1,num) if not (num%i))
if s == num:
print '%d is perfect!'%num
for number in range(2,10000):
isPerfect(number)
練習5:9*9乗算表
def print99():
for i in range(1,10):
for j in range(1,i+1):
print '%d*%d=%d '%(j,i,i*j),
print '
'
print99()
python3
print('%d*%d=%d '%(j,i,i*j),end='')
最大公約数
# coding:utf-8
def hcf(x,y):
smaller = x if x < y else y
hcf = [i for i in range(1, smaller+1) if not (x%i) and not (y%i)]
return hcf[-1]
hcf(54,24)
最小公倍数
def lcm(x,y):
bigger = x if x > y else y
lcm = [i for i in range(bigger, x*y+1) if not (i%x) and not (i%y)]
return lcm[0]
lcm(54,24)
判定範囲内の素数(素数)
sum = []for i in range(2,100):
for j in range(2, i):
if i%j == 0:
break
if j == i-1:
sum.append(i)
print(sum)