일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 너비 우선 탐색
- BFS
- tensorflow
- pytorch
- 이분 탐색
- 알고리즘
- Overfitting
- 분할 정복
- 크루스칼
- 플로이드 와샬
- 가끔은 말로
- 회고록
- object detection
- 2023
- c++
- 조합론
- dfs
- 가끔은_말로
- DP
- 세그먼트 트리
- lazy propagation
- back propagation
- 우선 순위 큐
- 미래는_현재와_과거로
- 다익스트라
- dropout
- 문자열
- 자바스크립트
- 백트래킹
- NEXT
Archives
- Today
- Total
Doby's Lab
Naive Factorization Code 본문
Pollard's Rho를 공부해보기 전에 직접 인수 분해 (소인수 분해) 알고리즘 코드를 작성해봤습니다.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<int> getPrime(int n){
vector<bool> isPrime(n, false);
vector<int> ret;
for(int i = 2; i <= n; i++){
if(!isPrime[i]){
ret.push_back(i);
for(int j = i + i; j <= n; j += i){
isPrime[j] = true;
}
}
}
return ret;
}
vector<int> factorization(int n){
auto prime = getPrime(n);
vector<int> ret;
for(int i = 0; i < prime.size(); i++){
if(n == 1) break;
if(n % prime[i] == 0){
while(n % prime[i] == 0){
ret.push_back(prime[i]);
n /= prime[i];
}
}
}
return ret;
}
int main(){
int n;
cin >> n;
auto res = factorization(n);
// n's factor
for(auto factor : res) cout << factor << " ";
return 0;
}
728x90
'PS > Study Note' 카테고리의 다른 글
PS 공부법, 아이디어 백트래킹 (Idea Backtracking) (1) | 2022.08.24 |
---|---|
Miller-Rabin 소수 판별법 Code (0) | 2022.07.30 |
Bitmasking 기본적인 연산 (0) | 2022.07.17 |
Extended Euclidean Algorithm (증명 X) (0) | 2022.06.20 |
Euclidean Algorithm Code(반복, 재귀) (0) | 2022.06.18 |