728x90
https://www.acmicpc.net/problem/4355
4355번: 서로소
입력은 여러 개의 테스트 케이스로 이루어져 있으며, 각 테스트 케이스는 n ≤ 1,000,000,000으로 이루어져 있다. 입력의 마지막 줄에는 0이 주어진다.
www.acmicpc.net
11689번 문제와 다를 게 없다. 아래 링크 참고.
1) 0을 입력 받기 전까지 while문에서 계속 순환하고,
2) 1이 입력되면 0이 출력되어야 한다는 점만 다르다. (입력으로 주어진 n마다 n보다 작으면서 서로소인 양의 정수의 수를 출력한다.)라는 조건 때문.
https://seolpark.tistory.com/76
[Python]BAEKJOON 11689 번 GCD(n, k) = 1
https://www.acmicpc.net/problem/11689 11689번: GCD(n, k) = 1 자연수 n이 주어졌을 때, GCD(n, k) = 1을 만족하는 자연수 1 ≤ k ≤ n 의 개수를 구하는 프로그램을 작성하시오. www.acmicpc.net 이 문제를 풀기 위해서는
seolpark.tistory.com
Sol1
while True:
N = int(input())
if N == 0: break
lst = []
res = N
for i in range(2,int(N**0.5)+1):
if N%i == 0:
lst.append(i) #소인수 구함
lst.append(N//i)
lst = list(set(lst))
if lst:
for i in lst:
for j in range(2,int(i**0.5)+1): #소수 구함
if i%j == 0:break #합성수
else: res *= (1-1/i)
print(int(res))
else:
if N ==1: print(0)
else: print(N-1)
Sol2
while True:
N = int(input())
if N==0: break
if N==1:
print(0)
continue
res = N
for i in range(2,int(N**0.5)+1):
if N%i == 0:
while N%i ==0:
N //= i
res *= (1-1/i)
if N>1:
res *= 1-1/N
print(int(res))
'Baekjoon Algorithm > python' 카테고리의 다른 글
[Python]BAEKJOON 11723번 집합 (0) | 2023.02.27 |
---|---|
[Python]BAEKJOON 1620번 나는야 포켓몬 마스터 이다솜 (0) | 2023.02.25 |
[Python]BAEKJOON 11689 번 GCD(n, k) = 1 (0) | 2023.02.23 |
[Python]BAEKJOON 11005번 진법 변환 2 (0) | 2023.02.23 |
[Python]BAEKJOON 13277번 큰 수 곱셈 (0) | 2023.02.22 |
댓글