数学に関するPythonのプログラムです.
n = int(input())
num_list = [True for i in range(n+1)]
num_list[0] = False
num_list[1] = False
for i in range(2,int(n**0.5)+1):
if num_list[i] == True:
p = i
for j in range(p*p,n+1,p):
num_list[j] = False
prime_list = []
for i in range(n+1):
if num_list[i]:
prime_list.append(i)
print(prime_list)
n = int(input())
is_prime = True
for i in range(2,int(n**0.5)+1):
if n%i == 0:
is_prime = False
print(str(i) + "で割り切れる")
break
print(is_prime)
n = int(input())
min_factor = [False for i in range(n+1)]
min_factor[0] = True
min_factor[1] = True
# 最小の素因数を求める
for i in range(2,int(n**0.5)+1):
if min_factor[i] == False:
p = i
min_factor[i] = p
for j in range(p*p,n+1,p):
if min_factor[j] == False:
min_factor[j] = p
for i in range(n+1):
if min_factor[i] == False:
min_factor[i] = i
# 最小の素因数を使って素因数分解を行う
div_list = [[] for i in range(n+1)]
for num in range(2,n+1):
num_ = num
while num > 1:
div_list[num_].append(min_factor[num])
num = int(num/min_factor[num])
print(div_list[n])
n = int(input())
num_list = [True for i in range(int(n**0.5)+1)]
num_list[0] = False
num_list[1] = False
for i in range(2,int(len(num_list)**0.5)+1):
if num_list[i] == True:
p = i
for j in range(p*p,len(num_list),p):
num_list[j] = False
prime_list = []
for i in range(len(num_list)):
if num_list[i]:
prime_list.append(i)
div_list = []
for i in range(len(prime_list)):
p = prime_list[i]
while n%p == 0:
div_list.append(p)
n = n/p
print(div_list)
def gcm(x,y):
if y == 0:
return x
elif x == 0:
return y
else:
if x >= y:
return gcm(x%y,y)
if x < y:
return gcm(x,y%x)
x = int(input())
y = int(input())
print(gcm(x,y),int(x*y/gcm(x,y)))
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
def fibonacci_list(n):
fib_list = [0 for i in range(n+1)]
fib_list[0] = 0
fib_list[1] = 1
for i in range(2,n+1):
fib_list[i] = fib_list[i-1] + fib_list[i-2]
return fib_list[n] # [n]を外せばフィボナッチ数列のリストを返すようにもできます
fibonacci_list(100)
def fibonacci_step(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
a_0 = 0
a_1 = 1
for i in range(n-1):
a_2 = a_0 + a_1
a_0 = a_1
a_1 = a_2
return a_2
fibonacci_step(10000)
# 行列の積の定義
def matrix_multi(A,B):
C_00 = A[0][0]*B[0][0] + A[0][1]*B[1][0]
C_01 = A[0][0]*B[0][1] + A[0][1]*B[1][1]
C_10 = A[1][0]*B[0][0] + A[1][1]*B[1][0]
C_11 = A[1][0]*B[0][1] + A[1][1]*B[1][1]
C = [[C_00,C_01],[C_10,C_11]]
return C
# 繰り返し2乗をする関数
def repeat_pow(A,n):
if n == 1:
return A
elif n%2 == 1:
B = repeat_pow(A,n-1)
return matrix_multi(A,B)
else:
A = repeat_pow(A,n/2)
return matrix_multi(A,A)
A = [[1,1],[1,0]]
print(repeat_pow(A,100)[0][1])