Skip to content

Instantly share code, notes, and snippets.

@Tdrj2716
Last active March 14, 2020 02:32
Show Gist options
  • Save Tdrj2716/f0f1c350a6abd32b0e2fe2912fa433f7 to your computer and use it in GitHub Desktop.
Save Tdrj2716/f0f1c350a6abd32b0e2fe2912fa433f7 to your computer and use it in GitHub Desktop.
素数判定のサンプルコード、及び素因数分解の問題(https://onlinejudge.u-aizu.ac.jp/problems/NTL_1_A )のソースコード
#include <iostream>
using namespace std;
int main()
{
int n; cin >> n; cout << n << ":";
int i = 2;
while(n != 1)
{
if(n % i == 0)
{
n /= i;
cout << " " << i;
}
else if (n < i * i) //nが大きい素数である場合がありO(√n)が最適解
{
cout << " " << n;
break;
}
else
{
i++;
}
}
cout << endl;
return 0;
}
bool isPrime(int n){
if(n <= 1) return false;
if(n == 2) return true;
for(int i = 2; i*i <= n; i++)
if(n % i == 0) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment