Skip to content

関数の課題 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions euclid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,20 @@
b = input("b の値を入力: ")

# TODO
def gcd(a,b):
while b != 0:
a,b = b, int(a) % int(b)
return a
result3 = gcd(a,b)
print(result3)

def coprime(a,b):
while b != 0:
a,b = b, int(a) % int(b)
return a == 1

result4 = coprime(a,b)
if result4:
print(f"{a}と{b}は互いに素")
else:
print(f"{a}と{b}は互いに素ではない")
6 changes: 5 additions & 1 deletion machine_epsilon.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# TODO
# TODO
epsilon = 1
while 1 + epsilon > 1:
epsilon = epsilon/2
print(epsilon)
21 changes: 20 additions & 1 deletion prime_number.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
a = input("aの値を入力: ")
b = input("bの値を入力: ")

# TODO
# TODO
def check_natural(n):
if not n.isdigit() or int(n)<0:
raise ValueError('{}は自然数ではありません'.format(n))
check_natural(a)
check_natural(b)

def is_prime(n):
if int(n) < 2:
return False
for i in range(2,int(n)):
if int(n) % i == 0:
return False
return True

for x in [a,b]:
if is_prime(x):
print('{}は素数'.format(x))
else:
print('{}は素数ではない'.format(x))
37 changes: 37 additions & 0 deletions trapezoidal_integral.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,40 @@
# print(sin(0))
# >>> 0
# -----------
from math import pi

def integral(f,a=0,b=1,n=100):
S=0
h = (b - a)/n
for i in range(1,n+1):
S += h/2 * (f(a + (i-1)*h)+f(a+i*h))
return (S)

result1 = integral(sin,0,pi/2,50)
print('(1){}'.format(result1))

def func2(x):
return 4/(1+x**2)

result2 = integral(func2)
print('(2){}'.format(result2))

from math import sqrt
from math import exp
def func3(x):
return sqrt(pi)*exp(-x)

result3 = integral(func3,-100,100,1000)
print('(3){}'.format(result3))