Last active
March 14, 2020 02:32
-
-
Save Tdrj2716/f0f1c350a6abd32b0e2fe2912fa433f7 to your computer and use it in GitHub Desktop.
素数判定のサンプルコード、及び素因数分解の問題(https://onlinejudge.u-aizu.ac.jp/problems/NTL_1_A )のソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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