Skip to content

Instantly share code, notes, and snippets.

@reishoku
Last active February 12, 2021 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reishoku/ea5975b9158656c0a5aefa0a66a0589f to your computer and use it in GitHub Desktop.
Save reishoku/ea5975b9158656c0a5aefa0a66a0589f to your computer and use it in GitHub Desktop.
C++のstd::listを使って素因数分解
// 2021-02-12. std::listで素因数分解プログラムを書きたい衝動に襲われたので書いた。
// g++ source.cpp -o prog -lm -std=gnu++14
#include <iostream>
#include <list>
#include <cmath>
using namespace std;
int main(void){
int number = 0;
cout << "input number (> 0): ";
cin >> number;
list<int> l;
int i=2;
while(true){
if(number == 1){
break;
}
if(number%i==0){
l.push_back(i);
number = number / i;
}else{
i++;
continue;
}
}
//}
for(auto j = l.begin(); j != l.end(); j++){
cout << *j << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment