본문 바로가기
Bioinformatics &c/Linux & Python

[Python] Q3. Largest prime factor

by 이모모 2022. 12. 28.
728x90
반응형

Euler라는 사이트에서는 Python 예제를 많이 다루고 있어요!
저는 그 중에서 3번 문제를 풀어봤습니다.

[ Question 3 ] Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

 

[ Answer ]

def smallest_prime_factor(x):
    factor = 1
    for candidate_factor in range (2,int(x**0.5)+1):
        if x%candidate_factor == 0:
            factor = candidate_factor
            break
    return factor
 
        
x = 13195
factors = []
while True:
    smallest_factor = smallest_prime_factor(x)
    if smallest_factor != 1:
        factors.append(smallest_factor)
        x = x / smallest_factor      
    else:
        factors.append(int(x)) 
        break
factors

> 5, 7, 13, 29

 

[ Trouble shooting ]

 

* 틀린 부분 점검

1. if 문에 : 안함
2. 오타.. (폰이라서 그런걸로 ㅎㅎ)
3. while True

728x90
반응형

'Bioinformatics &c > Linux & Python' 카테고리의 다른 글

[Linux] guide_ssh  (0) 2022.12.28
[Linux] install_ggrep  (0) 2022.12.28
[GitHub] Sourcetree를 이용하여 GitHub에 업로드 하는법  (0) 2022.12.28