0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【python】プログラミングイントロダクション備忘録①

Last updated at Posted at 2023-07-07

Python言語によるプログラミングイントロダクションについて勉強をしているのでその記録として記事を投稿します。
備忘録です。

総当たりによる平方根の近似

x = float(input('数字入力->'))

epsilon = 0.01
step = epsilon ** 2
num_guesses = 0
ans = 0.0

while abs(ans ** 2 - x) >= epsilon and ans <= x:
    ans += step # 近似
    num_guesses += 1 # ループ数
print(f'繰り返しの数→{num_guesses}')
if abs(ans ** 2 - x) >= epsilon:
    print(f'近似解→{x}')
else:
    print(f'{ans}root→{x}')

2より大きな素数か判定せよ、もし違う場合、最小の約数を表示せよ

def practice_v1():
    # 2より大きな素数か判定せよ、もし違う場合、最小の約数を表示せよ
    x = int(input('数字入力->'))

    smallest_divisor = None # 最小の約数
    if x % 2 == 0:
        smallest_divisor = 2
    else:
        for i in range(3, x, 2):
            if x % i == 0:
                smallest_divisor = i
                break
    if smallest_divisor != None:
        print(f'{x}の最小約数→{smallest_divisor}')
    else:
        print(f'{x}は素数です。')

def practice_v2():
    """2以上1000未満の素数の和"""
    data = 1000
    ans = 0
    cnt = 0
    for prime in range(2, data):
        for prime_1 in range(2, prime):
            if prime % prime_1 == 0:
                break
        else:
            ans += prime
    print(ans, cnt)

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?